|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +public class SB_시공의_돌풍 { |
| 5 | + static int T,N,M; |
| 6 | + static int[][] board; |
| 7 | + static int[] dx = {-1, 1, 0, 0}; |
| 8 | + static int[] dy = {0, 0, -1, 1}; |
| 9 | + static Node[] cl = new Node[2]; |
| 10 | + |
| 11 | + private static void diffusion() { |
| 12 | + // 먼지 확산 |
| 13 | + int[][] dust = new int[N][M]; |
| 14 | + for (int i = 0; i < N; i++) { |
| 15 | + for (int j = 0; j < M; j++) { |
| 16 | + if (board[i][j]==-1) continue; |
| 17 | + int div = board[i][j]/5; |
| 18 | + int cnt = 0; |
| 19 | + for (int k = 0; k < 4; k++) { |
| 20 | + int nx = i + dx[k]; |
| 21 | + int ny = j + dy[k]; |
| 22 | + if (!isValid(nx, ny) || board[nx][ny]==-1) continue; |
| 23 | + dust[nx][ny]+=div; |
| 24 | + cnt++; |
| 25 | + } |
| 26 | + dust[i][j] -= div*cnt; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + // 확산된 먼지 더하기 |
| 31 | + for (int i = 0; i < N; i++) { |
| 32 | + for (int j = 0; j < M; j++) { |
| 33 | + board[i][j] += dust[i][j]; |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private static void cleanUp() { |
| 39 | + // 윗부분 먼지 흡입 |
| 40 | + Queue<Integer> que1 = new ArrayDeque<>(); |
| 41 | + int r = cl[0].r; |
| 42 | + que1.add(0); // 청소기에서 나온 공기 0 |
| 43 | + for (int j = 1; j < M; j++) { // (R,0) -> (R,M) |
| 44 | + que1.add(board[r][j]); |
| 45 | + } |
| 46 | + for (int i = r-1; i >= 0; i--) { // (R,M) -> (0,M) |
| 47 | + que1.add(board[i][M-1]); |
| 48 | + } |
| 49 | + for (int j = M-1-1; j >= 0; j--) { // (0,M) -> (0,0) |
| 50 | + que1.add(board[0][j]); |
| 51 | + } |
| 52 | + for (int i = 1; i < r; i++) { // (0,0) -> (R,0) |
| 53 | + que1.add(board[i][0]); |
| 54 | + } |
| 55 | + |
| 56 | + // 윗부분 먼지 밀기 |
| 57 | + for (int j = 1; j < M; j++) { // (R,0) -> (R,M) |
| 58 | + board[r][j] = que1.poll(); |
| 59 | + } |
| 60 | + for (int i = r-1; i >= 0; i--) { // (R,M) -> (0,M) |
| 61 | + board[i][M-1] = que1.poll(); |
| 62 | + } |
| 63 | + for (int j = M-1-1; j >= 0; j--) { // (0,M) -> (0,0) |
| 64 | + board[0][j] = que1.poll(); |
| 65 | + } |
| 66 | + for (int i = 1; i < r; i++) { // (0,0) -> (R,0) |
| 67 | + board[i][0] = que1.poll(); |
| 68 | + } |
| 69 | + board[r][0] = -1; |
| 70 | + } |
| 71 | + |
| 72 | + private static void cleanDown() { |
| 73 | + // 아래 부분 먼지 흡입 |
| 74 | + Queue<Integer> que = new ArrayDeque<>(); |
| 75 | + int r = cl[1].r; |
| 76 | + que.add(0); // 청소기에서 나온 공기 0 |
| 77 | + for (int j = 1; j < M; j++) { // (R,0) -> (R,M) |
| 78 | + que.add(board[r][j]); |
| 79 | + } |
| 80 | + for (int i = r+1; i < N; i++) { // (R,M) -> (N,M) |
| 81 | + que.add(board[i][M-1]); |
| 82 | + } |
| 83 | + for (int j = M-1-1; j >= 0; j--) { // (N,M) -> (N,0) |
| 84 | + que.add(board[N-1][j]); |
| 85 | + } |
| 86 | + for (int i = N-1-1; i > r; i--) { // (N,0) -> (R,0) |
| 87 | + que.add(board[i][0]); |
| 88 | + } |
| 89 | + |
| 90 | + // 아래부분 먼지 밀기 |
| 91 | + for (int j = 1; j < M; j++) { // (R,0) -> (R,M) |
| 92 | + board[r][j] = que.poll(); |
| 93 | + } |
| 94 | + for (int i = r+1; i < N; i++) { // (R,M) -> (N,M) |
| 95 | + board[i][M-1] = que.poll(); |
| 96 | + } |
| 97 | + for (int j = M-1-1; j >= 0; j--) { // (N,M) -> (N,0) |
| 98 | + board[N-1][j] = que.poll(); |
| 99 | + } |
| 100 | + for (int i = N-1-1; i > r; i--) { // (N,0) -> (R,0) |
| 101 | + board[i][0] = que.poll(); |
| 102 | + } |
| 103 | + board[r][0] = -1; |
| 104 | + } |
| 105 | + |
| 106 | + private static boolean isValid(int x, int y) { |
| 107 | + return 0<=x && x<N && 0<=y && y<M; |
| 108 | + } |
| 109 | + public static void main(String[] args) throws IOException { |
| 110 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 111 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 112 | + |
| 113 | + N = Integer.parseInt(st.nextToken()); |
| 114 | + M = Integer.parseInt(st.nextToken()); |
| 115 | + T = Integer.parseInt(st.nextToken()); |
| 116 | + |
| 117 | + board = new int[N][M]; |
| 118 | + boolean flag = false; |
| 119 | + for (int i = 0; i < N; i++) { |
| 120 | + st = new StringTokenizer(br.readLine()); |
| 121 | + for (int j = 0; j < M; j++) { |
| 122 | + board[i][j] = Integer.parseInt(st.nextToken()); |
| 123 | + if (board[i][j]==-1) { |
| 124 | + if(!flag) { |
| 125 | + cl[0] = new Node(i, j); |
| 126 | + flag = true; |
| 127 | + } else cl[1] = new Node(i, j); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + for (int t = 0; t < T; t++) { |
| 133 | + // 1. 먼지 확산 |
| 134 | + diffusion(); |
| 135 | + |
| 136 | + // 2. 돌풍 청소 |
| 137 | + cleanUp(); |
| 138 | + cleanDown(); |
| 139 | + } |
| 140 | + |
| 141 | + // 총 먼지 |
| 142 | + int cnt = 0; |
| 143 | + for (int i = 0; i < N; i++) { |
| 144 | + for (int j = 0; j < M; j++) { |
| 145 | + if (board[i][j]==-1) continue; |
| 146 | + cnt += board[i][j]; |
| 147 | + } |
| 148 | + } |
| 149 | + System.out.println(cnt); |
| 150 | + } |
| 151 | + |
| 152 | + static class Node{ |
| 153 | + int r, c; |
| 154 | + |
| 155 | + Node(int r, int c) { |
| 156 | + this.r = r; |
| 157 | + this.c = c; |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments