-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP9019.java
More file actions
69 lines (56 loc) · 1.29 KB
/
P9019.java
File metadata and controls
69 lines (56 loc) · 1.29 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
package BOJ;
import java.util.*;
public class P9019 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int test_case = sc.nextInt();
while(test_case-- > 0){
int start = sc.nextInt();
int last = sc.nextInt();
boolean[] check = new boolean[10000];
String[] line = new String[10000];
String ans = bfs(check, line, start, last);
System.out.println(ans);
}
}
public static String bfs(boolean[] check, String[] line, int start, int last){
Queue<Integer> q = new LinkedList<Integer>();
q.add(start);
check[start] = true;
line[start] = "";
int position = 0;
int c = start;
while(!q.isEmpty()){
position = q.remove();
if(position==last){
return line[position];
}
c = (position*2)%10000;
if(check[c]==false){
q.add(c);
check[c] = true;
line[c] = line[position]+"D";
}
c = position-1;
if(c==-1) c = 9999;
if(check[c]==false){
check[c] = true;
q.add(c);
line[c] = line[position]+"S";
}
c = (position%1000)*10+position/1000;
if(check[c]==false){
q.add(c);
check[c] = true;
line[c] = line[position]+"L";
}
c = (position%10)*1000+position/10;
if(check[c]==false){
q.add(c);
check[c] = true;
line[c] = line[position]+"R";
}
}
return line[last];
}
}