|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.IOException; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.util.LinkedList; |
| 5 | +import java.util.Queue; |
| 6 | + |
| 7 | +public class YJ_시공의_돌풍 { |
| 8 | + static class Dust{ |
| 9 | + int x; |
| 10 | + int y; |
| 11 | + int data; |
| 12 | + |
| 13 | + Dust(int x, int y, int data){ |
| 14 | + this.x = x; |
| 15 | + this.y = y; |
| 16 | + this.data = data; |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + static int[][] room; |
| 21 | + static int[][] temp; |
| 22 | + static boolean[][] visited; |
| 23 | + public static void main(String[] args) throws IOException { |
| 24 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 25 | + String[] data = br.readLine().split("\\s"); |
| 26 | + int n = Integer.parseInt(data[0]); |
| 27 | + int m = Integer.parseInt(data[1]); |
| 28 | + int t = Integer.parseInt(data[2]); |
| 29 | + |
| 30 | + visited = new boolean[n][m]; |
| 31 | + room = new int[n][m]; |
| 32 | + int[] tornado = new int[2]; |
| 33 | + int z = 0; |
| 34 | + for(int i=0; i<n; i++){ |
| 35 | + String[] d = br.readLine().split("\\s"); |
| 36 | + for(int j=0; j<m; j++){ |
| 37 | + int dust = Integer.parseInt(d[j]); |
| 38 | + if(dust == -1){ |
| 39 | + visited [i][j] = true; |
| 40 | + tornado[z++] = i; |
| 41 | + } |
| 42 | + room[i][j] = dust; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + int result = 0; |
| 47 | + while(t-- > 0){ |
| 48 | + //먼지가 인접한 4방향의 상하좌우 칸으로 확산: bfs 너비탐색 |
| 49 | + bfs(n,m); |
| 50 | + //시공의 돌풍이 청소를 시작 |
| 51 | + goTornado(n,m,tornado); |
| 52 | + //확산된 먼지 합산 |
| 53 | + result = calculateTotalDust(n,m); |
| 54 | + } |
| 55 | + System.out.println(result); |
| 56 | + } |
| 57 | + |
| 58 | + static void bfs(int n, int m){ |
| 59 | + final int SPREAD = 5; |
| 60 | + Queue<Dust> queue = new LinkedList<>(); |
| 61 | + int[] nx = {1,0,-1,0}; |
| 62 | + int[] ny = {0,1,0,-1}; |
| 63 | + |
| 64 | + temp = new int[n][m]; |
| 65 | + queue.offer(new Dust(0,0,room[0][0])); |
| 66 | + |
| 67 | + while(!queue.isEmpty()){ |
| 68 | + Dust dust = queue.poll(); |
| 69 | + int share = dust.data/SPREAD; |
| 70 | + |
| 71 | + int currX = dust.x; |
| 72 | + int currY = dust.y; |
| 73 | + if(visited[currX][currY]){ |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + for(int i=0; i<4; i++){ |
| 78 | + int x = currX + nx[i]; |
| 79 | + int y = currY + ny[i]; |
| 80 | + |
| 81 | + if(stop(x,y,n,m)){ |
| 82 | + continue; |
| 83 | + } |
| 84 | + int next = room[x][y]; |
| 85 | + if(next == -1){ |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + temp[x][y] += share; |
| 90 | + room[currX][currY] -= share; |
| 91 | + queue.offer(new Dust(x,y,next)); |
| 92 | + } |
| 93 | + visited[currX][currY] = true; |
| 94 | + } |
| 95 | + |
| 96 | + sumDust(n,m); |
| 97 | + } |
| 98 | + |
| 99 | + private static boolean stop(int x, int y, int n, int m){ |
| 100 | + return x<0 || y<0 || x>=n || y>=m; |
| 101 | + } |
| 102 | + |
| 103 | + private static void sumDust(int n, int m){ |
| 104 | + for(int x=0; x<n; x++){ |
| 105 | + for(int y=0; y<m; y++){ |
| 106 | + room[x][y] += temp[x][y]; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + static void goTornado(int n, int m, int[] tornado){ |
| 112 | + up(0,0,tornado[0],m-1); |
| 113 | + down(tornado[1],0,n-1,m-1); |
| 114 | + cleaning(tornado); |
| 115 | + } |
| 116 | + // 0,0,윗태풍r,윗태풍c |
| 117 | + private static void up(int startR, int startC, int endR, int endC){ |
| 118 | + int corner = room[startR][startC]; |
| 119 | + //맨윗 줄 ← |
| 120 | + for(int c = startC; c<endC; c++){ |
| 121 | + room[startR][c] = room[startR][c+1]; |
| 122 | + } |
| 123 | + //맨오른쪽 줄 ↑ |
| 124 | + for(int r = startR; r<endR; r++){ |
| 125 | + room[r][endC] = room[r+1][endC]; |
| 126 | + } |
| 127 | + //맨아랫 줄 → |
| 128 | + for(int c = endC; c>startC; c--){ |
| 129 | + room[endR][c] = room[endR][c-1]; |
| 130 | + } |
| 131 | + //맨왼쪽 줄 ↓ |
| 132 | + for(int r = endR; r>startR; r--){ |
| 133 | + room[r][startR] = room[r-1][startR]; |
| 134 | + } |
| 135 | + room[startR+1][startC] = corner; |
| 136 | + } |
| 137 | + |
| 138 | + // 아랫태풍r,0,끝r,끝c |
| 139 | + private static void down(int startR, int startC, int endR, int endC){ |
| 140 | + int corner = room[startR][startC]; |
| 141 | + //맨왼쪽 줄 ↓ |
| 142 | + for(int r = startR; r<endR; r++){ |
| 143 | + room[r][startC] = room[r+1][startC]; |
| 144 | + } |
| 145 | + //맨아랫 줄 ← |
| 146 | + for(int c = startC; c<endC; c++){ |
| 147 | + room[endR][c] = room[endR][c+1]; |
| 148 | + } |
| 149 | + //맨오른쪽 줄 ↑ |
| 150 | + for(int r = endR; r>startR; r--){ |
| 151 | + room[r][endC] = room[r-1][endC]; |
| 152 | + } |
| 153 | + //맨윗쪽 줄 → |
| 154 | + for(int c = endC; c>startC; c--){ |
| 155 | + room[startR][c] = room[startR][c-1]; |
| 156 | + } |
| 157 | + room[startR][startC+1] = corner; |
| 158 | + } |
| 159 | + |
| 160 | + private static void cleaning(int[] tornado){ |
| 161 | + room[tornado[0]][0] = -1; |
| 162 | + room[tornado[1]][0] = -1; |
| 163 | + room[tornado[0]][1] = 0; |
| 164 | + room[tornado[1]][1] = 0; |
| 165 | + } |
| 166 | + |
| 167 | + static int calculateTotalDust(int n, int m){ |
| 168 | + int result = 0; |
| 169 | + for(int x=0; x<n; x++){ |
| 170 | + for(int y=0; y<m; y++){ |
| 171 | + if(room[x][y] == -1){ |
| 172 | + continue; |
| 173 | + } |
| 174 | + visited [x][y] = false; |
| 175 | + result += room[x][y]; |
| 176 | + } |
| 177 | + } |
| 178 | + return result; |
| 179 | + } |
| 180 | +} |
0 commit comments