-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9B.cpp
More file actions
60 lines (54 loc) · 1.31 KB
/
9B.cpp
File metadata and controls
60 lines (54 loc) · 1.31 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
#include<iostream>
#include<stdio.h>
using namespace std;
int x[] = {1, 0, -1, 0};
int y[] = {0, -1, 0, 1};
int a[20][20] = {0}, H, W, nx, ny, sum;
char tem;
bool isafe(int nnx, int nny);
void dfs(int nnx, int nny);
int main()
{
while((scanf("%d %d", &W, &H)) != EOF && (W || H)){
sum = 0;
for(int i = 0; i < H; i++){
for(int j = 0; j < W; j++){
cin >> tem;
if(tem == '.'){
a[i][j] = 1;
}else if(tem == '#'){
a[i][j] = 0;
}else{
a[i][j] = 1;
nx =j, ny = i;
}
}
}
dfs(nx, ny);
// for(int i = 0; i < H; i++) {
// for (int j = 0; j < W; j++) {
// cout << a[i][j] << " ";
// }
// cout << endl;
// }
printf("%d\n", sum);
}
return 0;
}
void dfs(int nnx, int nny){
if(isafe(nnx, nny) && a[nny][nnx] == 1){
sum++;
a[nny][nnx] = 2;
int mx, my;
for(int i = 0; i < 4; i++){
mx = nnx + x[i], my = nny + y[i];
dfs(mx, my);
}
}
}
bool isafe(int nnx, int nny){
if(nnx < 0 || nnx >= W || nny < 0 || nny >= H){
return false;
}
return true;
}