BOJ : https://www.acmicpc.net/problem/9019
github : https://github.com/junho0956/Algorithm/blob/master/9019/9019/%EC%86%8C%EC%8A%A4.cpp
문제에서 요구하는 그대로 작성하였다.
코드를 정확히 하지않으면 1000 같은 숫자는 L 했을 때 1 이 되고 R 했을 때 100 이 되는 경우들에 대한
반례가 분명 생길 것 같아서
어차피 4자리 숫자이니 한자리씩 가지고 와서 체크후, BFS 해도 괜찮을 것 같았다.
코드에 대한 설명은 주석처리 해놓았다.
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
|
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
// 한자리씩 가지고오기 위한 구조체
typedef struct number {
int init[4];
};
number result;
// 어떤 명령어를 사용했는지 체크하기 위한 배열
pair<char, int> parent[10000];
bool visit[10000];
int Testcase, A, B, now;
queue<int> q;
stack<char> s;
void now_check(int num) {
int idx = 3;
while (num) {
num /= 10;
}
}
void D() {
for (int i = 1; i < 4; i++) {
}
temp = (temp * 2) % 10000;
if (!visit[temp] && parent[temp].first == NULL) {
parent[temp].first = 'D';
parent[temp].second = now;
q.push(temp);
}
}
void S() {
for (int i = 1; i < 4; i++) {
}
if (temp == 0) temp = 9999;
else temp--;
if (!visit[temp] && parent[temp].first == NULL) {
parent[temp].first = 'S';
parent[temp].second = now;
q.push(temp);
}
}
void L() {
if (!visit[temp] && parent[temp].first == NULL) {
parent[temp].first = 'L';
parent[temp].second = now;
q.push(temp);
}
}
void R() {
if (!visit[temp] && parent[temp].first == NULL) {
parent[temp].first = 'R';
parent[temp].second = now;
q.push(temp);
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> Testcase;
while (Testcase--) {
for (int i = 0; i < 10000; i++) parent[i].first = NULL;
for (int i = 0; i < 10000; i++) visit[i] = 0;
cin >> A >> B;
q.push(A);
while (!q.empty()) {
now = q.front();
q.pop();
if (visit[now]) continue;
visit[now] = true;
if (now == B) {
while (!q.empty()) q.pop();
break;
}
// now_check 를 통해서 구조체에 한자리씩 저장해둔다
now_check(now);
// 주어진 조건에 맞게 함수내에서 q push
D(), S(), L(), R();
}
int temp = B;
// 부모를 따라가는 과정
while (parent[temp].first != NULL) {
s.push(parent[temp].first);
temp = parent[temp].second;
}
while (!s.empty()) {
cout << s.top();
s.pop();
}
cout << '\n';
}
return 0;
}
|
|
|
'algorithm > BOJ' 카테고리의 다른 글
BOJ 2251번 물통 (0) | 2020.01.24 |
---|---|
BOJ 1525번 퍼즐 (0) | 2020.01.24 |
BOJ 1963번 소수 경로 (0) | 2020.01.23 |
BOJ 13913번 숨바꼭질4 (0) | 2020.01.23 |
BOJ 13549번 숨바꼭질3 (0) | 2020.01.22 |