-
Notifications
You must be signed in to change notification settings - Fork 0
/
2638.cpp
92 lines (76 loc) · 1.81 KB
/
2638.cpp
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
#include<iostream>
#include<cstring>
#include<queue>
#define N 105
using namespace std;
typedef pair<int, int> pii;
int n, m, map[N][N], c, check[N][N];
int xx[4]={0, 0, 1, -1};
int yy[4]={1, -1, 0, 0};
void bfs(int x, int y)
{
queue<pii> q;
q.push({x, y});
memset(check, 0, sizeof(check));
while(!q.empty()){
pii head = q.front();
q.pop();
for(int i=0; i<4; i++){
int nx=head.first+xx[i];
int ny=head.second+yy[i];
if(nx<1 || ny<1 || nx>n || ny>m) continue;
if(check[nx][ny] || map[nx][ny]) continue;
check[nx][ny]=1;
map[nx][ny]=2;
q.push({nx, ny});
}
}
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int i, j;
cin >> n >> m;
for(i=1; i<=n; i++){
for(j=1; j<=m; j++){
cin >> map[i][j];
c+=map[i][j];
}
}
bfs(1, 1);
int ans=0;
while(c){
ans++;
// for(i=1; i<=n; i++){
// for(j=1; j<=m; j++)
// cout << map[i][j] << " ";
// cout << endl;
// }
// cout << endl;
for(i=1; i<=n; i++){
for(j=1; j<=m; j++){
if(map[i][j]==1){
int air=0;
for(int k=0; k<4; k++){
if(map[i+xx[k]][j+yy[k]]==2) air++;
}
if(air>=2){
map[i][j]=3;
c--;
}
}
}
}
for(i=1; i<=n; i++){
for(j=1; j<=m; j++){
if(map[i][j]==3){
map[i][j]=2;
bfs(i, j);
}
}
}
}
cout << ans;
return 0;
}