|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.ArrayDeque; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.Queue; |
| 6 | +import java.util.StringTokenizer; |
| 7 | +
|
| 8 | +public class Main { |
| 9 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 11 | + private static final int[] dx = {1, -1, 2}; |
| 12 | + private static int[] visited; |
| 13 | + private static int N, K; |
| 14 | +
|
| 15 | + public static void main(String[] args) throws IOException { |
| 16 | + init(); |
| 17 | + int answer = BFS(N); |
| 18 | +
|
| 19 | + bw.write(visited[K] + "\n"); |
| 20 | + bw.write(answer + "\n"); |
| 21 | + bw.flush(); |
| 22 | + bw.close(); |
| 23 | + br.close(); |
| 24 | + } |
| 25 | +
|
| 26 | + private static void init() throws IOException { |
| 27 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 28 | + N = Integer.parseInt(st.nextToken()); |
| 29 | + K = Integer.parseInt(st.nextToken()); |
| 30 | +
|
| 31 | + visited = new int[100001]; |
| 32 | + Arrays.fill(visited, Integer.MAX_VALUE); |
| 33 | + } |
| 34 | +
|
| 35 | + private static int BFS(int start) { |
| 36 | + Queue<int[]> q = new ArrayDeque<>(); |
| 37 | + int result = 0; |
| 38 | + visited[start] = 0; |
| 39 | + q.add(new int[] {start, visited[start]}); |
| 40 | +
|
| 41 | + while (!q.isEmpty()) { |
| 42 | + int[] current = q.poll(); |
| 43 | +
|
| 44 | + if (current[1] > visited[current[0]]) continue; |
| 45 | + if (current[0] == K && visited[current[0]] == current[1]) { |
| 46 | + result++; |
| 47 | + } |
| 48 | +
|
| 49 | + for (int i = 0; i < 3; i++) { |
| 50 | + int nx = 0; |
| 51 | + if (i == 2) { |
| 52 | + nx = current[0]*dx[i]; |
| 53 | + } else { |
| 54 | + nx = current[0]+dx[i]; |
| 55 | + } |
| 56 | + if (nx < 0 || nx > 100000) continue; |
| 57 | + if (visited[nx] < current[1]+1) continue; |
| 58 | + visited[nx] = current[1]+1; |
| 59 | + q.add(new int[] {nx, visited[nx]}); |
| 60 | + } |
| 61 | + } |
| 62 | +
|
| 63 | + return result; |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
0 commit comments