|
| 1 | +package org.example._41week; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | +public class Bizard { |
| 9 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + |
| 11 | + // 1 2 3 4 |
| 12 | + // u d l r |
| 13 | + // 0 1 2 3 |
| 14 | + // l d r u |
| 15 | + private static final int[] dr = {0, 1, 0, -1}; |
| 16 | + private static final int[] dc = {-1, 0, 1, 0}; |
| 17 | + private static final int[] answers = new int[4]; |
| 18 | + private static final int EMPTY = 0; |
| 19 | + private static int N; |
| 20 | + private static int M; |
| 21 | + private static int[][] map; |
| 22 | + private static int[][] commands; |
| 23 | + |
| 24 | + private static int sharkRow; |
| 25 | + private static int sharkCol; |
| 26 | + |
| 27 | + private static boolean[][] visited; |
| 28 | + |
| 29 | + |
| 30 | + public static void main(String[] args) throws IOException { |
| 31 | +// makeNumber(); |
| 32 | +// expressVisited(); |
| 33 | + |
| 34 | + init(); |
| 35 | + |
| 36 | + for (int i = 0; i < M; i++) { |
| 37 | + blizzard(commands[i][0], commands[i][1]); |
| 38 | + |
| 39 | + while (boom()) { |
| 40 | + } |
| 41 | + |
| 42 | + transformBeads(); |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + System.out.println(answers[1] + 2 * answers[2] + 3 * answers[3]); |
| 47 | + } |
| 48 | + |
| 49 | + private static void expressVisited() { |
| 50 | + N = 7; |
| 51 | + int row = 4; |
| 52 | + int col = 4; |
| 53 | + visited = new boolean[N + 1][N + 1]; |
| 54 | + visited[row][col] = true; |
| 55 | + print(visited); |
| 56 | + |
| 57 | + for (int i = 0; i <= 8; i++) { |
| 58 | + for (int j = 1; j <= 2 * (i + 1); j++) { |
| 59 | + if (j <= (i + 1)) { |
| 60 | + row = row + dr[(2 * i) % 4]; |
| 61 | + col = col + dc[(2 * i) % 4]; |
| 62 | + } else { |
| 63 | + row = row + dr[(2 * i + 1) % 4]; |
| 64 | + col = col + dc[(2 * i + 1) % 4]; |
| 65 | + } |
| 66 | + |
| 67 | + if (isOut(row, col)) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + visited[row][col] = true; |
| 72 | + print(visited); |
| 73 | + } |
| 74 | + |
| 75 | + System.out.println(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private static void makeNumber() { |
| 80 | + for (int i = 0; i <= 8; i++) { |
| 81 | + for (int j = 1; j <= 2 * (i + 1); j++) { |
| 82 | + if (j <= (i + 1)) { |
| 83 | + System.out.print((2 * i) % 4 + " "); |
| 84 | + } else { |
| 85 | + System.out.print((2 * i + 1) % 4 + " "); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + System.out.println(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private static void print(boolean[][] visited) { |
| 94 | + for (int row = 1; row <= N; row++) { |
| 95 | + for (int col = 1; col <= N; col++) { |
| 96 | + System.out.print(visited[row][col] ? "V " : "0 "); |
| 97 | + } |
| 98 | + System.out.println(); |
| 99 | + } |
| 100 | + System.out.println(); |
| 101 | + } |
| 102 | + |
| 103 | + private static void blizzard(int dir, int size) { |
| 104 | + // before |
| 105 | + // 1 2 3 4 |
| 106 | + // u d l r |
| 107 | + |
| 108 | + // after |
| 109 | + // 0 1 2 3 |
| 110 | + // l d r u |
| 111 | + dir = adjust(dir); |
| 112 | + |
| 113 | + int row = sharkRow; |
| 114 | + int col = sharkCol; |
| 115 | + |
| 116 | + for (int i = 0; i < size; i++) { |
| 117 | + row = row + dr[dir]; |
| 118 | + col = col + dc[dir]; |
| 119 | + |
| 120 | + map[row][col] = EMPTY; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private static int adjust(int dir) { |
| 125 | + if (dir == 1) { |
| 126 | + dir = 3; |
| 127 | + } else if (dir == 2) { |
| 128 | + dir = 1; |
| 129 | + } else if (dir == 3) { |
| 130 | + dir = 0; |
| 131 | + } else { |
| 132 | + dir = 2; |
| 133 | + } |
| 134 | + return dir; |
| 135 | + } |
| 136 | + |
| 137 | + private static Deque<List<Integer>> pickUp() { |
| 138 | + int row = sharkRow; |
| 139 | + int col = sharkCol; |
| 140 | + |
| 141 | + Deque<List<Integer>> deque = new ArrayDeque<>(); |
| 142 | + |
| 143 | + // 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
| 144 | + // 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 |
| 145 | + for (int i = 0; ; i++) { |
| 146 | + for (int j = 1; j <= 2 * (i + 1); j++) { |
| 147 | + if (j <= (i + 1)) { |
| 148 | + row = row + dr[(2 * i) % 4]; |
| 149 | + col = col + dc[(2 * i) % 4]; |
| 150 | + } else { |
| 151 | + row = row + dr[(2 * i + 1) % 4]; |
| 152 | + col = col + dc[(2 * i + 1) % 4]; |
| 153 | + } |
| 154 | + |
| 155 | + if (isOut(row, col)) { |
| 156 | + return deque; |
| 157 | + } |
| 158 | + |
| 159 | + List<Integer> peek = deque.peekLast(); |
| 160 | + |
| 161 | + if (map[row][col] == EMPTY) { |
| 162 | + continue; |
| 163 | + } |
| 164 | + |
| 165 | + if (peek == null || peek.get(0) != map[row][col]) { |
| 166 | + List<Integer> list = new LinkedList<>(); |
| 167 | + list.add(map[row][col]); |
| 168 | + |
| 169 | + deque.add(list); |
| 170 | + } else { |
| 171 | + peek.add(map[row][col]); |
| 172 | + } |
| 173 | + |
| 174 | + map[row][col] = 0; |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private static Deque<Integer> pickUpForTransform() { |
| 180 | + int row = sharkRow; |
| 181 | + int col = sharkCol; |
| 182 | + |
| 183 | + Deque<List<Integer>> temp = pickUp(); |
| 184 | + Deque<Integer> breads = new ArrayDeque<>(); |
| 185 | + |
| 186 | + while (!temp.isEmpty()) { |
| 187 | + List<Integer> pop = temp.pop(); |
| 188 | + breads.addLast(pop.size()); |
| 189 | + breads.addLast(pop.get(0)); |
| 190 | + } |
| 191 | + |
| 192 | + return breads; |
| 193 | + } |
| 194 | + |
| 195 | + private static boolean isOut(int row, int col) { |
| 196 | + return row <= 0 || row > N || col <= 0 || col > N; |
| 197 | + } |
| 198 | + |
| 199 | + // 폭발 채워놓기 |
| 200 | + private static boolean boom() { |
| 201 | + int[][] newMap = new int[N + 1][N + 1]; |
| 202 | + int row = sharkRow; |
| 203 | + int col = sharkCol; |
| 204 | + |
| 205 | + boolean hasConsecutiveBeads = false; |
| 206 | + |
| 207 | + Deque<List<Integer>> breads = pickUp(); |
| 208 | + |
| 209 | + for (int i = 0; ; i++) { |
| 210 | + for (int j = 1; j <= 2 * (i + 1); j++) { |
| 211 | + int prevRow = row; |
| 212 | + int prevCol = col; |
| 213 | + if (j <= (i + 1)) { |
| 214 | + row = row + dr[(2 * i) % 4]; |
| 215 | + col = col + dc[(2 * i) % 4]; |
| 216 | + } else { |
| 217 | + row = row + dr[(2 * i + 1) % 4]; |
| 218 | + col = col + dc[(2 * i + 1) % 4]; |
| 219 | + } |
| 220 | + |
| 221 | + if (breads.isEmpty()) { |
| 222 | + map = newMap; |
| 223 | + |
| 224 | + return hasConsecutiveBeads; |
| 225 | + } |
| 226 | + |
| 227 | + List<Integer> peek = breads.peek(); |
| 228 | + if (peek.size() >= 4) { |
| 229 | + breads.pop(); |
| 230 | + |
| 231 | + // 값 되돌리기. |
| 232 | + j--; |
| 233 | + row = prevRow; |
| 234 | + col = prevCol; |
| 235 | + |
| 236 | + hasConsecutiveBeads = true; |
| 237 | + |
| 238 | + answers[peek.get(0)] += peek.size(); |
| 239 | + continue; |
| 240 | + } |
| 241 | + |
| 242 | + Integer value = peek.remove(0); |
| 243 | + newMap[row][col] = value; |
| 244 | + if (peek.isEmpty()) { |
| 245 | + breads.pop(); |
| 246 | + } |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + private static void transformBeads() { |
| 252 | + int[][] newMap = new int[N + 1][N + 1]; |
| 253 | + int row = sharkRow; |
| 254 | + int col = sharkCol; |
| 255 | + |
| 256 | + Deque<Integer> breads = pickUpForTransform(); |
| 257 | + |
| 258 | + for (int i = 0; ; i++) { |
| 259 | + for (int j = 1; j <= 2 * (i + 1); j++) { |
| 260 | + if (j <= (i + 1)) { |
| 261 | + row = row + dr[(2 * i) % 4]; |
| 262 | + col = col + dc[(2 * i) % 4]; |
| 263 | + } else { |
| 264 | + row = row + dr[(2 * i + 1) % 4]; |
| 265 | + col = col + dc[(2 * i + 1) % 4]; |
| 266 | + } |
| 267 | + |
| 268 | + if (isOut(row, col) || breads.isEmpty()) { |
| 269 | + map = newMap; |
| 270 | + return; |
| 271 | + } |
| 272 | + |
| 273 | + newMap[row][col] = breads.pop(); |
| 274 | + } |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + private static void init() throws IOException { |
| 279 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 280 | + N = Integer.parseInt(st.nextToken()); |
| 281 | + M = Integer.parseInt(st.nextToken()); |
| 282 | + map = new int[N + 1][N + 1]; |
| 283 | + commands = new int[M][2]; |
| 284 | + |
| 285 | + for (int row = 1; row <= N; row++) { |
| 286 | + st = new StringTokenizer(br.readLine()); |
| 287 | + for (int col = 1; col <= N; col++) { |
| 288 | + map[row][col] = Integer.parseInt(st.nextToken()); |
| 289 | + } |
| 290 | + } |
| 291 | + |
| 292 | + for (int i = 0; i < M; i++) { |
| 293 | + commands[i] = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); |
| 294 | + } |
| 295 | + |
| 296 | + sharkRow = (N + 1) / 2; |
| 297 | + sharkCol = (N + 1) / 2; |
| 298 | + } |
| 299 | +} |
0 commit comments