- 入门级bfs求连通块
- 两个技巧:一重循环遍历至四周;打标记;
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstdlib>
using namespace std;
int m,n,ans,xx[8]={-1,-1,-1,0,0,1,1,1},yy[8]={-1,0,1,1,-1,1,-1,0};
struct node{
int x,y;
};
char oil[105][105];
bool inq[105][105];
void gt()
{
int x,y;
queue<node>q;
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(oil[i][j]=='@'&&!inq[i][j])
{
ans++;
inq[i][j]=1;
q.push({i,j});
while(!q.empty())
{
node t=q.front();
q.pop();
for(int i=0;i<=7;i++)
{
x=t.x+xx[i];
y=t.y+yy[i];
if(x>=1&&x<=m&&y>=1&&y<=n&&!inq[x][y]&&oil[x][y]=='@')
{
q.push({x,y});
inq[x][y]=1;
}
}
}
}
}
}
}
void intt()
{
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
inq[i][j]=0;
ans=0;
}
int main()
{
while(cin>>m>>n&&m&&n)
{
for(int i=1;i<=m;i++)
{
getchar();
for(int j=1;j<=n;j++)
cin>>oil[i][j];
}
gt();
cout<<ans<<endl;
intt();
}
return 0;
}