poj 2386 bfs

来源:互联网 发布:macbookair软件下载 编辑:程序博客网 时间:2024/05/16 01:08

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;}
原创粉丝点击