
๐ ๋ฌธ์ ๋ณด๊ธฐ - [๋ฐฑ์ค] DSLR
๋ค ๊ฐ์ ๋ช ๋ น์ด D, S, L, R ์ ์ด์ฉํ๋ ๊ฐ๋จํ ๊ณ์ฐ๊ธฐ๊ฐ ์๋ค. ์ด ๊ณ์ฐ๊ธฐ์๋ ๋ ์ง์คํฐ๊ฐ ํ๋ ์๋๋ฐ, ์ด ๋ ์ง์คํฐ์๋ 0 ์ด์ 10,000 ๋ฏธ๋ง์ ์ญ์ง์๋ฅผ ์ ์ฅํ ์ ์๋ค. ๊ฐ ๋ช ๋ น์ด๋ ์ด ๋ ์ง์คํฐ์ ์ ์ฅ๋ n์ ๋ค์๊ณผ ๊ฐ์ด ๋ณํํ๋ค. n์ ๋ค ์๋ฆฟ์๋ฅผ d1, d2, d3, d4๋ผ๊ณ ํ์(์ฆ n = ((d1ย ร 10 + d2) ร 10 + d3) ร 10 + d4๋ผ๊ณ ํ์)
์์์ ์ธ๊ธํ ๊ฒ์ฒ๋ผ, L ๊ณผ R ๋ช ๋ น์ด๋ ์ญ์ง ์๋ฆฟ์๋ฅผ ๊ฐ์ ํ๊ณ ์ฐ์ฐ์ ์ํํ๋ค. ์๋ฅผ ๋ค์ด์ n = 1234 ๋ผ๋ฉด ์ฌ๊ธฐ์ L ์ ์ ์ฉํ๋ฉด 2341 ์ด ๋๊ณ R ์ ์ ์ฉํ๋ฉด 4123 ์ด ๋๋ค.
์ฌ๋ฌ๋ถ์ด ์์ฑํ ํ๋ก๊ทธ๋จ์ ์ฃผ์ด์ง ์๋ก ๋ค๋ฅธ ๋ ์ ์ A์ B(A โ B)์ ๋ํ์ฌ A๋ฅผ B๋ก ๋ฐ๊พธ๋ ์ต์ํ์ ๋ช ๋ น์ด๋ฅผ ์์ฑํ๋ ํ๋ก๊ทธ๋จ์ด๋ค. ์๋ฅผ ๋ค์ด์ A = 1234, B = 3412 ๋ผ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ๋ ๊ฐ์ ๋ช ๋ น์ด๋ฅผ ์ ์ฉํ๋ฉด A๋ฅผ B๋ก ๋ณํํ ์ ์๋ค.
1234 โLย 2341 โLย 3412
1234 โRย 4123 โRย 3412
๋ฐ๋ผ์ ์ฌ๋ฌ๋ถ์ ํ๋ก๊ทธ๋จ์ ์ด ๊ฒฝ์ฐ์ LL ์ด๋ RR ์ ์ถ๋ ฅํด์ผ ํ๋ค.
n์ ์๋ฆฟ์๋ก 0 ์ด ํฌํจ๋ ๊ฒฝ์ฐ์ ์ฃผ์ํด์ผ ํ๋ค. ์๋ฅผ ๋ค์ด์ 1000 ์ L ์ ์ ์ฉํ๋ฉด 0001 ์ด ๋๋ฏ๋ก ๊ฒฐ๊ณผ๋ 1 ์ด ๋๋ค. ๊ทธ๋ฌ๋ R ์ ์ ์ฉํ๋ฉด 0100 ์ด ๋๋ฏ๋ก ๊ฒฐ๊ณผ๋ 100 ์ด ๋๋ค.
ํ๋ก๊ทธ๋จ ์ ๋ ฅ์ T ๊ฐ์ ํ ์คํธ ์ผ์ด์ค๋ก ๊ตฌ์ฑ๋๋ค. ํ ์คํธ ์ผ์ด์ค ๊ฐ์ T ๋ ์ ๋ ฅ์ ์ฒซ ์ค์ ์ฃผ์ด์ง๋ค. ๊ฐ ํ ์คํธ ์ผ์ด์ค๋ก๋ ๋ ๊ฐ์ ์ ์ A์ B(A โ B)๊ฐ ๊ณต๋ฐฑ์ผ๋ก ๋ถ๋ฆฌ๋์ด ์ฐจ๋ก๋ก ์ฃผ์ด์ง๋๋ฐ A๋ ๋ ์ง์คํฐ์ ์ด๊ธฐ ๊ฐ์ ๋ํ๋ด๊ณ B๋ ์ต์ข ๊ฐ์ ๋ํ๋ธ๋ค. A ์ B๋ ๋ชจ๋ 0 ์ด์ 10,000 ๋ฏธ๋ง์ด๋ค.
A์์ B๋ก ๋ณํํ๊ธฐ ์ํด ํ์ํ ์ต์ํ์ ๋ช ๋ น์ด ๋์ด์ ์ถ๋ ฅํ๋ค. ๊ฐ๋ฅํ ๋ช ๋ น์ด ๋์ด์ด ์ฌ๋ฌ๊ฐ์ง๋ฉด, ์๋ฌด๊ฑฐ๋ ์ถ๋ ฅํ๋ค.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static boolean[] visited;
static String[] operation;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
while(0 < T--) {
StringTokenizer st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int target = Integer.parseInt(st.nextToken());
System.out.println(bfs(start, target));
}
}
public static String bfs(int start, int target) {
visited = new boolean[10000];
operation = new String[10000];
Queue<Integer> queue = new LinkedList<>();
operation[start] = "";
queue.offer(start);
visited[start] = true;
while(!queue.isEmpty()) {
int now = queue.poll();
if(now == target) return operation[now];
// D
int d = (now * 2) > 9999 ? (now * 2) % 10000 : (now * 2);
if(!visited[d]) {
setting(queue, now, d, "D");
}
// S
int s = now == 0 ? 9999 : now - 1;
if(!visited[s]) {
setting(queue, now, s, "S");
}
// L
int l = (now % 1000) * 10 + (now / 1000);
if(!visited[l]) {
setting(queue, now, l, "L");
}
// R
int r = (now % 10) * 1000 + (now / 10);
if(!visited[r]) {
setting(queue, now, r, "R");
}
}
return "";
}
public static void setting(Queue<Integer> queue, int now, int next, String add) {
visited[next] = true;
operation[next] = operation[now] + add;
queue.offer(next);
}
}