|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.IOException; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.util.PriorityQueue; |
| 5 | +import java.util.StringTokenizer; |
| 6 | + |
| 7 | +/** |
| 8 | + * 25% 틀림 -> 1000초 이상 아니고, 초과로 |
| 9 | + */ |
| 10 | +public class JM_19237 { |
| 11 | + static class Smell { |
| 12 | + int sn; |
| 13 | + int k; |
| 14 | + |
| 15 | + public Smell(int sn, int k) { |
| 16 | + this.sn = sn; |
| 17 | + this.k = k; |
| 18 | + } |
| 19 | + |
| 20 | + public void set(int sn, int k) { |
| 21 | + this.sn = sn; |
| 22 | + this.k = k; |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public String toString() { |
| 27 | + return "(" + sn + ", " + k + ')'; |
| 28 | + } |
| 29 | + } |
| 30 | + static class Shark implements Comparable<Shark> { |
| 31 | + int sn; |
| 32 | + int y, x; |
| 33 | + int d; |
| 34 | + |
| 35 | + public Shark(int sn, int y, int x, int d) { |
| 36 | + this.y = y; |
| 37 | + this.x = x; |
| 38 | + this.sn = sn; |
| 39 | + this.d = d; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public int compareTo(Shark o) { |
| 44 | + return this.sn - o.sn; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public String toString() { |
| 49 | + return "Shark{" + |
| 50 | + "sn=" + sn + |
| 51 | + ", y=" + y + |
| 52 | + ", x=" + x + |
| 53 | + ", d=" + d + |
| 54 | + '}'; |
| 55 | + } |
| 56 | + } |
| 57 | + static int N; // 격자 NxN |
| 58 | + static int M; // 상어 개수 |
| 59 | + static int K; // 냄새 사라지는 시간 |
| 60 | + static Smell[][] map; |
| 61 | + static int[][][] sharkPriorityDir; |
| 62 | + static PriorityQueue<Shark> sharks; |
| 63 | + static PriorityQueue<Shark> nextShanks; |
| 64 | + static final int MAX_TIME = 1000; |
| 65 | + static final int[][] DIR = {{0, 0}, {-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // 상,하,좌,우 |
| 66 | + |
| 67 | + private static boolean inRange(int y, int x) { |
| 68 | + return 0 <= y && y < N && 0 <= x && x < N; |
| 69 | + } |
| 70 | + |
| 71 | + private static void reduceKAll() { |
| 72 | + for (int i = 0; i < N; i++) { |
| 73 | + for (int j = 0; j < N; j++) { |
| 74 | + if(map[i][j].sn == 0) continue; |
| 75 | + map[i][j].k--; |
| 76 | + if(map[i][j].k == 0) map[i][j].sn = 0; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private static void move() { |
| 82 | + while (!nextShanks.isEmpty()) { |
| 83 | + Shark shank = nextShanks.poll(); |
| 84 | + if(map[shank.y][shank.x].sn == 0 || map[shank.y][shank.x].sn == shank.sn) { |
| 85 | + map[shank.y][shank.x].set(shank.sn, K + 1); // K + 1로 초기화 필요 |
| 86 | + sharks.add(shank); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private static void addNextShark() { |
| 92 | + while (!sharks.isEmpty()) { |
| 93 | + Shark curr = sharks.poll(); |
| 94 | + Shark prev = null; // 가능한 이동 경로가 없을 경우, 냄새가 있는 칸 |
| 95 | + Shark next = null; |
| 96 | + |
| 97 | + for(int nd : sharkPriorityDir[curr.sn][curr.d]) { |
| 98 | + if(nd == 0) continue; |
| 99 | + int ny = curr.y + DIR[nd][0]; |
| 100 | + int nx = curr.x + DIR[nd][1]; |
| 101 | + if(!inRange(ny,nx)) continue; |
| 102 | + |
| 103 | + if(next == null && map[ny][nx].sn == 0) { |
| 104 | + next = new Shark(curr.sn, ny, nx, nd); |
| 105 | + } |
| 106 | + if(prev == null && map[ny][nx].sn == curr.sn) { |
| 107 | + prev = new Shark(curr.sn, ny, nx, nd); |
| 108 | + } |
| 109 | + } |
| 110 | + if(next == null) nextShanks.add(prev); |
| 111 | + else nextShanks.add(next); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private static int solve() { |
| 116 | + int time = 0; |
| 117 | + while (true) { |
| 118 | + addNextShark(); // 1. 우선순위 방향 결정 |
| 119 | + move(); // 2. 이동 |
| 120 | + reduceKAll(); // 3. 현재 k 시간 감소시키기 |
| 121 | + |
| 122 | + time++; |
| 123 | + if(time > MAX_TIME) return -1; |
| 124 | + if(sharks.size() == 1) break; |
| 125 | + } |
| 126 | + return time; |
| 127 | + } |
| 128 | + |
| 129 | + public static void init(int[][] tmpMap, int[] startDir) { |
| 130 | + for (int i = 0; i < N; i++) { |
| 131 | + for (int j = 0; j < N; j++) { |
| 132 | + map[i][j] = new Smell(0, 0); |
| 133 | + if(tmpMap[i][j] != 0) { |
| 134 | + int n = tmpMap[i][j]; |
| 135 | + map[i][j].set(n, K); |
| 136 | + sharks.add(new Shark(tmpMap[i][j], i, j, startDir[n])); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public static void main(String[] args) throws IOException { |
| 143 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 144 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 145 | + N = Integer.parseInt(st.nextToken()); |
| 146 | + M = Integer.parseInt(st.nextToken()); |
| 147 | + K = Integer.parseInt(st.nextToken()); |
| 148 | + map = new Smell[N][N]; |
| 149 | + sharks = new PriorityQueue<>(); |
| 150 | + nextShanks = new PriorityQueue<>(); |
| 151 | + |
| 152 | + int[][] tmpMap = new int[N][N]; |
| 153 | + for (int i = 0; i < N; i++) { |
| 154 | + st = new StringTokenizer(br.readLine()); |
| 155 | + for (int j = 0; j < N; j++) { |
| 156 | + tmpMap[i][j] = Integer.parseInt(st.nextToken()); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + int[] startDir = new int[M + 1]; |
| 161 | + st = new StringTokenizer(br.readLine()); |
| 162 | + for (int i = 1; i <= M; i++) { |
| 163 | + startDir[i] = Integer.parseInt(st.nextToken()); |
| 164 | + } |
| 165 | + |
| 166 | + sharkPriorityDir = new int[M + 1][5][5]; |
| 167 | + for (int m = 1; m <= M; m++) { |
| 168 | + for (int i = 1; i <= 4; i++) { |
| 169 | + st = new StringTokenizer(br.readLine()); |
| 170 | + for (int j = 1; j <= 4; j++) { |
| 171 | + sharkPriorityDir[m][i][j] = Integer.parseInt(st.nextToken()); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + init(tmpMap, startDir); |
| 177 | + System.out.println(solve()); |
| 178 | + } |
| 179 | +} |
0 commit comments