洪水填充算法 [英] Flood Fill Algorithms

查看:423
本文介绍了洪水填充算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再其上周末,这意味着我与我的业余爱好项目玩。

Its the weekend again, and that means I get to play with my hobby project.

我已经得到了累了创建测试水平由手,所以我想我会采取从发动机的研制工作在一个关卡编辑器突破:

I've gotten tired of creating test levels by hand, so I thought I'd take a break from engine development and work on a level editor:

我想实现洪水填充算法在编辑器中,这将工作就像在画图程序。有没有人有任何指针什么技术将工作适合我吗?

I'd like to implement a flood fill algorithm in the editor, which would work just like in a paint program. Does anyone have any pointers on what technique would work good for me here?

该水平只是一个二维数组,因此它可以被认为是同一个位图真的。

The level is just a 2d array, so it could be considered the same as a bitmap really.

谢谢!

推荐答案

维基百科的文章是pretty的好。只要你的网格很小,公正的东西会工作。

The Wikipedia article is pretty good. As long as your grids are small, just about anything will work.

今年秋季早些时候我做了一些洪水填充10万像素的扫描图像。 (问题是,从已被扫描上影印书页除去黑边)。在这种情况下,只有两个颜色,因此我基本上处理的问题像在无向图中搜索,与连接到它的邻居沿各像素四个罗盘方向。我保持着单独的位图跟踪哪些像素访问过

Earlier this fall I did some flood filling on 10 megapixel scanned images. (The problem was to remove black edges from book pages that had been scanned on a photocopier.) In that case there are only two colors so I essentially treated the problem like a search in an undirected graph, with each pixel connected to its neighbors along the four compass directions. I maintained a separate bitmap to keep track of which pixels had been visited.

主要研究结果

  • 不要尝试递归深度优先搜索。你真的想要一个明确的数据结构。

  • Don't try recursive depth-first search. You really want an explicit data structure.

这是辅助的队列中使用较少的空间比一摞。关于四十倍更小的空间。换句话说,preFER广度优先搜索深度优先搜索。

An auxiliary queue uses much less space than a stack. About forty times less space. In other words, prefer breadth-first search to depth-first search.

此外,这些研究结果只适用于电网与多个百万像素。在一个漂亮的小格子像你的问题,任何简单的算法应该所示。

Again, these findings apply only to grids with multiple megapixels. On a nice small grid like the one shown in your question, any simple algorithm should work.

这篇关于洪水填充算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆