๋ฌธ์
ํด๋น ํฌ์คํ ์ ๋ฐฑ์ค์ 9019๋ฒ DSLR ์ ์ ๊ทผ๊ณผ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ค๋ช ํ ๊ธ ์ ๋๋ค.
์ ๋ต ์์ค ์ฝ๋๋ฅผ ํ์ธํ์๋ ค๋ฉด solve url ์์ ํ์ธ ๊ฐ๋ฅํฉ๋๋ค.
9019๋ฒ: DSLR
๋ค ๊ฐ์ ๋ช ๋ น์ด D, S, L, R ์ ์ด์ฉํ๋ ๊ฐ๋จํ ๊ณ์ฐ๊ธฐ๊ฐ ์๋ค. ์ด ๊ณ์ฐ๊ธฐ์๋ ๋ ์ง์คํฐ๊ฐ ํ๋ ์๋๋ฐ, ์ด ๋ ์ง์คํฐ์๋ 0 ์ด์ 10,000 ๋ฏธ๋ง์ ์ญ์ง์๋ฅผ ์ ์ฅํ ์ ์๋ค. ๊ฐ ๋ช ๋ น์ด๋ ์ด ๋ ์ง์คํฐ์
www.acmicpc.net
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์ด๋ค ๋ฐฉ์์ผ๋ก ์ ๊ทผํด์ผ ํ๋์ง๋ฅผ ๋จผ์ ์๊ฐํด๋ณด์.
๋ฌธ์ ์ ๊ทผ
์ด๋ฒ ๋ฌธ์ ๋ BFS ๋ก ํด๊ฒฐํ ์ ์๋ ์ฌ๋ฏธ์๋ ๋ฌธ์ ์ด๋ค.
์ด ๋ฌธ์ ๋ฅผ ํ๊ธฐ ์ํด์ ๊ฑฐ์ 3์๊ฐ์ ์์๋๋ฐ.. ๊ณ์ํด์ ์๊ฐ ์ด๊ณผ ๋ฌ๋ ๋ฌธ์ ๋ค ใ ใ
ํด๊ฒฐ๋ฒ
1 ~ 1๋ง ๊น์ง ์ซ์๊ฐ ์กด์ฌํ ๋, 4๊ฐ์ง ์ฐ์ฐ์ ๋ชจ๋ ์ํํด๋ณด๋ฉด์ ์ต์ ํ์๋ฅผ ์ธ๋ฉด ๋๋ค.
์ฒ์์๋ L๊ณผ R ์ฐ์ฐ์์ Shift ์ฐ์ฐ์ ๊ตฌํํ๊ธฐ ์ํด์ String ํด๋์ค์ Arrays.stream() ์ ํจ๊ป ์ด์ฉํ์๋๋ฐ, ์๊ฐ ์ด๊ณผ๊ฐ ๋ฌ๊ณ ๊ฒฐ๊ตญ ๋ธ๋ก๊ทธ์ ๋์์ ๋ฐ์์ shift ์ฐ์ฐ์ ์ํ์ ์ผ๋ก ํด๊ฒฐํ๋ ๋ฌธ์ ์ด๋ค.
์ ๋ต ์ฝ๋
public class Main {
private static int[] depth;
private static int a;
private static int b;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Integer.parseInt(br.readLine());
while(t-- > 0) {
String[] ab = br.readLine().split(" ");
a = Integer.parseInt(ab[0]);
b = Integer.parseInt(ab[1]);
depth = new int[10000];
bw.write(getCommand() + "\n");
}
bw.flush();
bw.close();
}
private static String getCommand() {
Queue<Pair> queue = new LinkedList<>();
queue.add(new Pair("", a));
depth[a] = 1;
while(!queue.isEmpty()) {
Pair front = queue.remove();
if(front.number == b) {
return front.command;
}
int l = l(front.number);
if(depth[l] == 0) {
depth[l] = depth[front.number] + 1;
queue.add(new Pair(front.command+"L", l));
}
int r = r(front.number);
if(depth[r] == 0) {
depth[r] = depth[front.number] + 1;
queue.add(new Pair(front.command+"R", r));
}
int d = d(front.number);
if(depth[d] == 0) {
depth[d] = depth[front.number] + 1;
queue.add(new Pair(front.command+"D", d));
}
int s = s(front.number);
if(depth[s] == 0) {
depth[s] = depth[front.number] + 1;
queue.add(new Pair(front.command+"S", s));
}
}
return null;
}
private static int d(int n) {
n *= 2;
if(9999 < n) n %= 10_000;
return n;
}
private static int s(int n) {
if(n == 0) n = 9999;
else n -= 1;
return n;
}
private static int l(int n) {
return n % 1000 * 10 + n / 1000;
}
private static int r(int n) {
return n % 10 * 1000 + n / 10;
}
}
class Pair {
String command;
int number;
public Pair(String command, int number) {
this.command = command;
this.number = number;
}
}
์ด๋ค ๊ฐ์ด๋ 1 ~ 1๋ง ์ฌ์ด๋ก๋ ์ฐ์ฐ์ด ๊ฐ๋ฅํด์ return ์ null ๋ก ํด์ค๋ ํฌ๊ฒ ๋ฌธ์ ๊ฐ ์๋ค.
๋๊ธ