728x90
https://www.acmicpc.net/problem/9019
// 코드
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
|
import java.util.*;
import java.io.*;
public class Main {
static class Register {
// 숫자를 연산해간다.
int num;
// 결과적으로는 답이되는 멤버변수이다.
String cmd;
// 생성자
Register(int num, String cmd) {
this.num = num;
this.cmd = cmd;
}
// 메서드
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 ); }
}
// main
public static void main(String[] args) throws Exception{
// Please Enter Your Code Here
StringBuilder sb = new StringBuilder();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
Deque<Register> deq = null;
boolean[] visited = null;
int[] val = null;
while(t --> 0) {
val = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
visited = new boolean[10000];
visited[val[0]] = true;
deq = new ArrayDeque<>();
deq.offer(new Register(val[0], ""));
while( !deq.isEmpty() ) {
Register cur = deq.poll();
if( cur.num == val[1] ) {
sb.append(cur.cmd + "\n");
break;
}
if( !visited[cur.D()] ) {
deq.offer( new Register(cur.D(), cur.cmd + "D") );
visited[cur.D()] = true;
}
if( !visited[cur.S()] ) {
deq.offer( new Register(cur.S(), cur.cmd + "S") );
visited[cur.S()] = true;
}
if( !visited[cur.L()] ) {
deq.offer( new Register(cur.L(), cur.cmd + "L") );
visited[cur.L()] = true;
}
if( !visited[cur.R()] ) {
deq.offer( new Register(cur.R(), cur.cmd + "R") );
visited[cur.R()] = true;
}
}
}
bw.write(sb.toString());
bw.flush();
bw.close();
br.close();
}
}
|
cs |
반응형