|
| 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 final int[] dvx = {0, 0, 0, 1, -1}; |
| 9 | + private static final int[] dvy = {0, 1, -1, 0, 0}; |
| 10 | + private static final int OFFSET = 50; |
| 11 | + private static final int MAX_COORD = 100; |
| 12 | + private static final int MAX_V = 7; |
| 13 | + private static final int V_OFFSET = 7; |
| 14 | + private static int targetX, targetY; |
| 15 | + private static int N; |
| 16 | + private static boolean[][] isObstacle; |
| 17 | + private static boolean[][][][] visited; |
| 18 | +
|
| 19 | + public static void main(String[] args) throws IOException { |
| 20 | + init(); |
| 21 | + int result = BFS(); |
| 22 | +
|
| 23 | + bw.write(String.valueOf(result)); |
| 24 | + bw.flush(); |
| 25 | + bw.close(); |
| 26 | + br.close(); |
| 27 | + } |
| 28 | +
|
| 29 | + private static void init() throws IOException { |
| 30 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 31 | + targetX = Integer.parseInt(st.nextToken()); |
| 32 | + targetY = Integer.parseInt(st.nextToken()); |
| 33 | + N = Integer.parseInt(st.nextToken()); |
| 34 | +
|
| 35 | + isObstacle = new boolean[MAX_COORD + 1][MAX_COORD + 1]; |
| 36 | + visited = new boolean[MAX_COORD + 1][MAX_COORD + 1][16][16]; |
| 37 | +
|
| 38 | + for (int i = 0; i < N; i++) { |
| 39 | + st = new StringTokenizer(br.readLine()); |
| 40 | + int ox = Integer.parseInt(st.nextToken()); |
| 41 | + int oy = Integer.parseInt(st.nextToken()); |
| 42 | + |
| 43 | + if (!OOB(ox + OFFSET, oy + OFFSET)) { |
| 44 | + isObstacle[ox + OFFSET][oy + OFFSET] = true; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | +
|
| 49 | + private static int BFS() { |
| 50 | + Queue<int[]> q = new ArrayDeque<>(); |
| 51 | +
|
| 52 | + int startX = 0 + OFFSET; |
| 53 | + int startY = 0 + OFFSET; |
| 54 | + int startVx = 0 + V_OFFSET; |
| 55 | + int startVy = 0 + V_OFFSET; |
| 56 | +
|
| 57 | + q.add(new int[] {startX, startY, startVx, startVy, 0}); |
| 58 | + visited[startX][startY][startVx][startVy] = true; |
| 59 | +
|
| 60 | + while (!q.isEmpty()) { |
| 61 | + int[] cur = q.poll(); |
| 62 | + int cx = cur[0]; |
| 63 | + int cy = cur[1]; |
| 64 | + int cvx = cur[2]; |
| 65 | + int cvy = cur[3]; |
| 66 | + int time = cur[4]; |
| 67 | + |
| 68 | + if (cx - OFFSET == targetX && cy - OFFSET == targetY) { |
| 69 | + return time; |
| 70 | + } |
| 71 | +
|
| 72 | + int realVx = cvx - V_OFFSET; |
| 73 | + int realVy = cvy - V_OFFSET; |
| 74 | + |
| 75 | + for (int i = 0; i < 5; i++) { |
| 76 | + int nRealVx = realVx + dvx[i]; |
| 77 | + int nRealVy = realVy + dvy[i]; |
| 78 | + |
| 79 | + if (Math.abs(nRealVx) > MAX_V || Math.abs(nRealVy) > MAX_V) continue; |
| 80 | +
|
| 81 | + int nx = cx + nRealVx; |
| 82 | + int ny = cy + nRealVy; |
| 83 | + int nvx = nRealVx + V_OFFSET; |
| 84 | + int nvy = nRealVy + V_OFFSET; |
| 85 | + |
| 86 | + if (OOB(nx, ny) || visited[nx][ny][nvx][nvy] || hasCollision(cx, cy, nx, ny)) continue; |
| 87 | + visited[nx][ny][nvx][nvy] = true; |
| 88 | + q.add(new int[] {nx, ny, nvx, nvy, time + 1}); |
| 89 | + } |
| 90 | + } |
| 91 | +
|
| 92 | + return -1; |
| 93 | + } |
| 94 | +
|
| 95 | + private static boolean hasCollision(int x1, int y1, int x2, int y2) { |
| 96 | + int dx = x2 - x1; |
| 97 | + int dy = y2 - y1; |
| 98 | + |
| 99 | + if (dx == 0 && dy == 0) { |
| 100 | + return isObstacle[x2][y2]; |
| 101 | + } |
| 102 | + |
| 103 | + int steps = gcd(Math.abs(dx), Math.abs(dy)); |
| 104 | +
|
| 105 | + int stepX = dx / steps; |
| 106 | + int stepY = dy / steps; |
| 107 | +
|
| 108 | + for (int i = 1; i <= steps; i++) { |
| 109 | + int checkX = x1 + stepX * i; |
| 110 | + int checkY = y1 + stepY * i; |
| 111 | +
|
| 112 | + if (isObstacle[checkX][checkY]) return true; |
| 113 | + } |
| 114 | +
|
| 115 | + return false; |
| 116 | + } |
| 117 | +
|
| 118 | + private static int gcd(int a, int b) { |
| 119 | + while (b != 0) { |
| 120 | + int r = a % b; |
| 121 | + a = b; |
| 122 | + b = r; |
| 123 | + } |
| 124 | + return a; |
| 125 | + } |
| 126 | +
|
| 127 | + private static boolean OOB(int x, int y) { |
| 128 | + return x < 0 || x > MAX_COORD || y < 0 || y > MAX_COORD; |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
0 commit comments