POJ 2386 Lake Counting

来源:互联网 发布:网页制作软件免费下载 编辑:程序博客网 时间:2024/06/07 15:40
Lake Counting
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 22854 Accepted: 11526
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

深搜求 有多少个连通块,简单。

#include <iostream>#include <cstdio>#include <queue>#include <cstring>using namespace std;int vis[105][105];char map[105][105];int sum;int n,m;int x[5]={-1,0,1};int y[5]={-1,0,1};void dfs(int h,int w){   // if(h>n||w>m||h<1||w<1) return ;    map[h][w]='.';    for(int i=0;i<3;i++)    {        for(int j=0;j<3;j++)        {            if(h+x[i]>0&&h+x[i]<=n&&w+y[j]>0&&w+y[j]<=m&&map[h+x[i]][w+y[j]]=='W')            dfs(h+x[i],w+y[j]);        }    }}int main(void){   // freopen("B.txt","r",stdin);   // int  n,m;   scanf("%d%d",&n,&m);    sum=0;   // memset(vis,0,sizeof(vis));   for(int i=1;i<=n;i++)   {       getchar();       for(int j=1;j<=m;j++)       {           scanf("%c",&map[i][j]);          // if(map[i][j]=='.')           // vis[i][j]=1;       }   }  // dfs(1,1); /* for(int i=1;i<=n;i++)  {      for(int j=1;j<=m;j++)      {          printf("%c",map[i][j]);      }      printf("\n");  }*/  for(int i=1;i<=n;i++)  {      for(int j=1;j<=m;j++)      {          if(map[i][j]=='W')          {             // printf("haha\n");              sum++;              dfs(i,j);          }      }  }    printf("%d\n",sum);    return 0;}


0 0
原创粉丝点击