|
| 1 | +import java.util.ArrayDeque; |
| 2 | +import java.util.Deque; |
| 3 | + |
| 4 | +public class JW_19237 { |
| 5 | + |
| 6 | + // μμ΄ κ°μ²΄ |
| 7 | + static class Shark { |
| 8 | + int num, idx, dir; // μμ΄μ λ²νΈ, μΈλ±μ€, λ°©ν₯ |
| 9 | + int[][] priority; // ν΄λΉ μμ΄μ μμΉμ λ°λ₯Έ λ°©ν₯ μ°μ μμ |
| 10 | + |
| 11 | + Shark(int num, int idx, int dir, int[][] priority) { |
| 12 | + this.num = num; |
| 13 | + this.idx = idx; |
| 14 | + this.dir = dir; |
| 15 | + this.priority = priority; |
| 16 | + } |
| 17 | + |
| 18 | + // λ°©ν₯ μ°μ μμ λ°ν |
| 19 | + int[] getPriority() { |
| 20 | + return priority[dir]; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + static int n, m, k; |
| 25 | + static int[] scentBoard, remainBoard, move; // λμ, λ¨μ μκ°, λ°©ν₯ λ²‘ν° |
| 26 | + static boolean[] visited; |
| 27 | + static Deque<Shark> sharks = new ArrayDeque<>(); // μμ΄ ν |
| 28 | + static Deque<int[]> scentDeque = new ArrayDeque<>(); // λμ ν |
| 29 | + |
| 30 | + public static void main(String[] args) throws Exception { |
| 31 | + n = read(); |
| 32 | + m = read(); |
| 33 | + k = read(); |
| 34 | + move = new int[] { 0, -n, n, -1, 1 }; |
| 35 | + scentBoard = new int[n * n]; |
| 36 | + remainBoard = new int[n * n]; |
| 37 | + visited = new boolean[n * n]; |
| 38 | + |
| 39 | + // κΈ°λ³Έ μ 보 μ
λ ₯ |
| 40 | + init(); |
| 41 | + |
| 42 | + int time = 0; |
| 43 | + while (time <= 1000) { |
| 44 | + int size = sharks.size(); |
| 45 | + |
| 46 | + // 1λ² μμ΄λ§ λ¨μλ€λ©΄ μ’
λ£ |
| 47 | + if (size == 1) { |
| 48 | + System.out.println(time); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + visited = new boolean[n * n]; // λ°©λ¬Έ λ°°μ΄ μ΄κΈ°ν |
| 53 | + // μμ΄λ₯Ό μμ λ²νΈ μλλ‘ κΊΌλ΄μ νμ |
| 54 | + while (size-- > 0) { |
| 55 | + Shark shark = sharks.poll(); |
| 56 | + int num = shark.num, idx = shark.idx; |
| 57 | + int nextDir = getNextDir(shark); // λ€μ λ°©ν₯ κ²°μ |
| 58 | + int next = idx + move[nextDir]; // μ΄λ |
| 59 | + |
| 60 | + // μ΄λ―Έ λμ°©ν΄μλ μμ΄κ° μλ€λ©΄ |
| 61 | + // μ¦, μμ λ³΄λ€ μμ λ²νΈμ μμ΄κ° λμ°©ν΄μλ€λ©΄ μ«κ²¨λ¨ |
| 62 | + if (visited[next]) |
| 63 | + continue; |
| 64 | + |
| 65 | + visited[next] = true; |
| 66 | + shark.idx = next; |
| 67 | + shark.dir = nextDir; |
| 68 | + sharks.offer(shark); // μμ΄ νμ μ½μ
|
| 69 | + scentDeque.offer(new int[] { next, num }); // λμ νμ μ½μ
|
| 70 | + } |
| 71 | + removeScent(); // λμ μΉ΄μ΄νΈ κ°μ |
| 72 | + putScent(); // λμ λ°°μ΄μ μ μ© |
| 73 | + time++; |
| 74 | + } |
| 75 | + System.out.println(-1); |
| 76 | + } |
| 77 | + |
| 78 | + // κΈ°λ³Έ μ 보 μ
λ ₯ |
| 79 | + private static void init() throws Exception { |
| 80 | + int[] tempSharks = new int[m + 1]; |
| 81 | + for (int i = 0; i < n * n; i++) { |
| 82 | + int num = read(); |
| 83 | + if (num != 0) { |
| 84 | + tempSharks[num] = i; // μμ΄μ μΈλ±μ€ μ μ₯ |
| 85 | + scentBoard[i] = num; // λμ μ½μ
|
| 86 | + remainBoard[i] = k; // λ¨μ μκ° κΈ°λ‘ |
| 87 | + } |
| 88 | + } |
| 89 | + int[] tempDir = new int[m + 1]; |
| 90 | + for (int i = 1; i < m + 1; i++) |
| 91 | + tempDir[i] = read(); |
| 92 | + |
| 93 | + // μμ΄ κ°μ²΄ μμ± λ° νμ μ½μ
|
| 94 | + for (int i = 1; i < m + 1; i++) { |
| 95 | + int idx = tempSharks[i], dir = tempDir[i]; |
| 96 | + int[][] priority = new int[5][5]; // μ°μ μμ μ
λ ₯ |
| 97 | + for (int j = 1; j < 5; j++) |
| 98 | + for (int k = 1; k < 5; k++) |
| 99 | + priority[j][k] = read(); |
| 100 | + Shark shark = new Shark(i, idx, dir, priority); |
| 101 | + sharks.offer(shark); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // λ€μ λ°©ν₯ κ²°μ |
| 106 | + private static int getNextDir(Shark shark) { |
| 107 | + int[] priority = shark.getPriority(); // μ°μ μμμ λ°λΌ λ°©ν₯ κ²°μ |
| 108 | + int myScent = 0; |
| 109 | + for (int i = 1; i < 5; i++) { |
| 110 | + int dir = priority[i]; |
| 111 | + int next = shark.idx + move[dir]; |
| 112 | + if (isValid(shark.idx, next)) { |
| 113 | + if (scentBoard[next] == 0) |
| 114 | + return dir; |
| 115 | + else if (scentBoard[next] == shark.num && myScent == 0) |
| 116 | + myScent = dir; |
| 117 | + } |
| 118 | + } |
| 119 | + return myScent; |
| 120 | + } |
| 121 | + |
| 122 | + // λμ μΉ΄μ΄νΈ κ°μ |
| 123 | + private static void removeScent() { |
| 124 | + for (int i = 0; i < n * n; i++) |
| 125 | + if (scentBoard[i] != 0 && --remainBoard[i] == 0) |
| 126 | + scentBoard[i] = 0; |
| 127 | + } |
| 128 | + |
| 129 | + // λμ νμ μλ μ’νμ λμ μ½μ
|
| 130 | + private static void putScent() { |
| 131 | + while (!scentDeque.isEmpty()) { |
| 132 | + int[] scent = scentDeque.poll(); |
| 133 | + int idx = scent[0], num = scent[1]; |
| 134 | + scentBoard[idx] = num; |
| 135 | + remainBoard[idx] = k; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // μΈλ±μ€ μ ν¨μ± κ²μ¬ |
| 140 | + private static boolean isValid(int cur, int next) { |
| 141 | + // μ 체 μΈλ±μ€λ₯Ό λ²μ΄λλ κ²½μ° |
| 142 | + if (next < 0 || next >= n * n) |
| 143 | + return false; |
| 144 | + // μ’, μ°λ‘ μμ§μΌ λ, κ°μ νμ μλμ§ νμΈ |
| 145 | + int curRow = cur / n; |
| 146 | + int nextRow = next / n; |
| 147 | + if (Math.abs(cur - next) == 1 && curRow != nextRow) |
| 148 | + return false; |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + private static int read() throws Exception { |
| 153 | + int c, n = System.in.read() & 15; |
| 154 | + while ((c = System.in.read()) >= 48) |
| 155 | + n = (n << 3) + (n << 1) + (c & 15); |
| 156 | + if (c == 13) |
| 157 | + System.in.read(); |
| 158 | + return n; |
| 159 | + } |
| 160 | +} |
0 commit comments