문제

일단 생각하기!
- 수빈이가 이동할 수 있는 좌표는 현재 좌표가
X라면 2*X, X-1, X+1이다. 즉, 한 좌표에서 3개의 좌표로 이동할 수 있다.
- Queue에 int형 배열로 현재 위치와 초를 넣은 뒤
2*X, X-1, X+1을 각각 넣어준다. 이때, 값이 K와 같다면 반복문을 종료한다.
- 이때, 넣었던 값을 체크하지 않는다면 메모리 초과가 나기 때문에 boolean 타입 방문 배열을 만들어 방문 체크를 하며 같은 값은 탐색하지 않도록 한다.
풀이
package BJ;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.StringTokenizer;
public class BJ_1697_숨바꼭질_김유나 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int sec = 0;
boolean [] isVisited = new boolean[100001];
Queue<int []> queue = new ArrayDeque<>();
queue.offer(new int[] {n, 0});
isVisited[n] = true;
while (!queue.isEmpty()) {
int current[] = queue.poll();
int loc = current[0];
int depth = current[1];
if (loc == k) {
sec = depth;
break;
}
depth++;
isVisited[loc] = true;
if (loc * 2 <= 100000 && !isVisited[loc * 2]) {
queue.offer(new int[] {loc * 2, depth});
isVisited[loc * 2] = true;
}
if (loc + 1 <= 100000 && !isVisited[loc + 1]) {
queue.offer(new int[] {loc + 1, depth});
isVisited[loc + 1] = true;
}
if (loc - 1 >= 0 && !isVisited[loc - 1]) {
queue.offer(new int[] {loc - 1, depth});
isVisited[loc - 1] = true;
}
}
System.out.println(sec);
}
}