poj 2386 bfs

Description

Due to recent rains, water has pooled in various places in Farmer John’s field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water (‘W’) or dry land (‘.’). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.

Given a diagram of Farmer John’s field, determine how many ponds he has.
Input

  • Line 1: Two space-separated integers: N and M

  • Lines 2..N+1: M characters per line representing one row of Farmer John’s field. Each character is either ‘W’ or ‘.’. The characters do not have spaces between them.
    Output

  • Line 1: The number of ponds in Farmer John’s field.
    Sample Input

10 12
W……..WW.
.WWW…..WWW
….WW…WW.
………WW.
………W..
..W……W..
.W.W…..WW.
W.W.W…..W.
.W.W……W.
..W…….W.
Sample Output

3
Hint

OUTPUT DETAILS:

There are three ponds: one in the upper left, one in the lower left,and one along the right side.

第一次尝试这种写法 orz

#include <iostream> 
#include <cstdio> 
#include <fstream> 
#include <algorithm> 
#include <cmath> 
#include <deque> 
#include <vector> 
#include <queue> 
#include <string> 
#include <cstring> 
#include <map> 
#include <stack> 
#include <set>

using namespace std;
#define ll long long
#define PQ priority_queue
#define MP make_pair
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define PRF first
#define PRS second
#define fr(x) freopen(x,"r",stdin)
#define fw(x) freopen(x,"w",stdout)
#define cls(ar,val) memset ( ar, val, sizeof ( ar ) )
#define debug(a) cerr<<#a<<"=="<<a<<endl
#define lp(loop,start,end) for ( int loop = start; loop < end; ++loop )
#define lpd(loop,start,end) for ( int loop = start; loop > end; --loop )
#define lpi(loop,start,end) for ( int loop = start; loop <= end; ++loop )
#define lpdi(loop,start,end) for ( int loop = start; loop >= end; --loop )
#define qmax(a,b) (((a)>(b))?(a):(b))
#define qmin(a,b) (((a)<(b))?(a):(b))
#define qabs(a) (((a)>=0)?(a):(-(a)))
const int INF = 0x3fffffff;
const int SINF = 0x7fffffff;
const long long LINF = 0x3fffffffffffffff;
const long long SLINF = 0x7fffffffffffffff;
const int maxn=100+10;
int n,m;
char mp[maxn][maxn];
void print() {
    lp(i,0,n) {
        lp(j,0,m) {
            printf("%c",mp[i][j]); 
        }
        printf("\n");
    }
}
//int check(int x,int y) {
//  if(x<0||x>=n-1||y<0||y>m-1) return 0;
//  return 1;
//}
void dfs(int x,int y) {
    mp[x][y]='.';
    for(int i=-1;i<=1;i++) {
        for(int j=-1;j<=1;j++) {
            int nx=x+i;int ny=y+j;
            if(x>=0&&x<n&&y>=0&&y<m&&mp[nx][ny]=='W') {
                dfs(nx,ny);
            }   
        }
    }
//  print();
//  printf("*************\n");
    return ;
}

void input () {
    // input method
    lp(i,0,n) {
            scanf("%s",mp[i]);
    }
//  print();
}
void work () {
    // main work
    int ans=0;
    lp(i,0,n) {
        lp(j,0,m) {
            if(mp[i][j]=='W') {
                dfs(i,j);
                ans++;
            }
        }
    }
    printf("%d\n",ans);
}
int main() {
//  fr("input.txt");
    while(scanf("%d%d",&n,&m)!=EOF) {
        input();
        work(); 
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值