# 백준 1697번 숨바꼭질

## Solution

• 변수명 겹치는 것 없는지 항상 주의하자.
import sys
from collections import deque

n, k = map(int, input().split())

# 수빈이와 동생이 있는 곳보다 더 큰 곳으로 갈 수도 있음
size = 100000

visit = [False] * (size + 1)
dist = [0] * (size + 1)
move = [-1, 1, 2]

def bfs(v):
queue = deque([v])
visit[v] = True

while queue:
c = queue.popleft()
for x in move:
if x == 2:
nv = c * x
else:
nv = c + x
# 0도 가능
if 0 <= nv <= size and not visit[nv]:
visit[nv] = True
dist[nv] = dist[c] + 1
queue.append(nv)

bfs(n)

print(dist[k])
• 모범 풀이
import sys
from collections import deque
N, K = list(map(int, si().split()))
X = 100000

dist = [-1] * (X + 1)

def bfs(x):
queue = deque()
queue.append(x)
dist[x] = 0
def move(y, d):
if y < 0 or y > X or dist[y] != -1:
return
dist[y] = d
queue.append(y)

while queue:
x = queue.popleft()
move(x - 1, dist[x] + 1)
move(x + 1, dist[x] + 1)
move(x * 2, dist[x] + 1)

bfs(N)
• java 풀이
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

public class Main {
static StringBuilder sb = new StringBuilder();

static int N, K;
static int[] dist;
static boolean[] visit;

static void input() {
N = scan.nextInt();
K = scan.nextInt();
visit = new boolean[100005];
dist = new int[100005];
}

// 숨바꼭질 시작~
static void bfs() {
Queue<Integer> Q = new LinkedList<>();
visit[N] = true;
dist[N] = 0;

// BFS 과정 시작
while (!Q.isEmpty()) {
int x = Q.poll();
if (x - 1 >= 0 && !visit[x - 1]) {
visit[x - 1] = true;
dist[x - 1] = dist[x] + 1;
}
if (x + 1 <= 100000 && !visit[x + 1]) {
visit[x + 1] = true;
dist[x + 1] = dist[x] + 1;
}
if (x * 2 <= 100000 && !visit[x * 2]) {
visit[x * 2] = true;
dist[x * 2] = dist[x] + 1;
}
}
}

static void pro() {
bfs();
System.out.println(dist[K]);
}

public static void main(String[] args) {
input();
pro();
}

static class FastReader {
StringTokenizer st;

}

public FastReader(String s) throws FileNotFoundException {
}

String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}

int nextInt() {
return Integer.parseInt(next());
}

long nextLong() {
return Long.parseLong(next());
}

double nextDouble() {
return Double.parseDouble(next());
}

String nextLine() {
String str = "";
try {
}