-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboj_9019.java
More file actions
89 lines (71 loc) · 2.41 KB
/
boj_9019.java
File metadata and controls
89 lines (71 loc) · 2.41 KB
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
package dfs_bfs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
//참고: https://superohinsung.tistory.com/103
public class boj_9019 {
static int T, answer, result;
static boolean[] visited;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder sb = new StringBuilder();
T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
st = new StringTokenizer(br.readLine());
answer = Integer.parseInt(st.nextToken());
result = Integer.parseInt(st.nextToken());
visited = new boolean[10000];
visited[answer] = true;
Queue<Register> que = new LinkedList<>();
que.add(new Register(answer, ""));
while (!que.isEmpty()) {
Register cur = que.poll();
if (cur.num == result) {
sb.append(cur.command).append("\n");
break;
}
if (!visited[cur.D()]) {
que.add(new Register(cur.D(), cur.command + "D"));
visited[cur.D()] = true;
}
if (!visited[cur.S()]) {
que.add(new Register(cur.S(), cur.command + "S"));
visited[cur.S()] = true;
}
if (!visited[cur.L()]) {
que.add(new Register(cur.L(), cur.command + "L"));
visited[cur.L()] = true;
}
if (!visited[cur.R()]) {
que.add(new Register(cur.R(), cur.command + "R"));
visited[cur.R()] = true;
}
}
}
System.out.println(sb);
}
static class Register {
int num;
String command;
Register(int num, String command) {
this.num = num;
this.command = command;
}
int D() {
return (num * 2) % 10000;
}
int S() {
return num == 0 ? 9999 : num - 1;
}
int L() {
return num % 1000 * 10 + num / 1000;
}
int R() {
return num % 10 * 1000 + num / 10;
}
}
}