|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +class Main { |
| 5 | + |
| 6 | + // 좌 하 우 상 |
| 7 | + private static final int[] rows = {0, 1, 0, -1}; |
| 8 | + private static final int[] cols = {-1, 0, 1, 0}; |
| 9 | + private static final int[][][] dustRows = { |
| 10 | + {{-2, 0, 2}, {-1, -1, 10}, {-1, 0, 7}, {-1, 1, 1}, {0, -2, 5}, {1, -1, 10}, {1, 0, 7}, {1, 1, 1}, {2, 0, 2}}, |
| 11 | + {{-1, -1, 1}, {-1, 1, 1}, {0, -2, 2}, {0, -1, 7}, {0, 1, 7}, {0, 2, 2}, {1, -1, 10}, {1, 1, 10}, {2, 0, 5}}, |
| 12 | + {{-2, 0, 2}, {-1, -1, 1}, {-1, 0, 7}, {-1, 1, 10}, {0, 2, 5}, {1, -1, 1}, {1, 0, 7}, {1, 1, 10}, {2, 0, 2}}, |
| 13 | + {{-2, 0, 5}, {-1, -1, 10}, {-1, 1, 10}, {0, -2, 2}, {0, -1, 7}, {0, 1, 7}, {0, 2, 2}, {1, -1, 1}, {1, 1, 1}} |
| 14 | + }; |
| 15 | + |
| 16 | + private static int n, totalOutDust; |
| 17 | + private static int[][] map; |
| 18 | + |
| 19 | + public static void main(String[] args) throws Exception { |
| 20 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 21 | + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 22 | + n = Integer.parseInt(br.readLine()); |
| 23 | + map = new int[n][n]; |
| 24 | + |
| 25 | + for (int i = 0; i < n; i++) { |
| 26 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 27 | + for (int j = 0; j < n; j++) { |
| 28 | + map[i][j] = Integer.parseInt(st.nextToken()); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + int moveAmount = 1; |
| 33 | + int currentMoveAmount = 0; |
| 34 | + int directionCount = 0; |
| 35 | + int direction = 0; |
| 36 | + int nextR = n / 2; |
| 37 | + int nextC = n / 2; |
| 38 | + |
| 39 | + for (int count = 0; count < n * n - 1; count++) { |
| 40 | + nextR += rows[direction]; |
| 41 | + nextC += cols[direction]; |
| 42 | + // a의 위치 |
| 43 | + int aRow = nextR + rows[direction]; |
| 44 | + int aCol = nextC + cols[direction]; |
| 45 | + |
| 46 | + moveDust(direction, nextR, nextC, aRow, aCol); |
| 47 | + // 현재 방향으로 얼마나 이동했는지 |
| 48 | + currentMoveAmount++; |
| 49 | + // 현재 방향으로 꽉 채워서 이동했다면 |
| 50 | + if (currentMoveAmount == moveAmount) { |
| 51 | + // 다음 위치로 이동하도록 변경 |
| 52 | + directionCount++; |
| 53 | + direction = (direction + 1) % 4; |
| 54 | + currentMoveAmount = 0; |
| 55 | + } |
| 56 | + // 방향 전환이 두 번 됐다면 현재 방향 이동량을 1증가 |
| 57 | + if (directionCount == 2) { |
| 58 | + moveAmount++; |
| 59 | + directionCount = 0; |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + bw.write(totalOutDust + ""); |
| 65 | + bw.flush(); |
| 66 | + bw.close(); |
| 67 | + } |
| 68 | + |
| 69 | + public static void moveDust(int direction, int r, int c, int aRow, int aCol) { |
| 70 | + int currentDust = map[r][c]; |
| 71 | + int totalMoveDust = 0; |
| 72 | + for (int i = 0; i < 9; i++) { |
| 73 | + int nextR = r + dustRows[direction][i][0]; |
| 74 | + int nextC = c + dustRows[direction][i][1]; |
| 75 | + int percentage = dustRows[direction][i][2]; |
| 76 | + int moveDust = (int) (currentDust * (percentage / 100.0)); |
| 77 | + totalMoveDust += moveDust; |
| 78 | + |
| 79 | + // 모래가 밖을 벗어나는 위치라면 |
| 80 | + if (nextR < 0 || nextR >= n || nextC < 0 || nextC >= n) { |
| 81 | + totalOutDust += moveDust; |
| 82 | + continue; |
| 83 | + } |
| 84 | + map[nextR][nextC] += moveDust; |
| 85 | + } |
| 86 | + if (aRow < 0 || aRow >= n || aCol < 0 || aCol >= n) { |
| 87 | + // a의 위치가 맵 밖을 벗어나면 |
| 88 | + totalOutDust += currentDust - totalMoveDust; |
| 89 | + } else { |
| 90 | + map[aRow][aCol] += currentDust - totalMoveDust; |
| 91 | + } |
| 92 | + map[r][c] = 0; |
| 93 | + } |
| 94 | +} |
0 commit comments