-
Notifications
You must be signed in to change notification settings - Fork 0
/
Uva572.cpp
50 lines (45 loc) · 1.08 KB
/
Uva572.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
#include <cstdio>
#include <cstring>
using namespace std;
bool visited[150][150] = {0};
char grid[150][150],line[150];
int dr[] = {1,1,0,-1,-1,-1, 0, 1}, n,m,count;
int dc[] = {0,1,1, 1, 0,-1,-1,-1};
void dfs(int i, int j){
if (i < 0 || i >= m || j < 0 || j >= n){
return;
}
if (!visited[i][j])
visited[i][j] = 1;
else
return;
int nr, nc;
for (int d=0;d<8;++d){
// printf("hers\n");
nr = i + dr[d];
nc = j + dc[d];
if (!visited[nr][nc] && grid[nr][nc] == '@'){
dfs(nr,nc);
}
}
return;
}
int main()
{
while(sscanf(gets(line), "%d %d", &m,&n), m){
count = 0;
memset(visited, 0, sizeof visited);
for(int i=0;i<m;++i){
gets(grid[i]);
}
for(int i=0;i<m;++i){
for(int j=0;j<n;j++){
if(!visited[i][j] && grid[i][j] == '@'){
dfs(i,j);
count ++;
}
}
}
printf("%d\n", count);
}
}