POJ 2386

//160K	16MS
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
struct point{
    int x, y;
};

const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool map[105][105];
bool vis[105][105];
int N, M, res;

inline bool ok(int x, int y){
    if(x >=0 && x < N && y >= 0 && y <M) return true;
    else return false;
}

void bfs(int x, int y){
    queue<point> q;
    point h, n;
    h.x = x, h.y = y;
    q.push(h);
    vis[h.x][h.y] = 1;
    while(!q.empty()){
        h = q.front();
        q.pop();
        for(int i=0; i<8; i++){
            n.x = h.x + dx[i];
            n.y = h.y + dy[i];
            if(ok(n.x, n.y)){
                if(map[n.x][n.y] == 1 && !vis[n.x][n.y]){
                    q.push(n);
                    vis[n.x][n.y] = 1;
                }
            }
        }
    }
}

int main(){
    while(scanf("%d %d", &N, &M) != EOF){
        char str[105];
        for(int i=0; i<N; i++){
            scanf("%s", str);
            for(int j=0; j<strlen(str); j++)
                map[i][j] = (str[j] == 'W' ? 1 : 0);
        }
        memset(vis, 0, sizeof(vis));
        int res = 0;
        for(int i=0; i<N; i++)
        for(int j=0; j<M; j++){
            if(map[i][j] == 1 && !vis[i][j]){
                bfs(i, j);
                res++;
            }
        }
        printf("%d\n", res);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值