6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
45 59 6 13#include<stdio.h> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int f[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; int t,m,n,X,Y; char str[21][21]; void BFS(int x,int y) { int xx,yy,i; if(x>=m||x<0||y<0||y>=n) return; for(i=0;i<4;i++) { xx=x+f[i][0]; yy=y+f[i][1]; if(xx<0||xx>=m||yy<0||yy>=n||str[xx][yy]=='#') continue; t++; str[xx][yy]='#'; BFS(xx,yy); } } int main() { int i,j; while(~scanf("%d%d",&n,&m),m+n) { t=1; getchar(); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%c",&str[i][j]); if(str[i][j]=='@') { X=i; Y=j; } } getchar(); } str[X][Y]='#'; BFS(X,Y); printf("%d\n",t); } return 0; }
原文地址:http://blog.csdn.net/holyang_1013197377/article/details/41145853