-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16946.cpp
More file actions
104 lines (91 loc) · 1.75 KB
/
16946.cpp
File metadata and controls
104 lines (91 loc) · 1.75 KB
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <utility>
#include <queue>
#include <map>
#include <set>
#include <string>
using namespace std;
int n, m;
int arr[1000][1000];
int father[1000][1000];
int dx[] = { 0,1,0,-1 };
int dy[] = { 1,0,-1,0 };
int c = 0;
bool visit[1000][1000];
vector<int>v;
int answer[1000][1000];
void findRoad(int fx, int fy) {
queue<pair<int,int>>q;
visit[fx][fy] = true;
father[fx][fy] = c;
q.push({ fx,fy });
int cnt = 0;
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
cnt++;
q.pop();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || ny <0 || nx>=n || ny >= m) continue;
if (visit[nx][ny]) continue;
if (arr[nx][ny] == 1) continue;
visit[nx][ny] = true;
father[nx][ny] = c;
q.push({ nx,ny });
}
}
v.push_back(cnt);
c++;
}
void sol() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if(arr[i][j] == 0 && !visit[i][j])
findRoad(i,j);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (arr[i][j] == 1) {
set<int> s;
int temp=1;
for (int k = 0; k < 4; k++) {
int nx = i + dx[k];
int ny = j + dy[k];
if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
if (arr[nx][ny] == 1) continue;
s.insert(father[nx][ny]);
}
for (int num : s) {
temp += v[num];
}
answer[i][j] = temp;
}
else {
answer[i][j] = 0;
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
string str;
cin >> str;
for (int j = 0; j < m; j++) {
arr[i][j] = str[j]-'0';
}
}
sol();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << answer[i][j]%10;
}
cout << "\n";
}
}