File tree Expand file tree Collapse file tree 1 file changed +73
-0
lines changed
Expand file tree Collapse file tree 1 file changed +73
-0
lines changed Original file line number Diff line number Diff line change 1+ // https://www.acmicpc.net/problem/2638
2+
3+ #include < iostream>
4+ #include < algorithm>
5+ #include < vector>
6+ #include < queue>
7+ using namespace std ;
8+
9+ int N, M, sum;
10+ int num[101 ][101 ];
11+ int cnt[101 ][101 ];
12+ bool c[101 ][101 ];
13+
14+ int dx[4 ] = { 1 , 0 , -1 , 0 };
15+ int dy[4 ] = { 0 , -1 , 0 , 1 };
16+
17+ queue <pair <int , int >> q;
18+
19+ void bfs () {
20+ q.push ({ 1 , 1 });
21+ c[1 ][1 ] = 1 ;
22+
23+ sum = 0 ;
24+ while (!q.empty ()) {
25+ int x = q.front ().first ;
26+ int y = q.front ().second ;
27+ q.pop ();
28+
29+ for (int i = 0 ; i < 4 ; i++) {
30+ int nx = x + dx[i];
31+ int ny = y + dy[i];
32+
33+ if (nx < 1 || nx > N || ny < 1 || ny > M)
34+ continue ;
35+ if (num[nx][ny] == 0 && !c[nx][ny]) {
36+ q.push ({ nx, ny });
37+ c[nx][ny] = 1 ;
38+ sum++;
39+ }
40+ else if (num[nx][ny] == 1 ) {
41+ cnt[nx][ny]++;
42+
43+ if (cnt[nx][ny] >= 2 ) {
44+ num[nx][ny] = 0 ;
45+ c[nx][ny] = 1 ;
46+ }
47+ }
48+ }
49+ }
50+ }
51+
52+ int main () {
53+ ios::sync_with_stdio (false );
54+ cin.tie (NULL ); cout.tie (NULL );
55+
56+ cin >> N >> M;
57+ for (int i = 1 ; i <= N; i++) {
58+ for (int j = 1 ; j <= M; j++) {
59+ cin >> num[i][j];
60+ }
61+ }
62+
63+ int res = 0 ;
64+ while (1 ) {
65+ bfs ();
66+ res++;
67+ memset (c, 0 , sizeof (c));
68+ memset (cnt, 0 , sizeof (cnt));
69+
70+ if (sum == N * M - 1 ) break ;
71+ }
72+ cout << res - 1 ;
73+ }
You can’t perform that action at this time.
0 commit comments