/
572.cpp
62 lines (52 loc) · 1 KB
/
572.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
//Uva-572
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<string>
#include<map>
#include<stack>
#include <algorithm>
using namespace std;
const int MAXSIZE = 1000;
char maps[MAXSIZE][MAXSIZE];
int n, m;
int _ans;
int dir[8][2] = {
{ -1,-1},{ -1,0 },{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}
};
void dfs(int x,int y) {
for (int i = 0; i < 8; i++) {
int xxx = x + dir[i][0];
int yyy = y + dir[i][1];
if (xxx >= 0 && xxx < n && yyy >= 0 && yyy < m && maps[xxx][yyy] == '@') {
maps[xxx][yyy] = '*';
dfs(xxx, yyy);
}
}
}
int main() {
scanf("%d %d", &n, &m);
while (n != 0 && m != 0){
getchar();
_ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
scanf("%c", &maps[i][j]);
getchar();
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (maps[i][j] == '@') {
maps[i][j] = '*';
dfs(i, j);
_ans++;
}
}
}
if(n != 0 || m != 0)
cout << _ans << endl;
scanf("%d %d", &n, &m);
}
return 0;
}