-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path572-Oil-Deposits.cpp
53 lines (44 loc) · 1.02 KB
/
572-Oil-Deposits.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
#include <stdio.h>
#include<string.h>
const int MAX = 120;
char image[MAX+1][MAX+1];
int N,M;
int dr[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dc[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int valid(int r, int c) {
if (r < 0 || c < 0 || r >= N || c >= M)
return 0;
return 1;
}
void DFSvisit(int r, int c) {
int nr, nc, i;
image[r][c] = '*';
for (i = 0; i < 8; i++) {
nr = r + dr[i];
nc = c + dc[i];
if (valid(nr, nc) && '@' == image[nr][nc])
DFSvisit(nr, nc);
}
}
int DFS() {
int r, c, count = 0;
for (r = 0; r < N; r++)
for (c = 0; c < M; c++)
if ('@' == image[r][c]) {
count++;
DFSvisit(r, c);
}
return count;
}
int main() {
int i, count, kase = 0;
while (2 == scanf("%d%d", &N,&M)) {
if(N == 0 && M == 0 ) break;
for (i = 0; i < N; i++)
scanf("%s", image[i]);
++kase;
count = DFS();
printf("%d\n",count);
}
return 0;
}