|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +public class DH_색깔폭탄 { |
| 5 | + static class Point { |
| 6 | + int r, c; |
| 7 | + public Point(int r, int c) { |
| 8 | + this.r = r; |
| 9 | + this.c = c; |
| 10 | + } |
| 11 | + } |
| 12 | + static int N, M, score; |
| 13 | + static int[][] map; |
| 14 | + static final int INF = Integer.MAX_VALUE; |
| 15 | + static final int RED = -2, BLACK = -1, EMPTY = 0; |
| 16 | + static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1}; |
| 17 | + |
| 18 | + public static void main(String[] args) throws Exception { |
| 19 | + initInput(); |
| 20 | + solution(); |
| 21 | + System.out.println(score); |
| 22 | + } |
| 23 | + |
| 24 | + static void solution() { |
| 25 | + Deque<Point> bomb; |
| 26 | + while((bomb = step1()).size() >= 2) { // 폭탄 묶음 찾기 |
| 27 | + step2(bomb); // 폭탄 터지기 |
| 28 | + step3(); // 중력 작용하기 |
| 29 | + step4(); // 반시계 방향으로 90도 회전하기 |
| 30 | + step3(); // 중력 작용하기 |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + // 폭탄 묶음 찾기 |
| 35 | + static Deque<Point> step1() { |
| 36 | + Deque<Point> result = new ArrayDeque<Point>(); // 우선순위에 따라 폭탄이 터질 위치를 저장하는 큐 |
| 37 | + Deque<Point> tmp = new ArrayDeque<Point>(); // bfs탐색했던 위치들을 모두 저장하는 큐 |
| 38 | + Deque<Point> q = new ArrayDeque<Point>(); // bfs 탐색하는 큐 |
| 39 | + |
| 40 | + // 폭탄묶음의 크기, 빨간색 폭탄의 개수, |
| 41 | + int groupSize = -INF, redCnt = INF; |
| 42 | + |
| 43 | + // 빨간색 폭탄은 bfs를 할 때마다 또 다시 사용할 수 있으므로 |
| 44 | + // 따로 빨간색을 제외한 색깔 폭탄을 확인했는지 구분해주기 위해 bfs에서 사용하는 방문 배열 외에 또 다른 2차원 boolean 배열을 만들어줌 |
| 45 | + boolean[][] checkColor = new boolean[N][N]; |
| 46 | + |
| 47 | + // bfs 탐색을 하는데, 우선순위에 따른 폭탄 묶음을 구하기 위해 |
| 48 | + // r은 N - 1부터 시작하고, c는 0부터 시작함 |
| 49 | + // 그리고 빨간 색이 아닌 다른 색깔 폭탄일 때 bfs 탐색 시작 |
| 50 | + for(int r = N - 1; r >= 0; r--) { |
| 51 | + for(int c = 0; c < N; c++) { |
| 52 | + if(!isColor(map[r][c]) || checkColor[r][c]) continue; |
| 53 | + |
| 54 | + boolean[][] v = new boolean[N][N]; |
| 55 | + int currentRedCnt = 0; // 현재 위치에서 폭탄 묶음을 구성할 때, 빨간 폭탄의 개수 |
| 56 | + int currentSize = 1; // 현재 위치에서 폭탄 묶음을 구성할 때, 폭탄 묶음의 크기 |
| 57 | + |
| 58 | + tmp.clear(); |
| 59 | + |
| 60 | + q.add(new Point(r, c)); |
| 61 | + tmp.add(new Point(r, c)); |
| 62 | + |
| 63 | + v[r][c] = true; |
| 64 | + |
| 65 | + while(!q.isEmpty()) { |
| 66 | + Point current = q.poll(); |
| 67 | + |
| 68 | + for(int d = 0; d < 4; d++) { |
| 69 | + int nr = current.r + dr[d]; |
| 70 | + int nc = current.c + dc[d]; |
| 71 | + |
| 72 | + // 범위에 벗어났거나, 확인을 한 곳이거나, 돌이 있거나, 빈 공간이라면 continue |
| 73 | + if(!check(nr, nc) || v[nr][nc] || map[nr][nc] == BLACK || map[nr][nc] == EMPTY) continue; |
| 74 | + |
| 75 | + // 빨간 폭탄도 아니고, 검정폭탄도 아닌데, 현재 자신의 색깔과 다른 경우 |
| 76 | + if((map[nr][nc] > 0 && map[nr][nc] != map[r][c])) continue; |
| 77 | + |
| 78 | + // 빨간 폭탄일 경우 |
| 79 | + if(map[nr][nc] == RED) currentRedCnt += 1; |
| 80 | + |
| 81 | + q.add(new Point(nr, nc)); |
| 82 | + tmp.add(new Point(nr, nc)); |
| 83 | + |
| 84 | + v[nr][nc] = true; |
| 85 | + currentSize += 1; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // 폭탄 묶음 크기가 큰 폭탄부터 |
| 90 | + if(groupSize < currentSize) { |
| 91 | + result.clear(); |
| 92 | + result.addAll(tmp); |
| 93 | + |
| 94 | + groupSize = currentSize; |
| 95 | + redCnt = currentRedCnt; |
| 96 | + } |
| 97 | + // 폭탄 묶음 크기가 같다면, 빨간 폭탄의 개수 확인하기 |
| 98 | + else if(groupSize == currentSize) { |
| 99 | + if(currentRedCnt < redCnt) { |
| 100 | + result.clear(); |
| 101 | + result.addAll(tmp); |
| 102 | + |
| 103 | + groupSize = currentSize; |
| 104 | + redCnt = currentRedCnt; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return result; |
| 111 | + } |
| 112 | + |
| 113 | + // 폭탄 터지기 |
| 114 | + static void step2(Deque<Point> bomb) { |
| 115 | + score += bomb.size() * bomb.size(); |
| 116 | + |
| 117 | + while(!bomb.isEmpty()) { |
| 118 | + Point current = bomb.poll(); |
| 119 | + map[current.r][current.c] = EMPTY; // 빈 공간으로 바꿔주기 |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // 중력 작용 |
| 124 | + // 폭탄들이 있으면 큐에 넣어주고, 돌이 나온다면 gravity(기준선)을 기준으로 쌓아주기 |
| 125 | + static void step3() { |
| 126 | + int[][] tmp = new int[N][N]; |
| 127 | + Deque<Integer> q = new ArrayDeque<Integer>(); |
| 128 | + |
| 129 | + for(int c = 0; c < N; c++) { |
| 130 | + int gravity = N; |
| 131 | + |
| 132 | + for(int r = N - 1; r >= 0; r--) { |
| 133 | + if(isBomb(map[r][c])) q.add(map[r][c]); |
| 134 | + if(map[r][c] == BLACK) { |
| 135 | + tmp[r][c] = BLACK; |
| 136 | + while(!q.isEmpty()) tmp[--gravity][c] = q.poll(); |
| 137 | + gravity = r; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + while(!q.isEmpty()) tmp[--gravity][c] = q.poll(); |
| 142 | + } |
| 143 | + |
| 144 | + map = tmp; |
| 145 | + } |
| 146 | + |
| 147 | + // 반시계 방향으로 회전 |
| 148 | + // (r, c) -> (N - 1 - c, r) 로 바뀜 |
| 149 | + static void step4() { |
| 150 | + int[][] tmp = new int[N][N]; |
| 151 | + for(int r = 0; r < N; r++) { |
| 152 | + for(int c = 0; c < N; c++) { |
| 153 | + tmp[N - 1 - c][r] = map[r][c]; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + map = tmp; |
| 158 | + } |
| 159 | + |
| 160 | + static boolean check(int r, int c) { |
| 161 | + return r >= 0 && r < N && c >= 0 && c < N; |
| 162 | + } |
| 163 | + |
| 164 | + // 빨간색을 제외한 모든 색깔 폭탄 |
| 165 | + static boolean isColor(int n) { |
| 166 | + return n > 0 && n < M + 1; |
| 167 | + } |
| 168 | + |
| 169 | + static boolean isBomb(int n) { |
| 170 | + return isColor(n) || n == RED; |
| 171 | + } |
| 172 | + |
| 173 | + static void printMapInfo() { |
| 174 | + System.out.println("printMapInfo ---------------------"); |
| 175 | + for(int r = 0; r < map.length; r++) { |
| 176 | + System.out.println(Arrays.toString(map[r])); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + static void initInput() throws Exception { |
| 181 | + System.setIn(new FileInputStream("./input/색깔폭탄.txt")); |
| 182 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 183 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 184 | + |
| 185 | + N = Integer.parseInt(st.nextToken()); |
| 186 | + M = Integer.parseInt(st.nextToken()); |
| 187 | + |
| 188 | + map = new int[N][N]; |
| 189 | + for(int r = 0; r < N; r++) { |
| 190 | + st = new StringTokenizer(br.readLine()); |
| 191 | + for(int c = 0; c < N; c++) { |
| 192 | + map[r][c] = Integer.parseInt(st.nextToken()); |
| 193 | + if(map[r][c] == 0) map[r][c] = RED; |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments