diff --git "a/Ukj0ng/202510/05 BOJ P5 \354\210\250\353\260\224\352\274\255\354\247\210 5.md" "b/Ukj0ng/202510/05 BOJ P5 \354\210\250\353\260\224\352\274\255\354\247\210 5.md" new file mode 100644 index 00000000..ffb5f5ce --- /dev/null +++ "b/Ukj0ng/202510/05 BOJ P5 \354\210\250\353\260\224\352\274\255\354\247\210 5.md" @@ -0,0 +1,69 @@ +``` +import java.io.*; +import java.util.ArrayDeque; +import java.util.Queue; +import java.util.StringTokenizer; + +public class Main { + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static final int[] dx = {1, -1, 2}; + private static boolean[][] visited; + private static int N, K; + + public static void main(String[] args) throws IOException { + init(); + + int answer = BFS(); + + bw.write(answer + "\n"); + bw.flush(); + bw.close(); + br.close(); + } + + private static void init() throws IOException { + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + K = Integer.parseInt(st.nextToken()); + + visited = new boolean[500001][2]; + } + + private static int BFS() { + Queue q = new ArrayDeque<>(); + int result = -1; + visited[N][0] = true; + q.add(new int[] {N, K, 0}); + + while (!q.isEmpty()) { + int[] current = q.poll(); + + if (visited[current[1]][current[2]%2]) { + result = current[2]; + break; + } + int nt = current[2] + 1; + int nk = current[1] + nt; + if (OOB(nk)) continue; + + for (int i = 0; i < 3; i++) { + int nx = current[0]; + + if (i == 2) nx *= dx[i]; + else nx += dx[i]; + + if (OOB(nx) || visited[nx][nt%2]) continue; + visited[nx][nt%2] = true; + q.add(new int[]{nx, nk, nt}); + } + } + + return result; + } + + private static boolean OOB(int nx) { + return nx < 0 || nx > 500000; + } +} +```