-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4963.cpp
74 lines (67 loc) · 1.01 KB
/
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
66
67
68
69
70
71
72
73
74
// 4963. 섬의 개수
// 2019.05.21
// 그래프 알고리즘, 그래프 이론
#include<iostream>
using namespace std;
int visit[51][51];
int a[51][51];
int dx[8] = {-1,-1,0,1,1,1,0,-1};
int dy[8] = {0,1,1,1,0,-1,-1,-1};
int w, h;
void DFS(int x, int y)
{
visit[x][y] = 1;
for (int i = 0; i < 8; i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
if (xx < 0 || yy < 0 || xx >= h || yy >= w)
{
continue;
}
if (a[xx][yy] == 1 && visit[xx][yy] == 0)
{
DFS(xx, yy);
}
}
}
int main()
{
while (1)
{
cin >> w >> h;
if (w == 0 && h == 0)
{
break;
}
for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 50; j++)
{
visit[i][j] = 0;
}
}
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
cin >> a[i][j];
}
}
// 섬의 개수를 구함
int count = 0;
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
if (a[i][j] == 1 && visit[i][j]==0)
{
DFS(i, j);
count++;
}
}
}
cout << count << endl;
}
return 0;
}