版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Lydia_r/article/details/86682515
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
const int N=30;
int w,h,step;
char array[N][N];
int ma[N][N];
//int dir[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
int dir[4][2]= {0,1,0,-1,1,0,-1,0};
void dfs(int x,int y){
if(x<0||y<0||x>=h||y>=w)
return;
int i,j,k;
ma[x][y]=1;
++step;
for(k=0;k<4;k++){
i=x+dir[k][0];
j=y+dir[k][1];
if(ma[i][j]==1||array[i][j]=='#')
continue;
dfs(i,j);
}
}
int main(){
int i,j,sx,sy;
while(scanf("%d %d",&w,&h)){
if(w==0&&h==0)
break;
memset(ma,0,sizeof(ma));
for(i=0;i<h;i++){
scanf("%s",array[i]);
}
for(i=0;i<h;i++){
for(j=0;j<w;j++){
if(array[i][j]=='@'){
sx=i;
sy=j;
break;
}
}
}
step=0;
dfs(sx,sy);
printf("%d\n",step);
}
return 0;
}