*
深度优先搜索
poj 1979
*/
#include <cstdio>
#include <string.h>
#define MAX_N 30
void dfs(int x,int y);
//input
char room[MAX_N][MAX_N];
int row,col;//行 列
int cnt;
int main(){
//freopen("C:/in.txt","r",stdin);
while(scanf("%d%d",&col,&row) && row && col){
memset(room,0,sizeof(room));
cnt = 0;
for(int i = 0;i < row; i++){
scanf("%s",room[i]);
}
//检测@
int i,j;
for( i = 0;i < row; i++){
for( j = 0;j < col; j++){
if(room[i][j] == '@'){
room[i][j] = '.';
dfs(i,j);
}
}
}
printf("%d\n",cnt);
}
return 0;
}
void dfs(int x,int y){
if(0 <= x && x < row && 0 <= y && y < col && room[x][y] == '.'){
++cnt;
room[x][y] = '#';
}else{
return ;
}
dfs(x-1,y);
dfs(x,y-1);
dfs(x+1,y);
dfs(x,y+1);
}
poj 1979
最新推荐文章于 2018-05-21 22:10:04 发布