BOJ::문제풀이
9019 DSLR
2영재
2018. 4. 13. 22:18
1. D, S, L, R 기능을 각각 구현한다.
2. 인트형과 스트링형을 담는 구조체를 만든다.
3. bfs탐색을 통해 가장 먼저 목표값을 찾는 string 을 출력한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #include <iostream> #include <string> #include <queue> using namespace std; struct Node { int n; string str; Node(int n,string str) :n(n),str(str) {} }; int t; string res; int funcD(int n) { return (n * 2 > 9999) ? (n * 2) % 10000 : 2 * n; } int funcS(int n) { return (n == 0) ? 9999 : n - 1; } int funcL(int n) { int ans = n / 1000; n -= (ans * 1000); n *= 10; ans += n; return ans; } int funcR(int n) { int ans = (n % 10) * 1000; n /= 10; ans += n; return ans; } void bfs(int a,int b) { queue<Node> q; q.push(Node(a,"")); bool visited[10000] = { false, }; visited[a] = true; while (!q.empty()) { int n = q.front().n; string str = q.front().str; q.pop(); if (n == b) { res += str + "\n"; return; } int next; next = funcD(n); if (!visited[next])q.push(Node(next, str + "D")); visited[next]=true; next = funcS(n); if (!visited[next])q.push(Node(next, str + "S")); visited[next] = true; next = funcL(n); if (!visited[next])q.push(Node(next, str + "L")); visited[next] = true; next = funcR(n); if (!visited[next])q.push(Node(next, str + "R")); visited[next] = true; } } int main(){ cin >> t; while (t--) { int a, b; cin >> a >> b; bfs(a, b); } cout << res; } | cs |