|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + /* |
| 7 | + 1. 맨 위 왼쪽이 1,1 맨 오른쪽 아래가 N,N |
| 8 | + 이동 우선순위는 위 (행번호 감소) , 그 다음 왼쪽 (열 번호 감소) |
| 9 | + 이동거리가 가장 가깝고, 물건이나 청소기가 없는 오염된 격자로 이동 |
| 10 | + |
| 11 | + 2. 청소는 자신이 바라보는 방향의 뒷쪽을 제외한 네 칸. |
| 12 | + 가장 먼지량이 큰 방향을 바라보고 청소 시작. 최대 한 격자당 20청소 |
| 13 | +
|
| 14 | + 합이 같은 방향이 여러개면 오른쪽,아래쪽,왼쪽,위쪽 순 |
| 15 | +
|
| 16 | +
|
| 17 | + 3. 먼지가 있는 모든 격자에 5씩 추가 |
| 18 | +
|
| 19 | +
|
| 20 | + 4. 깨끗한 격자는 주변 4방향 격자의 먼지량 합/10의 먼지가 확산됨. 확산은 동시에 진행됨 |
| 21 | + -> 한번에 반영 |
| 22 | +
|
| 23 | + 5/ 총 먼지량 출력 |
| 24 | +
|
| 25 | + 900 * 50 * 50 = 90000 * 25 = 225만 -> 완탐 된다. |
| 26 | +
|
| 27 | + 각각의 청소기에서 bfs를 진행하며 최단 거리의 먼지를 찾는다. |
| 28 | + */ |
| 29 | + |
| 30 | + static class Cleaner{ |
| 31 | + int y,x; |
| 32 | + |
| 33 | + public Cleaner(int y, int x){ |
| 34 | + this.y = y; |
| 35 | + this.x = x; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + static StringBuilder sb = new StringBuilder(); |
| 40 | + static int[][] arr; |
| 41 | + static boolean[][] hasCleaner; |
| 42 | + static int[] dy = {-1,0,1,0}; |
| 43 | + static int[] dx = {0,-1,0,1}; |
| 44 | + static int[] dy2 = {0,-1,0,1,0}; |
| 45 | + static int[] dx2 = {-1,0,1,0,0}; |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + static Cleaner[] cleaners; |
| 50 | + static int ans, size, cleanerCnt, testCnt; |
| 51 | + |
| 52 | + |
| 53 | + public static void main(String[] args) throws IOException { |
| 54 | + |
| 55 | + init(); |
| 56 | + for (int i = 0; i < testCnt; i++){ |
| 57 | + process(); |
| 58 | + print(); |
| 59 | + } |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + private static void init() throws IOException { |
| 64 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 65 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 66 | + |
| 67 | + size = Integer.parseInt(st.nextToken()); |
| 68 | + cleanerCnt = Integer.parseInt(st.nextToken()); |
| 69 | + testCnt = Integer.parseInt(st.nextToken()); |
| 70 | + |
| 71 | + arr = new int[size+2][size+2]; |
| 72 | + hasCleaner = new boolean[size+2][size+2]; |
| 73 | + cleaners = new Cleaner[cleanerCnt]; |
| 74 | + |
| 75 | + for (int i = 1 ; i <= size; i++){ |
| 76 | + st = new StringTokenizer(br.readLine()); |
| 77 | + for (int j = 1; j <= size; j++){ |
| 78 | + arr[i][j] = Integer.parseInt(st.nextToken()); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + for (int i = 0; i < cleanerCnt; i++){ |
| 83 | + st = new StringTokenizer(br.readLine()); |
| 84 | + int y = Integer.parseInt(st.nextToken()); |
| 85 | + int x = Integer.parseInt(st.nextToken()); |
| 86 | + cleaners[i] = new Cleaner(y,x); |
| 87 | + hasCleaner[y][x] = true; |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + private static void process(){ |
| 93 | + ans = 0; |
| 94 | + |
| 95 | + // 1. 움직임 |
| 96 | + for (int i = 0; i < cleanerCnt; i++)move(i); |
| 97 | + |
| 98 | + //2. 청소 |
| 99 | + for (int i = 0; i < cleanerCnt; i++)clean(i); |
| 100 | + |
| 101 | + |
| 102 | + // 3. 먼지가 있는 모든 격자에 5씩 추가 |
| 103 | + for (int i = 1 ; i <= size; i++){ |
| 104 | + for (int j = 1; j <= size; j++){ |
| 105 | + if (arr[i][j] > 0) arr[i][j] += 5; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // 4. 먼지 확산 |
| 110 | + dustDiffuse(); |
| 111 | + |
| 112 | + |
| 113 | + // 5. 답 구해서 출력 |
| 114 | + for (int i = 1 ; i <= size; i++){ |
| 115 | + for (int j = 1; j <= size; j++){ |
| 116 | + if (arr[i][j] < 0) continue; |
| 117 | + ans += arr[i][j]; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private static void clean(int num){ |
| 123 | + Cleaner c = cleaners[num]; |
| 124 | + int max = 0; |
| 125 | + int maxDir= -1; |
| 126 | + int sum = 0; |
| 127 | + for (int i = 0; i < 5; i++) { |
| 128 | + int ny = c.y + dy2[i]; |
| 129 | + int nx = c.x + dx2[i]; |
| 130 | + int newNum = Math.min(arr[ny][nx],20); |
| 131 | + |
| 132 | + if (newNum > 0) { |
| 133 | + sum += newNum; |
| 134 | + } |
| 135 | + } |
| 136 | + for (int i = 0; i < 4; i++){ |
| 137 | + int ny = c.y + dy2[i]; |
| 138 | + int nx = c.x + dx2[i]; |
| 139 | + int newNum = Math.min(arr[ny][nx],20); |
| 140 | + int temp = sum - newNum; |
| 141 | + if (temp > max){ |
| 142 | + max = temp; |
| 143 | + maxDir = i; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + for (int i = 0; i < 5; i++){ |
| 148 | + if (maxDir == i) continue; |
| 149 | + int ny = c.y + dy2[i]; |
| 150 | + int nx = c.x + dx2[i]; |
| 151 | + if (arr[ny][nx] == -1) continue; |
| 152 | + arr[ny][nx] = Math.max(arr[ny][nx] - 20, 0); |
| 153 | + |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private static void move(int num ){ |
| 158 | + Cleaner c = cleaners[num]; |
| 159 | + if (arr[c.y][c.x] != 0) return; |
| 160 | + Queue<Integer> qY = new LinkedList<>(); |
| 161 | + Queue<Integer> qX = new LinkedList<>(); |
| 162 | + boolean[][] visited = new boolean[size+1][size+1]; |
| 163 | + |
| 164 | + int ansY = 500; |
| 165 | + int ansX = 500; |
| 166 | + |
| 167 | + visited[c.y][c.x] = true; |
| 168 | + qY.add(c.y); |
| 169 | + qX.add(c.x); |
| 170 | + |
| 171 | + |
| 172 | + while(!qY.isEmpty() && ansY == 500){ |
| 173 | + int qSize = qY.size(); |
| 174 | + for (int j = 0; j < qSize; j++){ |
| 175 | + int y = qY.poll(); |
| 176 | + int x = qX.poll(); |
| 177 | + |
| 178 | + for (int i = 0 ; i < 4 ; i++){ |
| 179 | + int ny = y + dy[i]; |
| 180 | + int nx = x + dx[i]; |
| 181 | + if ( ny <= 0 || nx <= 0 || ny > size || nx > size) continue; |
| 182 | + if (hasCleaner[ny][nx] || visited[ny][nx] || arr[ny][nx] == -1) continue; |
| 183 | + |
| 184 | + visited[ny][nx] = true; |
| 185 | + if (arr[ny][nx] > 0){ |
| 186 | + if (ny < ansY || (ny == ansY && nx < ansX ) ){ |
| 187 | + ansY = ny; |
| 188 | + ansX = nx; |
| 189 | + } |
| 190 | + continue; |
| 191 | + |
| 192 | + } |
| 193 | + qY.add(ny); |
| 194 | + qX.add(nx); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | + } |
| 201 | + |
| 202 | + if (ansY == 500) return; |
| 203 | + hasCleaner[c.y][c.x] = false; |
| 204 | + c.y = ansY; |
| 205 | + c.x = ansX; |
| 206 | + hasCleaner[c.y][c.x] = true; |
| 207 | + |
| 208 | + } |
| 209 | + |
| 210 | + private static void dustDiffuse(){ |
| 211 | + int[][] temp = new int[size+2][size+2]; |
| 212 | + |
| 213 | + for (int i = 1; i <= size; i++){ |
| 214 | + for (int j = 1; j <= size; j++){ |
| 215 | + if (arr[i][j] == 0){ |
| 216 | + for (int k = 0 ; k < 4; k++){ |
| 217 | + int ny = i + dy[k]; |
| 218 | + int nx = j + dx[k]; |
| 219 | + if (arr[ny][nx] == -1) continue; |
| 220 | + temp[i][j] += arr[ny][nx]; |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + for (int i = 1; i <= size; i++){ |
| 226 | + for (int j = 1; j <= size; j++){ |
| 227 | + arr[i][j] += temp[i][j]/10; |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + private static void print(){ |
| 233 | + System.out.println(ans); |
| 234 | + } |
| 235 | +} |
| 236 | +``` |
0 commit comments