-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOJ_4963.cpp
65 lines (57 loc) · 1.39 KB
/
BOJ_4963.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
#include <bits/stdc++.h>
#define ll long long
#define lld long double
#define MOD 10007
#define INF 99999999
using namespace std;
int w,h,ans;
int A[55][55];
bool vis[55][55];
int dx[] = {0,0,-1,1,1,1,-1,-1};
int dy[] = {1,-1,0,0,1,-1,-1,1};
void Bfs(int x,int y) {
vis[x][y] = true;
queue<pair<int,int>>q;
q.push({x,y});
while(!q.empty()) {
auto cur = q.front();q.pop();
for(int dir=0;dir<8;dir++) {
int nx = dx[dir] + cur.first;
int ny = dy[dir] + cur.second;
if(nx<=0||ny<=0||nx>h||ny>w)continue;
if(vis[nx][ny])continue;
if(A[nx][ny]==0)continue;
q.push({nx,ny});
vis[nx][ny]=true;
}
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(NULL);
while(1) {
cin >> w >> h;
if(w==0&&h==0)
break;
for(int i=1;i<=50;i++) {
for(int j=1;j<=50;j++) {
A[i][j]=0;
vis[i][j]=false;
}
}
for(int i=1;i<=h;i++) {
for(int j=1;j<=w;j++) {
cin >> A[i][j];
}
}
for(int i=1;i<=h;i++) {
for(int j=1;j<=w;j++) {
if(vis[i][j]==false&&A[i][j]==1) {
Bfs(i,j);
ans++;
}
}
}
cout << ans << '\n';
ans=0;
}
}