Guilty Prince LightOJ - 1012
1 #include<cstdio>
2 #include<cstring>
3 bool vis[22][22];
4 int ans,h,w,T,TT;
5 char s[22][22];
6 void dfs(int x,int y)
7 {
8 vis[x][y]=1;
9 ans++;
10 if(x>1&&!vis[x-1][y]) dfs(x-1,y);
11 if(x<h&&!vis[x+1][y]) dfs(x+1,y);
12 if(y>1&&!vis[x][y-1]) dfs(x,y-1);
13 if(y<w&&!vis[x][y+1]) dfs(x,y+1);
14 }
15 int main()
16 {
17 int i,j,sx,sy;
18 scanf("%d",&T);
19 for(TT=1;TT<=T;TT++)
20 {
21 scanf("%d%d",&w,&h);
22 if(w==0||h==0)
23 {
24 printf("Case %d: %d
",TT,0);
25 continue;
26 }
27 ans=0;
28 memset(s,0,sizeof(s));
29 memset(vis,0,sizeof(vis));
30 for(i=1;i<=h;i++)
31 scanf("%s",s[i]+1);
32 for(i=1;i<=h;i++)
33 for(j=1;j<=w;j++)
34 if(s[i][j]=='#')
35 vis[i][j]=1;
36 else if(s[i][j]=='@')
37 {
38 sx=i;sy=j;
39 }
40 dfs(sx,sy);
41 printf("Case %d: %d
",TT,ans);
42 }
43 return 0;
44 }