/
Light-oj-1012.cpp
63 lines (54 loc) · 1.16 KB
/
Light-oj-1012.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
63
#include <bits/stdc++.h>
using namespace std;
#define scanI(a) scanf("%d",&a)
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};
int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
char s[50][50];
bool visited[50][50];
int res=1;
int w,h;
bool valid(int i,int j){
return ((i>=0&&i<h)&&(j>=0&&j<w));
}
void dfs(int i,int j){
visited[i][j]=1;
s[i][j]='#';
FOR(k,0,4){
int R=dx[k]+i;
int C=dy[k]+j;
if(!visited[R][C]&&s[R][C]!='#'&&valid(R,C)){
res++;
dfs(R,C);
}
}
}
int main(){
int t,c=1;
scanI(t);
while(t--){
scanI(w);
scanI(h);
FOR(i,0,h){
FOR(j,0,w){
visited[i][j]=0;
}
}
int pos1=0,pos2=0;
FOR(i,0,h){
FOR(j,0,w){
cin>>s[i][j];
if(s[i][j]=='@'){
pos1=i;
pos2=j;
}
}
}
dfs(pos1,pos2);
printf("Case %d: %d\n",c++,res);
res=1;
}
return 0;
}