-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboj4963.cpp
More file actions
63 lines (55 loc) · 1.13 KB
/
boj4963.cpp
File metadata and controls
63 lines (55 loc) · 1.13 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
#include <iostream>
#include <vector>
using namespace std;
int dx[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
int dy[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
int** map;
int** visited;
int w, h;
void dfs(int y, int x) {
for (int i = 0; i < 8; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= w || ny >= h) continue;
if (!visited[ny][nx] && map[ny][nx]) {
visited[ny][nx] = 1;
dfs(ny, nx);
}
}
}
int solve(int w, int h) {
map = new int* [h];
visited = new int* [h];
int ret = 0;
for (int i = 0; i < h; i++) {
map[i] = new int[w];
visited[i] = new int[w];
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> map[i][j];
visited[i][j] = 0;
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (!visited[i][j] && map[i][j]) {
visited[i][j] = 1;
dfs(i, j);
ret++;
}
}
}
delete[] map;
delete[] visited;
return ret;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
while (1) {
cin >> w >> h;
if (!w&& !h) break;
cout << solve(w, h) << '\n';
}
return 0;
}