|
| 1 | +```java |
| 2 | +import java.awt.*; |
| 3 | +import java.io.*; |
| 4 | +import java.util.*; |
| 5 | +import java.util.Queue; |
| 6 | + |
| 7 | +public class BJ_16946_벽_부수고_이동하기_4 { |
| 8 | + |
| 9 | + private static final int[] dx = { 0, 1, 0, -1 }; |
| 10 | + private static final int[] dy = { 1, 0, -1, 0 }; |
| 11 | + |
| 12 | + private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 13 | + private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 14 | + private static StringBuilder sb = new StringBuilder(); |
| 15 | + private static StringTokenizer st; |
| 16 | + |
| 17 | + private static int N, M, index; |
| 18 | + private static int[][] matrix; |
| 19 | + private static boolean[][] visited; |
| 20 | + private static Queue<Point> q; |
| 21 | + private static Set<Integer> set; |
| 22 | + private static Map<Integer, Integer> groups; |
| 23 | + |
| 24 | + public static void main(String[] args) throws IOException { |
| 25 | + init(); |
| 26 | + sol(); |
| 27 | + } |
| 28 | + |
| 29 | + private static void init() throws IOException { |
| 30 | + st = new StringTokenizer(br.readLine()); |
| 31 | + N = Integer.parseInt(st.nextToken()); |
| 32 | + M = Integer.parseInt(st.nextToken()); |
| 33 | + index = 2; |
| 34 | + |
| 35 | + matrix = new int[N][M]; |
| 36 | + for (int i = 0; i < N; i++) { |
| 37 | + String input = br.readLine(); |
| 38 | + for (int j = 0; j < M; j++) { |
| 39 | + matrix[i][j] = input.charAt(j) - '0'; |
| 40 | + } |
| 41 | + } |
| 42 | + visited = new boolean[N][M]; |
| 43 | + q = new ArrayDeque<>(); |
| 44 | + set = new HashSet<>(); |
| 45 | + groups = new HashMap<>(); |
| 46 | + } |
| 47 | + |
| 48 | + private static void sol() throws IOException { |
| 49 | + for (int i = 0; i < N; i++) { |
| 50 | + for (int j = 0; j < M; j++) { |
| 51 | + if (matrix[i][j] == 0 && !visited[i][j]) { |
| 52 | + bfs(i, j); |
| 53 | + index++; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + printMat(); |
| 59 | + |
| 60 | + bw.write(sb.toString()); |
| 61 | + bw.flush(); |
| 62 | + bw.close(); |
| 63 | + br.close(); |
| 64 | + } |
| 65 | + |
| 66 | + private static void bfs(int i, int j) { |
| 67 | + q.clear(); |
| 68 | + q.offer(new Point(i, j)); |
| 69 | + visited[i][j] = true; |
| 70 | + matrix[i][j] = index; |
| 71 | + |
| 72 | + int cnt = 1; |
| 73 | + while (!q.isEmpty()) { |
| 74 | + Point cur = q.poll(); |
| 75 | + |
| 76 | + for (int d = 0; d < 4; d++) { |
| 77 | + int nx = cur.x + dx[d]; |
| 78 | + int ny = cur.y + dy[d]; |
| 79 | + |
| 80 | + if (OOB(nx, ny) || visited[nx][ny] || matrix[nx][ny] != 0) continue; |
| 81 | + |
| 82 | + visited[nx][ny] = true; |
| 83 | + matrix[nx][ny] = index; |
| 84 | + cnt++; |
| 85 | + q.offer(new Point(nx, ny)); |
| 86 | + } |
| 87 | + } |
| 88 | + groups.put(index, cnt); |
| 89 | + } |
| 90 | + |
| 91 | + private static void printMat() { |
| 92 | + int cnt; |
| 93 | + for (int i = 0; i < N; i++) { |
| 94 | + for (int j = 0; j < M; j++) { |
| 95 | + if (matrix[i][j] != 1) { |
| 96 | + sb.append(0); |
| 97 | + } else { |
| 98 | + cnt = 1; |
| 99 | + set.clear(); |
| 100 | + for (int d = 0; d < 4; d++) { |
| 101 | + int nx = i + dx[d]; |
| 102 | + int ny = j + dy[d]; |
| 103 | + |
| 104 | + if (OOB(nx, ny) || matrix[nx][ny] == 1 || set.contains(matrix[nx][ny])) continue; |
| 105 | + |
| 106 | + set.add(matrix[nx][ny]); |
| 107 | + cnt += groups.get(matrix[nx][ny]); |
| 108 | + } |
| 109 | + sb.append(cnt % 10); |
| 110 | + } |
| 111 | + } |
| 112 | + sb.append("\n"); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private static boolean OOB(int x, int y) { |
| 117 | + return x < 0 || N <= x || y < 0 || M <= y; |
| 118 | + } |
| 119 | + |
| 120 | +} |
| 121 | +``` |
0 commit comments