6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
45 59 6 13
#include<stdio.h> #include<queue> #include<iostream> using namespace std; char mat[111][111]; int m,n,cnt; int dx[]={1,-1,0,0}; int dy[]={0,0,1,-1}; void dfs(int x,int y) { if(mat[x][y]=='#'||x<1||y<1||x>n||y>m)return; mat[x][y]='#'; cnt++; for(int i=0;i<4;i++) { dfs(x+dx[i],y+dy[i]); } } int main() { int sx,sy; while(~scanf("%d%d",&m,&n),(m+n)) { cnt=0; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { scanf(" %c",&mat[i][j]); if(mat[i][j]=='@')sx=i,sy=j; } dfs(sx,sy); cout<<cnt<<endl; } }
#include<stdio.h> #include<queue> #include<iostream> using namespace std; struct point { int x,y; }st; queue<point>q; char mat[111][111]; int m,n,cnt; int dx[]={1,-1,0,0}; int dy[]={0,0,1,-1}; void bfs(int x,int y) { while(!q.empty())q.pop(); q.push(st); mat[st.x][st.y]='#'; while(!q.empty()) { st=q.front(); q.pop(); cnt++; for(int i=0;i<4;i++) { point next=st; next.x+=dx[i],next.y+=dy[i]; if(mat[next.x][next.y]=='#'||next.x<1||next.y<1||next.x>n||next.y>m)continue; q.push(next); mat[next.x][next.y]='#'; } } } int main() { int sx,sy; while(~scanf("%d%d",&m,&n),(m+n)) { cnt=0; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { scanf(" %c",&mat[i][j]); if(mat[i][j]=='@')st.x=i,st.y=j; } bfs(sx,sy); cout<<cnt<<endl; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。