图的广度优先搜索
2019-03-19 本文已影响0人
hdchieh
#include<stdio.h>
#include<string.h>
int count;
char room[20][20];
int reached[20][20];
int m,n;
void move(int x,int y){
//printf("x%d,y%d,room[%d][%d]%c\n",x,y,x,y,room[x][y]);
if(x>=0&&x<m&&y>=0&&y<n&&(room[x][y]=='.'||room[x][y]=='@'))
if(reached[x][y]==0){
reached[x][y]=1;
count++;
move(x-1,y);
move(x+1,y);
move(x,y-1);
move(x,y+1);
}
}
int main(){
int i,j;
while(scanf("%d%d",&m,&n)!=EOF){
i=j=-1;
count=0;
for(int k=0;k<m;k++){
scanf("%s",room[k]);
//printf("test:%s\n",room[k]);
memset(reached[k],0,sizeof(int)*n);
if(i==-1){
if(strchr(room[k],'@')!=NULL){
i=k;
j=(int)(strchr(room[k],'@')-room[k]);
}
}
}
move(i,j);
printf("%d\n",count);
}
}