搜索_Lake Counting

source

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

DFS Solution

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
 char garden[102][102];
int n,m;
void dfs(int i,int j)
{
    if(i<1||j<1||i>n||j>m) return ;
    garden[i][j]='.';
    for(int dx=i-1;dx<=i+1;dx++)
        for(int dy=j-1;dy<=j+1;dy++)
        {
              if(garden[dx][dy]=='W')
              {
                  dfs(dx,dy);
              }

        }


}
int main()
{

    int sum=0;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        cin>>garden[i][j];
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            if(garden[i][j]=='W')
            {
                dfs(i,j);
                sum++;
            }

        }
        printf("%d",sum);

}

BFS Solution

#include<cstdio>
#include<queue>
using namespace std;
int main()
{
    char maze[102][102];
    int n,m,sum=0;
    pair<int,int> p;
    queue<pair<int,int> > myqueue;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%s",maze[i]);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(maze[i][j]=='W')
            {
                maze[i][j]='.';

                myqueue.push(pair<int,int> (i,j));
                while(!myqueue.empty())
                {
                    p=myqueue.front();
                    myqueue.pop();
                    for(int dx=p.first-1;dx<=p.first+1;dx++)
                        for(int dy=p.second-1;dy<=p.second+1;dy++)
                        {
                            if(dx>-1&&dy>-1&&dx<n&&dy<m&&(maze[dx][dy]=='W'))
                            {
                                maze[dx][dy]='.';
                                myqueue.push(pair<int,int> (dx,dy));

                            }
                        }
                }
                sum++;
            }
        }
    }
    printf("%d",sum);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 恍然瞬间 你我态度已变 不再计算从前 只是 感 慨 万 千 那年 青涩少年 那时的 怀春想念 都有一个纯真容颜 渴...
    Rivoun阅读 244评论 4 1
  • 1月意识到李笑来的专栏虽然知识量密度不大,但是没有无用的,要仔细消化,运用到实际中,还是需要进行再消化。 于是开始...
    施二聿行阅读 178评论 0 1
  • 知道某个合乎道德 顺应心理 默默的碰见 无喧哗的贴切 从来没有过的沉稳 这些无谓的挣扎 理性的让好宝宝继续做个好人...
    亦柔阅读 104评论 0 0
  • 潭水不流暗自清 你说时光不老我们不散 又是暮春草长江南季 桃花盛开又败一地 青草深处夏虫鸣 满载斑斓梦前宵 荷花垂...
    鱼筱悦阅读 437评论 0 1