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