📍 문제 설명
💡 접근
3가지의 이동 가능 위치에 따라서 이동시켜주면된다.
👩💻 코드
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int n, m, v, e, k, answer, cnt, second;
static int[] arr, dis = {-1, 1, 2};
static long[] dp;
static int[][] board;
static boolean[] visited;
static int[] dx = {-1,1,0,0};
static int[] dy = {0,0,-1,1};
static ArrayList<Integer> list;
static StringBuilder sb;
static Queue<Integer> q;
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
Main T = new Main();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = kb.nextInt();
k = kb.nextInt();
visited = new boolean[100001];
System.out.println(T.solution(second));
}
private int solution(int second) {
q = new LinkedList<>();
q.offer(n);
visited[n] = true;
while(!q.isEmpty()) {
int len = q.size();
for(int i = 0; i < len; i++) {
int cx = q.poll();
if (cx == k) return second;
for (int j = 0; j < dis.length; j++) {
int nx = 0;
if(j != 2) nx = cx + dis[j];
else nx = cx * dis[j];
if (nx < 0 || nx > 100000 || visited[nx]) continue;
visited[nx] = true;
q.offer(nx);
}
}
second++;
}
return 0;
}
private static class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
'}';
}
}
}