POJ2386 Lake Counting BFS

POJ2386 Lake Counting
/*BFS 
0MS   592K	
*/
#include <stdio.h>
#include <utility>//pair<>
#include <queue>
#define x first
#define y second
using namespace std;
int M,N;
const int MAX=105;
const int INF=0x3f3f3f3f;
char m[MAX][MAX];
int next[8][2]={1,0,0,1,-1,0,0,-1,1,1,-1,-1,1,-1,-1,1};
typedef pair<int,int>Point;
void BFS(int &ax,int &ay)
{
    queue<Point>Q;
    Point a(ax,ay);
    Q.push(a);
    m[a.x][a.y]='.';
    while (!Q.empty())
    {
        Point now,then;
        now=Q.front();
        Q.pop();
        for (int k=0;k<8;k++)
        {
            then=now;
            then.x+=next[k][0];
            then.y+=next[k][1];
            if (then.x<0||then.y<0||then.x>=M||then.y>=N||m[then.x][then.y]=='.') continue;
            Q.push(then);
            m[then.x][then.y]='.';
        }
    }
}
int main ()
{
    int ans=0;
    scanf ("%d%d",&M,&N);
    for (int i=0;i<M;i++)
        scanf ("%s",m[i]);
    for (int i=0;i<M;i++)
        for (int k=0;k<N;k++)
            if (m[i][k]=='W')
            {
                BFS(i,k);
                ans++;
            }
    printf ("%d\n",ans);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值