|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + |
| 7 | + private static int[] dr = {0, 0, -1, 1}; |
| 8 | + private static int[] dc = {-1, 1, 0, 0}; |
| 9 | + private static int R; |
| 10 | + private static int C; |
| 11 | + private static char[][] arr; |
| 12 | + |
| 13 | + private static class Point { |
| 14 | + int r; |
| 15 | + int c; |
| 16 | + public Point(int r, int c) { |
| 17 | + this.r = r; |
| 18 | + this.c = c; |
| 19 | + } |
| 20 | + } |
| 21 | + public static void main(String[] args) throws IOException { |
| 22 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 23 | + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 24 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 25 | + |
| 26 | + R = Integer.parseInt(st.nextToken()); |
| 27 | + C = Integer.parseInt(st.nextToken()); |
| 28 | + int N = Integer.parseInt(st.nextToken()); |
| 29 | + |
| 30 | + arr = new char[R][C]; |
| 31 | + for (int i = 0; i < R; i++) { |
| 32 | + arr[i] = br.readLine().toCharArray(); |
| 33 | + } |
| 34 | + |
| 35 | + // 3초에 터지는 지점들 저장 |
| 36 | + List<Point> bombAt3Sec = new ArrayList<>(); |
| 37 | + // 5초에 터지는 지점들 저장 |
| 38 | + List<Point> bombAt5Sec = new ArrayList<>(); |
| 39 | + boolean[][] visited = new boolean[R][C]; |
| 40 | + for (int r = 0; r < R; r++) { |
| 41 | + for (int c = 0; c < C; c++) { |
| 42 | + if (arr[r][c] == 'O') { |
| 43 | + visited[r][c] = true; |
| 44 | + bombAt3Sec.add(new Point(r, c)); |
| 45 | + for (int i = 0; i < 4; i++) { |
| 46 | + int nr = r + dr[i]; |
| 47 | + int nc = c + dc[i]; |
| 48 | + if (0 <= nr && nr < R && 0 <= nc && nc < C) { |
| 49 | + if (arr[nr][nc] == '.' && !visited[nr][nc]) { |
| 50 | + visited[nr][nc] = true; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + for (int r = 0; r < R; r++) { |
| 58 | + for (int c = 0; c < C; c++) { |
| 59 | + if (!visited[r][c]) { |
| 60 | + bombAt5Sec.add(new Point(r, c)); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // N에 맞게 출력 |
| 66 | + if (N == 1) { |
| 67 | + for (int r = 0; r < R; r++) { |
| 68 | + for (int c = 0; c < C; c++) { |
| 69 | + bw.write(arr[r][c]); |
| 70 | + } |
| 71 | + bw.write("\n"); |
| 72 | + } |
| 73 | + } else { |
| 74 | + if (N % 2 == 0) { |
| 75 | + char[][] filledArr = getFilledArr(); |
| 76 | + for (int r = 0; r < R; r++) { |
| 77 | + for (int c = 0; c < C; c++) { |
| 78 | + bw.write(filledArr[r][c]); |
| 79 | + } |
| 80 | + bw.write("\n"); |
| 81 | + } |
| 82 | + } else { |
| 83 | + char[][] result; |
| 84 | + if (N % 4 == 3) { |
| 85 | + result = getArr(bombAt3Sec); |
| 86 | + |
| 87 | + } |
| 88 | + else{ // N % 4 == 1 |
| 89 | + result = getArr(bombAt5Sec); |
| 90 | + } |
| 91 | + for (int r = 0; r < R; r++) { |
| 92 | + for (int c = 0; c < C; c++) { |
| 93 | + bw.write(result[r][c]); |
| 94 | + } |
| 95 | + bw.write("\n"); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + bw.flush(); |
| 102 | + bw.close(); |
| 103 | + br.close(); |
| 104 | + } |
| 105 | + |
| 106 | + private static char[][] getFilledArr() { |
| 107 | + char[][] ret = new char[R][C]; |
| 108 | + for (int r = 0; r < R; r++) { |
| 109 | + Arrays.fill(ret[r], 'O'); |
| 110 | + } |
| 111 | + return ret; |
| 112 | + } |
| 113 | + |
| 114 | + private static char[][] getArr(List<Point> points) { |
| 115 | + char[][] ret = getFilledArr(); |
| 116 | + for (int i = 0; i < points.size(); i++) { |
| 117 | + Point p = points.get(i); |
| 118 | + ret[p.r][p.c] = '.'; |
| 119 | + for (int idx = 0; idx < 4; idx ++) { |
| 120 | + int nr = p.r + dr[idx]; |
| 121 | + int nc = p.c + dc[idx]; |
| 122 | + if (0 <= nr && nr < R && 0 <= nc && nc < C) { |
| 123 | + if (ret[nr][nc] == '.') { continue; } |
| 124 | + ret[nr][nc] = '.'; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + return ret; |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
0 commit comments