Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<queue>
- #include<stdio.h>
- using namespace std;
- struct Node
- {
- int r,c;
- };
- queue<Node>Q;
- bool vst[107][107];
- int dx[]={0,0,1,1,1,-1,-1,-1};
- int dy[]={1,-1,0,1,-1,0,1,-1};
- char Grid[107][107];
- int Bfs(Node S)
- {
- int i;
- vst[S.r][S.c]=1;
- Q.push(S);
- Node u,v;
- while(!Q.empty())
- {
- u=Q.front();
- Q.pop();
- for(i=0;i<8;i++)
- {
- v=u;
- v.r+=dx[i];
- v.c+=dy[i];
- if(Grid[v.r][v.c]!='@')continue;
- if(!vst[v.r][v.c])
- {
- Q.push(v);
- vst[v.r][v.c]=1;
- }
- }
- }
- }
- int main()
- {
- int m,n,i,j;
- while(scanf("%d%d",&m,&n)==2)
- {
- if(!m &&!n)break;
- for(i=1;i<=m;i++)
- {
- scanf("%s",Grid[i]+1);
- // printf("%s\n",Grid[i]+1);
- }
- int ans=0;
- for(i=1;i<=m;i++)
- {
- for(j=1;j<=n;j++)
- {
- if(!vst[i][j]&&Grid[i][j]=='@')
- {
- Node S;
- S.r=i;
- S.c=j;
- Bfs(S);
- ans++;
- }
- }
- }
- printf("%d\n",ans);
- ///
- for(i=1;i<=m;i++)
- {
- for(j=1;j<=n;j++)
- {
- vst[i][j]=Grid[i][j]=0;
- }
- }
- ///
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement