Sample Input1 6 7 ....... ..***.. ..*..*. ..*..*. ...**.. ....... Sample Input2 5 7 ....... ..***.. ..*.*.. ..***.. .......
Sample Output1 4 Sample Output2 1
#include<stdio.h> #include<queue> #include<string.h> using namespace std; const int N = 105 ; struct node { int x,y; }; char mapt[N][N]; int vist[N][N],n,m; int dir[4][2]={0,1,0,-1,1,0,-1,0}; int bfs(int x,int y) { queue<node>q; node now,pre; int ans=1,flag=1; vist[x][y]=1; now.x=x; now.y=y; q.push(now); while(!q.empty()) { pre=q.front(); q.pop(); for(int e=0; e<4; e++) { now.x=pre.x+dir[e][0]; now.y=pre.y+dir[e][1]; if(now.x<0||now.x>=n||now.y<0||now.y>=m) { flag=0; continue; } if(!vist[now.x][now.y]&&mapt[now.x][now.y]=='.') { vist[now.x][now.y]=1; ans++; q.push(now); } } } if(flag==0) ans=0; return ans; } int main() { //int n,m; while(scanf("%d%d",&n,&m)>0) { for(int i=0; i<n; i++) scanf("%s",mapt[i]); int ans=0; memset(vist,0,sizeof(vist)); for(int i=0; i<n; i++) for(int j=0; j<m; j++) if(!vist[i][j]&&mapt[i][j]=='.') ans+=bfs(i,j); printf("%d\n",ans); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u010372095/article/details/46959565