您的位置:首页 > 其它

poj 2386 Lake Counting

2011-07-27 08:58 323 查看
找到W就从这儿开始广搜,把能连接的W赋成点,广搜到头后,
在找W,再广搜,看能广搜几次
#include<stdio.h>
#include<queue>
using namespace std;
struct node
{
int x,y;
};
char map[100][100];
int n,m;
int dir[8][2]={-1,-1, 0,-1, 1,-1, -1,0, 1,0, -1,1, 0,1, 1,1};
void bfs(int i,int j)
{
int k;
map[i][j]='.';
queue<node>q;
node cur,next;
cur.x=i;
cur.y=j;
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop();
for(k=0;k<8;k++)
{
next.x=cur.x+dir[k][0];//把k误写成i了
next.y=cur.y+dir[k][1];
if(next.x>=0&&next.x<m&&next.y>=0&&next.y<n)
{
if(map[next.x][next.y]=='W')
{
map[next.x][next.y]='.';
q.push(next);
}
}
}
}
}
int main()
{
int i,j,sum=0;
scanf("%d%d",&m,&n);
getchar();
for(i=0;i<m;i++)
gets(map[i]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(map[i][j]=='W')
{
sum++;
bfs(i,j);
}
printf("%d\n",sum);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: