Home BOJ. DSLR (9019)
Post
Cancel

BOJ. DSLR (9019)

[Link] 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
  took too long for debugging this error

  StringBuilder sb1 = new StringBuilder();
	StringBuilder sb2 = new StringBuilder();
  sb1.append('c');
	sb2.append('c');
	sb1.toString().equals(sb2.toString()) => true
	char[] ch = new char[3];
	sb2.append(ch[0]);
  sb1.toString().equals(sb2.toString()) => false
*/
import java.util.*;
import java.io.*;

public class Main {
	static BufferedReader br;
	static int max = 0, maxIdx = 0;
	public static void main(String[] args) throws IOException {
		StringBuilder sb1 = new StringBuilder();
		StringBuilder sb2 = new StringBuilder();
		br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		String[] line ;
		int t = toi(br.readLine());
		for(int iter = 0; iter < t; iter++) {
			line = getLine();
			int a = toi(line[0]), b = toi(line[1]);
			int[] parent = new int[10000];
			Arrays.fill(parent, -1);
			char[] oper = new char[10000];
			Queue<Integer> q = new LinkedList<>();
			q.add(a);
			parent[a] = -2;
			while(!q.isEmpty()) {
				int cur = q.poll();
				if(cur == b) break;
				int db = doublef(cur), leftV = left(cur), rightV = right(cur), deV = decrement(cur);
				if(parent[db] == -1) {
					parent[db] = cur;
					oper[db] = 'D';
					q.add(db);
				}
				if(parent[deV] == -1) {
					parent[deV] = cur;
					oper[deV] = 'S';
					q.add(deV);
				}
				if(parent[leftV] == -1) {
					parent[leftV] = cur;
					oper[leftV] = 'L';
					q.add(leftV);
				}
				if(parent[rightV] == -1) {
					parent[rightV] = cur;
					oper[rightV] = 'R';
					q.add(rightV);
				}
			}
			ArrayList<Character> al = new ArrayList<>();
			int idx = b;
			while(parent[idx] != -2) {
				al.add(oper[idx]);
				idx = parent[idx];
			}
			for(int i = al.size() - 1; i >=0; i--) sb.append(al.get(i));
			sb.append("\n");
		}
		print(sb);
	}

	static int doublef(int n) {
		n *= 2;
		if(n >= 10000) n-= 10000;
		return n;
	}

	static int decrement(int n) {
		return n == 0 ? 9999 : n - 1;
	}

	static int[] getDigits(int n) {
		int d1 = n / 1000;
		n-= 1000*d1;
		int d2 = n / 100;
		n-= 100 * d2;
		int d3 = n / 10;
		int d4 = n - 10 * d3;
		return new int[] { d1, d2, d3, d4 };
	}

	static int right(int n) {
		int[] digits = getDigits(n);
		return 1000 * digits[3] + 100 * digits[0] + 10 * digits[1] + digits[2];
	}

	static int left(int n) {
		int[] digits = getDigits(n);
		return 1000 * digits[1] + 100 * digits[2] + 10 * digits[3] + digits[0];
	}

	static int toi(String s) { return Integer.parseInt(s); }
	static String[] getLine() throws IOException { return br.readLine().split(" "); }
	static <T> void print(T s) { System.out.print(s); }
	static <T> void println(T s) { System.out.println(s); }
}
This post is licensed under CC BY 4.0 by the author.