http://acm.hdu.edu.cn/showproblem.php?pid=1312
从这老题开始吧
View Code
#include <iostream> #include <queue> using namespace std ; char map[25][25]; int ans,n,m; typedef struct L{ int x,y; }L; void bfs(int x,int y) { queue <L> q ; int tab[][2]={1,0,-1,0,0,1,0,-1}; L point,go,front; point.x=x; point.y=y; q.push(point); map[x][y]='#'; while(!q.empty()) { front=q.front(); q.pop(); for(int i=0;i<4;i++) { go.x=front.x+tab[i][0]; go.y=front.y+tab[i][1]; if(go.x>=0&&go.x<n&&go.y>=0&&go.y<m&&map[go.x][go.y]=='.') { ans++; q.push(go); map[go.x][go.y]='#'; } } } } int main() { while(scanf("%d%d",&m,&n),(m||n)) { for(int i=0;i<n;i++) scanf("%s",map[i]); int x,y; for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(map[i][j]=='@') x=i,y=j; ans=1; bfs(x,y); printf("%d\n",ans); } return 0; }