본문 바로가기

BOJ

백준 9019 / DSLR




현재까지 수행했던 경로를 string으로 담거나 역추적을 하면 된다.


string도 vector처럼 push_back과 pop_back이 가능하니 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include<stdio.h>
#include<iostream>
#include<vector>
#include<queue>
#include<memory.h>
#include<string>
using namespace std;
 
typedef struct DATA {
 
    int x;
    string path;
    int from;
    int cnt;
 
}DATA;
 
queue<DATA>q;
int a, b;
bool check[10001];
 
int main() {
 
    int testcase;
    cin >> testcase;
 
 
    while (testcase--) {
 
        memset(check, falsesizeof(check));
        cin >> a >> b;
        DATA d;
        d.x = a;
        d.cnt = 0;
        d.path = "";
        q.push(d);
 
 
        while (!q.empty()) {
            int now = q.front().x;
            int cn = q.front().cnt;
            int fm = q.front().from;
            string pt = q.front().path;
 
            if (now == b) {
 
                cout << pt << endl;;
 
                while (!q.empty()) {
                    q.pop();
                }
 
                break;
            }
            q.pop();
 
 
 
            int d1, d2, d3, d4;
 
            d1 = now / 1000;
            d2 = (now - d1 * 1000/ 100;
            d3 = (now - d1 * 1000 - d2 * 100/ 10;
            d4 = (now - d1 * 1000 - d2 * 100 - d3 * 10);
 
            DATA data;
 
            for (int i = 0; i < 4; i++) {
                //D
                if (i == 0) {
 
                    int nx = (now * 2) % 10000;
 
                    if (check[nx] == false) {
                        check[nx] = true;
                        data.x = nx;
                        pt.push_back('D');
                        data.path = pt;
 
                        data.from = now;
 
                        data.cnt = cn + 1;
 
                        q.push(data);
                        pt.pop_back();
                    }
                }
                //S
                if (i == 1) {
 
                    int nx;
                    if (now > 0) {
                        nx = now - 1;
                    }
                    if (now == 0) {
                        nx = 9999;
                    }
                    if (check[nx] == false) {
                        check[nx] = true;
                        data.x = nx;
                        pt.push_back('S');
                        data.path = pt;
                        data.from = now;
                        data.cnt = cn + 1;
 
 
                        q.push(data);
                        pt.pop_back();
                    }
 
                }
 
                //L
                if (i == 2) {
 
                    int nx;
                    nx = d2 * 1000 + d3 * 100 + d4 * 10 + d1;
                    if (check[nx] == false) {
                        check[nx] = true;
                        data.x = nx;
 
                        pt.push_back('L');
                        data.path = pt;
                        data.from = now;
 
                        data.cnt = cn + 1;
 
                        q.push(data);
                        pt.pop_back();
                    }
                }
                //R
                if (i == 3) {
 
                    int nx;
                    nx = d4 * 1000 + d1 * 100 + d2 * 10 + d3;
                    if (check[nx] == false) {
                        check[nx] = true;
                        data.x = nx;
 
                        pt.push_back('R');
                        data.path = pt;
                        data.from = now;
                        data.cnt = cn + 1;
                        q.push(data);
                        pt.pop_back();
                    }
                }
            }
 
 
 
        }
 
    }
 
}
cs


'BOJ' 카테고리의 다른 글

백준 1967 / 트리의 지름  (0) 2019.01.07
백준 2146 / 다리 만들기  (0) 2019.01.05
백준 2573 / 빙산  (0) 2019.01.04
백준 2589 / 보물섬  (0) 2019.01.02
백준 7569 / 토마토  (0) 2019.01.02