Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions Ukj0ng/202510/05 BOJ P5 숨바꼭질 5.md
Original file line number Diff line number Diff line change
@@ -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<int[]> 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;
}
}
```