File tree Expand file tree Collapse file tree 1 file changed +78
-0
lines changed
Expand file tree Collapse file tree 1 file changed +78
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
7+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
8+
9+ public static void main (String [] args ) throws Exception {
10+ int T = Integer . parseInt(br. readLine());
11+ while (T -- > 0 ) {
12+ StringTokenizer st = new StringTokenizer (br. readLine());
13+ int A = Integer . parseInt(st. nextToken());
14+ int B = Integer . parseInt(st. nextToken());
15+ bfs(A , B );
16+ }
17+ bw. flush();
18+ bw. close();
19+ }
20+
21+ static void bfs (int start , int target ) throws IOException {
22+ boolean [] visited = new boolean [10000 ];
23+ int [] prev = new int [10000 ];
24+ char [] op = new char [10000 ];
25+
26+ ArrayDeque<Integer > q = new ArrayDeque<> ();
27+ visited[start] = true ;
28+ q. add(start);
29+
30+ while (! q. isEmpty()) {
31+ int cur = q. poll();
32+ if (cur == target) break ;
33+
34+ // D
35+ int next = (cur * 2 ) % 10000 ;
36+ if (! visited[next]) {
37+ visited[next] = true ;
38+ prev[next] = cur;
39+ op[next] = ' D' ;
40+ q. add(next);
41+ }
42+ // S
43+ next = (cur == 0 ? 9999 : cur - 1 );
44+ if (! visited[next]) {
45+ visited[next] = true ;
46+ prev[next] = cur;
47+ op[next] = ' S' ;
48+ q. add(next);
49+ }
50+ // L
51+ next = (cur % 1000 ) * 10 + cur / 1000 ;
52+ if (! visited[next]) {
53+ visited[next] = true ;
54+ prev[next] = cur;
55+ op[next] = ' L' ;
56+ q. add(next);
57+ }
58+ // R
59+ next = (cur % 10 ) * 1000 + cur / 10 ;
60+ if (! visited[next]) {
61+ visited[next] = true ;
62+ prev[next] = cur;
63+ op[next] = ' R' ;
64+ q. add(next);
65+ }
66+ }
67+
68+ StringBuilder sb = new StringBuilder ();
69+ for (int p = target; p != start; p = prev[p]) {
70+ sb. append(op[p]);
71+ }
72+ bw. write(sb. reverse(). toString());
73+ bw. write(' \n ' );
74+ }
75+ }
76+
77+
78+ ```
You can’t perform that action at this time.
0 commit comments