首页 > 其他分享 >POJ--2386 Lake Counting(DFS)

POJ--2386 Lake Counting(DFS)

时间:2022-12-26 00:33:36浏览次数:59  
标签:.. -- MAX Lake dfs WW field DFS Farmer

记录
23:18 2022-12-25

http://poj.org/problem?id=2386

reference:《挑战程序设计竞赛(第2版)》2.1.4 p33

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,需要注意的是把思维打开,dfs是一种思想,不单单是图的专属。

也可以看成是具有不相交集合的图(意思是有孤立的没和其它图有连线),这样dfs图需要照顾到每个集合(也就是将有边的图走一遍)
上面这一句说了这么多,简单概括就是求有几个孤立图的时候的做法(我废话真多)

图中的dfs是找存在的边,这里需要看存在的边是四周八个方向,依次dfs八个方向,将存在W的地方变成.(.表示土地,表示已经遍历过了),这样最后遍历的次数就是孤立图的个数。

#include<cstdio>
int N, M;
#define MAX_N 10000
#define MAX_M 10000
char field[MAX_N][MAX_M + 1];

//现在的位置是x, y
void dfs(int x, int y) {
    field[x][y] = '.'; // 将现在的位置替换为.

    //遍历8个方向
    for (int dx = -1; dx <= 1; dx++) {
        for (int dy = -1; dy <= 1; dy++) {
            //移动的结果是nx,ny
            int nx = x + dx, ny = y + dy;
            //判断nx,ny是否再园子里且是否有积水
            if (0 <= nx && nx < N && 0 <= ny && ny < M && field[nx][ny] == 'W' ) dfs(nx, ny);
        }
    }
    return ;
}

void solve() {
    int res = 0;
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < M; j++){
            if(field[i][j] == 'W') {
                //从有W的地方开始dfs
                dfs(i, j);
                res++;
            }
        }
    }
    printf("%d\n", res);
}

int main() {
    while (~scanf("%d %d\n", &N, &M)) {
        for(int i = 0; i < N; i++) {
            for(int j = 0; j < M; j++){
                scanf("%c", &field[i][j]);
            }
            getchar();
        }
        solve();
    }
    // while(~scanf("%d %d",&N,&M)){
    //     for(int i=0;i<=N-1;i++){   
    //         scanf("%s",field[i]);
    //     }
    //     solve();
    // }
}

标签:..,--,MAX,Lake,dfs,WW,field,DFS,Farmer
From: https://www.cnblogs.com/57one/p/17004884.html

相关文章

  • 使用键值对数组构造的无重复随机数算法
    --@paramlist_length生成的数组长度--@parammax_random_length随机数的最大范围math.generate=function(list_length,max_random_length) localrandom={}......
  • vue中$attrs
    $attrs用来保存给子组件绑定了,但子组件没有接收的数据,如下://父组件...<Schoolv-bind="{a,b,c}"></School>...//子组件School...props:['a','b']...打印子......
  • 一个简单的rust字符串时钟
    1、简介  用rust写的一个简单的练手的demo,一个字符串时钟,在终端用字符串方式显示当前时间。本质是对图片取灰度,然后每个像素按灰度门限用星号代替灰度值,就把图片变为由......
  •     包将有联系的模块组织在一起,即放在同一个文件夹下,并且在这个文件夹床架一个名字为__init__.py文件,那么这个文件夹就称之为包。1、制作包[New]-->[Pyt......
  • vue.delete 删除数组
    删除数组指定索引的元素可以采用delete,Array.splice和vue.$delete三种方法如下所示:leta=[1,2,3,4,5]letb=[1,2,3,4,5]letc=[1,2,3,4,5]deletea[2]b.......
  • 在同一台主机启动多个FreeSWITCH实例
    本文更新于2022-04-23,使用FreeSWITCH1.10.7。假设已经在/usr/local/freeswitch安装FreeSWITCH,并在此目录启动一个实例。现需在另一个目录(笔者使用/usr/local/freeswitch2......
  • Python小程序实现
    Python小程序实现标签(空格分隔):python目录Python小程序实现1,16进制IP地址转换为10进制2,10进制IP地址转换为16进制3,16进制字符串转换为10进制1,16进制IP地址转换为10进制......
  • [Smplify环境配置]Ubuntu 22.04+Python 3.9+pycharm 2022.3 配置Smplify
    最近在做单目3d人体姿态估计的相关项目,看了一些基于smpl的论文,smplify是smpl人体模型比较早期的应用,想着复现了研究一下,但是smplify是基于python2.7编写的,这里结合网上很......
  • [Contest]2022 sti2百度搜索首届技术创新挑战赛赛道二比赛复盘总结
    2022sti2比赛复盘总结前期进行了PaddleInference的配置探索,后期经过了Paddleslim的transformer裁剪+蒸馏的策略上分。侥幸进入了决赛,感觉还是偏运气,感觉自己在比赛技术......
  • Djiango:csrf跨站请求伪造
    目录一、csrf跨站请求伪造1.csrf简介2.模拟钓鱼网站案例二、csrf相关校验策略1.form的csrf策略2.ajax请求csrf策略三、csrf相关视图验证1.FBV添加csrf校验装饰器2.CBV添加c......