-
Notifications
You must be signed in to change notification settings - Fork 0
/
벽 부수고 이동하기4.java
123 lines (106 loc) · 3.45 KB
/
벽 부수고 이동하기4.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// BOJ - 벽 부수고 이동하기4(16946번)
// BFS
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main_16946 {
public static int n, m;
public static int[][] map;
public static int[][] cnt;
public static int[][] ans;
public static int[][] group;
public static class Node {
int x;
int y;
public Node(int x, int y){
this.x = x;
this.y = y;
}
}
public static int g_cnt;
public static int[] dx = {-1, 1, 0, 0};
public static int[] dy = {0, 0, -1, 1};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
StringBuilder sb = new StringBuilder();
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
map = new int[n][m];
ans = new int[n][m];
cnt = new int[n][m];
group = new int[n][m];
for(int i=0;i<n;i++){
char[] c = br.readLine().toCharArray();
for(int j=0;j<m;j++){
map[i][j] = c[j] - '0';
}
}
int bfs_cnt = 0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(map[i][j] == 0 && group[i][j] == 0){
g_cnt++;
bfs_cnt++;
bfs(i, j);
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(map[i][j] == 1){
boolean[] check = new boolean[g_cnt+1];
int sum = 0;
for(int d=0;d<4;d++){
int nx = i + dx[d];
int ny = j + dy[d];
if(nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
if(map[nx][ny] == 0) {
int g = group[nx][ny];
if(check[g]){
continue;
} else {
sum += ans[nx][ny];
check[g] = true;
}
}
}
sb.append((sum+1)%10);
}else {
sb.append(0);
}
}
sb.append("\n");
}
System.out.println(sb.toString());
}
public static void bfs(int x, int y){
Queue<Node> q = new LinkedList<>();
Queue<Node> q2 = new LinkedList<>();
int c = 1;
q.add(new Node(x, y));
q2.add(new Node(x, y));
group[x][y] = g_cnt;
while (!q.isEmpty()){
Node node = q.poll();
for(int d=0;d<4;d++){
int nx = node.x + dx[d];
int ny = node.y + dy[d];
if(nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
if(group[nx][ny] != 0) continue;
if(map[nx][ny] != 0 ) continue;
group[nx][ny] = g_cnt;
c++;
q.add(new Node(nx, ny));
q2.add(new Node(nx, ny));
}
}
while (!q2.isEmpty()){
Node node = q2.poll();
ans[node.x][node.y] = c;
}
}
}