BOJ 9019 DSLR

1 개요[ | ]

BOJ 9019 DSLR

2 C++[ | ]

#include <bits/stdc++.h>
using namespace std;

int A, B;
bool visited[10000];

void solve() {
    fill(visited, visited+10000, 0);
    queue<pair<int, string>> q;
    q.push(make_pair(A, ""));
    visited[A] = true;
    int num;
    string op;
    int D, S, L, R, temp;
    while (!q.empty()) {
        num = q.front().first;
        op = q.front().second;
        q.pop();
        if (num == B) {
            cout << op << '\n';
            return;
        }
        D = (num * 2) % 10000;
        if (!visited[D]) {
            visited[D] = true;
            q.push(make_pair(D, op + "D"));
        }
        S = num - 1 < 0 ? 9999 : num - 1;
        if (!visited[S]) {
            visited[S] = true;
            q.push(make_pair(S, op + "S"));
        }
        L = (num % 1000) * 10 + (num / 1000);
        if (!visited[L]) {
            visited[L] = true;
            q.push(make_pair(L, op + "L"));
        }
        R = num / 10 + (num % 10) * 1000;
        if (!visited[R]) {
            visited[R] = true;
            q.push(make_pair(R, op + "R"));
        }
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T;
    cin >> T;
    while (T--) {
        cin >> A >> B;
        solve();
    }
}