-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path1241_DFS.cpp
42 lines (38 loc) · 959 Bytes
/
1241_DFS.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
#include <iostream>
using namespace std;
const int MAX = 105;
char map[MAX][MAX];
int m, n;
int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[] = {-1, 0, 1, 1, 1, 0, -1, -1};
void dfs(int x, int y) {
int i, nx, ny;
for (i = 0; i < 8; i++) {
nx = x + dx[i];
ny = y + dy[i];
if (nx >= 0 && nx < m && ny >= 0 && ny < n && map[nx][ny] == '@') {
map[nx][ny] = '*';
dfs(nx, ny);
}
}
}
int main() {
while (cin >> m >> n) {
if (n == 0 || m == 0)
break;
int i, j, count;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
cin >> map[i][j];
count = 0;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
if (map[i][j] == '@') {
map[i][j] = '*';
count++;
dfs(i, j);
}
cout << count << endl;
}
return 0;
}