BOJ_G4_9019
๐ [G4_9019] DSLR
- ์๊ฐ์ด๊ณผ..
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
// ์ซ์, ํจ์์ด๋ฆ(D,S,L,R)
static class ch {
int num;
String ch;
public ch(int num, String ch) {
this.num = num;
this.ch = ch;
}
}
static int A, B;
static Queue<ch> queue;
static String res;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st;
// **** input start ****
int T = Integer.parseInt(br.readLine());
for (int tc = 0; tc < T; tc++) {
st = new StringTokenizer(br.readLine(), " ");
A = Integer.parseInt(st.nextToken());
B = Integer.parseInt(st.nextToken());
// **** input end ****
String[] DSLR = { "D", "S", "L", "R" };
int min = Integer.MAX_VALUE;
String output = "";
// 4 ์์๋๋ก ๋ฃ์ด๋ณด๊ธฐ
for (int i = 0; i < 4; i++) {
res = "";
queue = new LinkedList<ch>();
queue.add(new ch(A, DSLR[i]));
BFS();
min = Math.min(min, res.length());
if(min == res.length()) {
output = res;
}
}
sb.append(output).append("\n");
}
System.out.println(sb);
} // main end
// BFS
static void BFS() {
while (!queue.isEmpty()) {
ch c = queue.poll();
int temp = c.num;
String str = c.ch;
// B๋ก ๋๋ฉด
if (temp == B) {
str = str.substring(1, str.length());
res += str;
break;
}
// DSLR ์์๋๋ก
// D
queue.add(new ch(D(temp), str + "D"));
// S
queue.add(new ch(S(temp), str + "S"));
// L
queue.add(new ch(L(temp), str + "L"));
// R
queue.add(new ch(R(temp), str + "R"));
}
}
// D = 2*N
static int D(int num) {
num *= 2;
// ๋ง์ฝ ๊ณฑํ๊ธฐ 2ํ ๊ฐ์ด 9999๋ณด๋ค ํด๊ฒฝ์ฐ mod 10000 ํด์ฃผ๊ธฐ
if (num > 9999)
num %= 10000;
return num;
}
// S = N-1
static int S(int num) {
num -= 1;
// 1 ๋บ ๊ฐ์ด 0์ผ ๊ฒฝ์ฐ 9999 ์ ์ฅ
if (num == 0)
num = 9999;
return num;
}
// L = left
static int L(int num) {
int temp = num;
int thousand = (temp / 1000);
temp %= 1000;
int hundred = (temp / 100);
temp %= 100;
int ten = (temp / 10);
temp %= 10;
int one = temp;
num = hundred * 1000 + ten * 100 + one * 10 + thousand;
return num;
}
// R = right
static int R(int num) {
int temp = num;
int thousand = (temp / 1000);
temp %= 1000;
int hundred = (temp / 100);
temp %= 100;
int ten = (temp / 10);
temp %= 10;
int one = temp;
num = one * 1000 + thousand * 100 + hundred * 10 + ten;
return num;
}
} // class end
- ์ฐพ์ ๋ฐฉ๋ฒ..
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st = null;
// **** input start ****
int T = Integer.parseInt(br.readLine());
for (int tc = 0; tc < T; tc++) {
st = new StringTokenizer(br.readLine(), " ");
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
// **** input end ****
boolean[] v = new boolean[10000];
String[] command = new String[10000];
Queue<Integer> queue = new LinkedList<Integer>();
queue.add(A);
// ๋ฐฉ๋ฌธ์ฒ๋ฆฌ
v[A] = true;
Arrays.fill(command, "");
while (!queue.isEmpty() && !v[B]) {
int temp = queue.poll();
// DSLR
int D = (temp * 2) % 10000;
int S = temp == 0 ? 9999 : temp - 1;
int L = (temp % 1000) * 10 + (temp / 1000);
int R = (temp % 10) * 1000 + (temp / 10);
if (!v[D]) {
// ๋ฐฉ๋ฌธ์ฒ๋ฆฌ
v[D] = true;
// ๋ฌธ์ ์ถ๊ฐ
command[D] = command[temp] + "D";
// queue ๋ํ๊ธฐ
queue.add(D);
}
if (!v[S]) {
// ๋ฐฉ๋ฌธ์ฒ๋ฆฌ
v[S] = true;
// ๋ฌธ์ ์ถ๊ฐ
command[S] = command[temp] + "S";
// queue ๋ํ๊ธฐ
queue.add(S);
}
if (!v[L]) {
// ๋ฐฉ๋ฌธ์ฒ๋ฆฌ
v[L] = true;
// ๋ฌธ์ ์ถ๊ฐ
command[L] = command[temp] + "L";
// queue ๋ํ๊ธฐ
queue.add(L);
}
if (!v[R]) {
// ๋ฐฉ๋ฌธ์ฒ๋ฆฌ
v[R] = true;
// ๋ฌธ์ ์ถ๊ฐ
command[R] = command[temp] + "R";
// queue ๋ํ๊ธฐ
queue.add(R);
}
}
sb.append(command[B]).append("\n");
}
System.out.println(sb);
} // main end
} // class end
๐ค ๋์ ์๊ฐ
์ฒ์์ BFS () + DSLR ์ ์ด์ฉํด์ ํ์๋ค.
๊ทธ๋ฌ๋ .. ์๊ฐ์ด๊ณผ..
๊ทธ๋์ ์ฐพ์๋ณด๋ ๋ฐฉ๋ฌธ์ฒ๋ฆฌ๊ฐ ์๊ฐ์ด๊ณผ์ ์์ธ์ด์๋ค.
๊ทธ๋ฆฌ๊ณ ๋๋ ์์ ๋ฌธ์๋ฅผ class๋ก ๋ฐ๋ก ๋ฌ์ ๋ ๋ค ๊ณ ๋ คํด์คฌ๋๋ฐ ๊ทธ๋ด ํ์ ์์ด ์๋ก๋ง ์ฒดํฌํ๊ณ ๋ฌธ์๋ ๋ฐฐ์ด์ ์ด์ฉํด ์ต๋ 10000์ด๋ 10000๊ฐ๋ฅผ ๊ฐ์ง ๋ฐฐ์ด์์ ๋ฌธ์๋ฅผ ์ ์ฅํด์ฃผ๋ฉด ๋๋ ๊ฒ์ด์๋ค.
์ด๊ฑด ์๊ฐ์ง๋ ๋ชปํ..
๊ทธ๋ฆฌ๊ณ ๋ฐฉ๋ฌธ์ฒ๋ฆฌ๋ฅผ ํตํด ํ์ธ.
๋ํ โLโ,โRโ์ ๊ตฌํ ๋ ์ด์ฐจํผ ๊ท์น์ ์ผ๋ก ์ด๋ํ๋ ๊ณฑํ๊ณ ๋๋๊ธฐ๋ง ํด์ฃผ๋ฉด ๋ ๊ฐ๋จํ๋ค.
๊ทธ๋๋ BFS๋ก ์ ๊ทผํ ๊ฒ์ด ์์ฃผ ๋ง์กฑ์ค.. ์ ๋ฐฉ๋ฌธ์ฒ๋ฆฌ๋ฅผ ๊น๋จน์์๊น..
์์ฉ๋ค..