http://acm.hit.edu.cn/hoj/problem/view?id=1797
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char map[25][25];
int visited[25][25];
int dir[4][2] ={1,0,0,1,-1,0,0,-1};
int w,h,ans;
bool check(int x,int y){
if(x >= 0 && y>= 0&& x < h && y< w&& !visited[x][y]&& map[x][y] == '.')
return 1;
else
return 0;
}
void dfs(int x,int y){
visited[x][y] = 1;
ans ++;
for(int i = 0;i <4;i++){
int nx = x +dir[i][0];
int ny = y +dir[i][1];
if(check(nx, ny))
dfs(nx, ny);
}
}
int main(){
int x,y;
while(scanf("%d%d",&w,&h)&&(w+h)){
for(int i=0;i<h;i++)
for(int j=0;j<w;j++){
scanf("%1s",&map[i][j]);//细节决定AC啊
if(map[i][j]=='@'){
x=i;y=j;
}
}
for(int i = 0; i <h ;i++)
memset(visited[i], 0, sizeof(visited[i]));
ans =0;
dfs(x,y);
printf("%d\n",ans);
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
char map[25][25];
int w,h,ans;
int Search(int m,int n)
{
if (map[m][n]=='#')
return 0;
else
map[m][n]='#';
return 1 + Search(m-1,n) + Search(m+1,n) + Search(m,n-1) + Search(m,n+1);
}
int main(){
int x,y;
while(scanf("%d%d",&w,&h)&&(w+h)){
memset(map,'#',sizeof map);
for(int i=1;i<=h;i++)
for(int j=1;j<=w;j++){
cin>>map[i][j];//这里用scanf("%1s",&map[i][j])右边边界会出现乱码
if(map[i][j]=='@'){
x=i;y=j;
}
}
for(int i=0;i<=h+2;i++){
for(int j=0;j<=w+2;j++)
printf("%c",map[i][j]);
printf("\n");
}
printf("%d\n",Search(x,y));
}
return 0;
}