Skip to content

Commit 6d147d1

Browse files
committed
[20251215] BOJ / G2 / 벽 부수고 이동하기 4 / 김민진
1 parent 691c2b6 commit 6d147d1

File tree

1 file changed

+122
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)