示例填充图片

什么是洪水填充? (What is Flood Fill?)

Flood fill is an algorithm mainly used to determine a bounded area connected to a given node in a multi-dimensional array. It is a close resemblance to the bucket tool in paint programs.

泛洪填充是一种算法,主要用于确定连接到多维数组中给定节点的有界区域。 它与绘画程序中的存储桶工具非常相似。

The most approached implementation of the algorithm is a stack-based recursive function, and that's what we're gonna talk about next.

该算法最常用的实现是基于堆栈的递归函数,这就是我们接下来要讨论的内容。

它是如何工作的? (How does it work?)

The problem is pretty simple and usually follows these steps:

该问题非常简单,通常按照以下步骤操作:

  1. Take the position of the starting point.取得起点位置。
  2. Decide whether you want to go in 4 directions ( N, S, W, E ) or 8 directions ( N, S, W, E, NW, NE, SW, SE ).

    决定要沿4个方向( N,S,W,E )还是沿8个方向( N,S,W,E,NW,NE,SW,SE )移动。

  3. Choose a replacement color and a target color.选择替换颜色和目标颜色。
  4. Travel in those directions.朝那些方向旅行。
  5. If the tile you land on is a target, replace it with the chosen color.如果您着陆的瓷砖是目标,则将其替换为所选颜色。
  6. Repeat 4 and 5 until you've been everywhere within the boundaries.重复4和5,直到到达边界内的任何地方。

Let's take the following array as an example:

让我们以以下数组为例:

The red square is the starting point and the gray squares are the so called walls.

红色正方形是起点,灰色正方形是墙。

For further details, here's a piece of code describing the function:

有关更多详细信息,这是一段描述该功能的代码:

int wall = -1;void flood_fill(int pos_x, int pos_y, int target_color, int color)
{if(a[pos_x][pos_y] == wall || a[pos_x][pos_y] == color) // if there is no wall or if i haven't been therereturn;                                              // already go backif(a[pos_x][pos_y] != target_color) // if it's not color go backreturn;a[pos_x][pos_y] = color; // mark the point so that I know if I passed through it. flood_fill(pos_x + 1, pos_y, color);  // then i can either go southflood_fill(pos_x - 1, pos_y, color);  // or northflood_fill(pos_x, pos_y + 1, color);  // or eastflood_fill(pos_x, pos_y - 1, color);  // or westreturn;}

As seen above, my starting point is (4,4). After calling the function for the start coordinates  x = 4  and  y = 4 , I can start checking if there is no wall or color on the spot. If that is valid i mark the spot with one  "color"  and start checking the other adjacent squares.

如上所述,我的出发点是(4,4)。 在调用了开始坐标x = 4y = 4的函数之后,我可以开始检查当场是否没有墙壁或颜色。 如果有效,则用一种“颜色”标记该点,然后开始检查其他相邻的正方形。

Going south we will get to point (5,4) and the function runs again.

向南走,我们将指向(5,4),函数再次运行。

运动问题 (Exercise problem)

I always considered that solving a (or more) problem/s using a newly learned algorithm is the best way to fully understand the concept.

我一直认为,使用新近学习的算法解决一个(或多个)问题是充分理解该概念的最佳方法。

So here's one:

所以这是一个:

Statement:

声明:

In a bidimensional array you are given n number of  "islands" . Try to find the largest area of an island and the corresponding island number. 0 marks water and any other x between 1 and n marks one square from the surface corresponding to island x.

在二维数组中,将为您提供n个“ islands” 。 尝试找到一个岛的最大面积和相应的岛号。 0表示水,1到n之间的其他x表示离岛x对应的表面一个正方形。

Input

输入值

  • n  - the number of islands.

    n-岛屿数。

  • l,c  - the dimensions of the matrix.

    l,c-矩阵的尺寸。

  • the next  l  lines,  c  numbers giving the  l th row of the matrix.

    接下来的l行, c给出矩阵的第l行。

Output

输出量

  • i  - the number of the island with the largest area.

    i-面积最大的岛屿数目。

  • A  - the area of the  i 'th island.

    A-i岛的面积。

Ex:

例如:

You have the following input:

您有以下输入:

2 4 4
0 0 0 1
0 0 1 1
0 0 0 2
2 2 2 2

For which you will get island no. 2 as the biggest island with the area of 5 squares.

为此,您将获得岛屿编号。 2个最大的岛屿,面积为5平方。

提示 (Hints)

The problem is quite easy, but here are some hints:

这个问题很容易,但是这里有一些提示:

1. Use the flood-fill algorithm whenever you encounter a new island.
2. As opposed to the sample code, you should go through the area of the island and not on the ocean (0 tiles).

翻译自: https://www.freecodecamp.org/news/flood-fill-algorithm-explained-with-examples/

示例填充图片

示例填充图片_用示例解释洪水填充算法相关推荐

  1. 示例填充图片_填充内容:工具,技巧和动态示例

    示例填充图片 随着浏览器中的设计变得越来越流行(和方便),对有用的设计工具的需求也在增加. 一组非常有用的工具围绕着填料含量. 当前,存在各种资源来帮助需要填充内容的设计人员,无论是文本还是图像. 由 ...

  2. 详细的easyExcel填充数据填充图片及导出示例

    添加依赖 <dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</art ...

  3. 填充图片颜色计算机,美化图表之填充图表区背景与设置图表填充色——想象力电脑应用...

    在前面的课程介绍里面,我们为大家介绍了图表的制作方法及图表元素的添加等功能.但对于图表的背景,却仍然还是一片软件默认的白色,为了使我们制作的图表更加美观,这里我们接着为大家介绍一下图表的美化方法,学会 ...

  4. python label显示图片_高大上的YOLOV3对象检测算法,使用python也可轻松实现

    继续我们的目标检测算法的分享,前期我们介绍了SSD目标检测算法的python实现以及Faster-RCNN目标检测算法的python实现以及yolo目标检测算法的darknet的window环境安装, ...

  5. python画红色填充三角形_用单独的颜色填充Matplotlib三元组中的三角形

    您要查找的功能包含在^{}中.在 从其文档中,您将看到它是"smart",并尝试猜测您是否为点或三角形指定了颜色:The next argument must be C, the ...

  6. 入门级图论算法:洪水填充算法

    洪水填充算法_洪水填充(Flood fill)算法 洪水填充算法_洪水填充(Flood fill)算法_滨封的博客-CSDN博客 洪水覆盖算法(Flood Fill):颜色填充 洪水覆盖算法(Floo ...

  7. 锦标赛排序、洪水填充算法、平衡规划

    锦标赛排序 锦标赛排序(胜者树,记录胜者) 锦标赛排序(胜者树,记录胜者) - Class Xman - 博客园 锦标赛排序(胜者树,记录胜者)_继续微笑lsj-CSDN博客_锦标赛排序 数据结构之树 ...

  8. 像素颜色JavaFX示例--简易图片处理工具

    文章结束给大家来个序程员笑话:[M] 声明:   本博客文章原创类别的均为个人原创,版权所有.载转请注明出处: http://blog.csdn.net/ml3947,另外本人的个人博客:http:/ ...

  9. python使用requests库下载单张图片的简单示例

    python使用requests库下载单张图片的简单示例 简要说明 代码示例 简要说明 下载图片的核心代码如下: res = requests.get(url=img_url, headers=hea ...

最新文章

  1. 基于颜色特征,形状特征和纹理特征的数字图像的检索(Digital Image Retrieval)MATLAB GUI实现
  2. [洛谷P4174][NOI2006]最大获利
  3. python ssh模块_python paramiko模块(ssh) 使用
  4. Redis:12---有序集合对象
  5. clickhouse安装教程
  6. Office与Visio同事安装兼容问题
  7. 网页布局02 盒子模型
  8. C++ 已知两点坐标和半径求圆心坐标程序
  9. 汉字区位码查询与算法
  10. 低版本向日葵本机识别码和验证码提取
  11. windows自带日语输入法快捷键
  12. Flutter 开发——识别iOS设备
  13. HTML5基础标签有哪些,HTML5基础标签
  14. autojs脚本通用ui模板解决了一些已知问题
  15. 银行核心系统和银行信贷系统【杭州多测师】【杭州多测师_王sir】
  16. 使用PlantUml插件画类图
  17. 企业职工工资在线管理信息系统【生产实习课设报告】
  18. 威联通QNAP使用acme.sh工具自动续签到期SSL证书
  19. AI仅用30天就研发出潜在抗癌新药,CRO企业已被AI占领
  20. Android--解决EditText放到popupWindow中,原有复制、粘贴、全选、选择功能失效问题

热门文章

  1. 性能进阶:使用JMeter进行websocket测试【建议收藏】
  2. C#插件开发之带控件的插件开发(基础篇)
  3. 有料科普 | 三峡大坝怎么过船,无用但能吹牛皮的知识又增加了
  4. JAVA毕业设计公交线路查询系统计算机源码+lw文档+系统+调试部署+数据库
  5. Win11如何录屏?压箱底的录屏工具分享给你
  6. cluster(1)
  7. ref修改dom样式遇到的问题
  8. 星宸科技SSD202D芯片+无线投屏协议在摩托车智能仪表,电动车智能仪表批量出货。
  9. 数独游戏-C语言实现
  10. 2018千元内的UGP VR一体机开箱评测:ugp vr一体机怎么样真的好吗?