算法:确定由任意路径描绘的两个扇形的形状,然后填充其中一个

发布于 2024-09-02 07:51:21 字数 1001 浏览 9 评论 0原文

注意:对于任何喜欢逻辑问题等的人来说,这都是一个具有挑战性的问题。

考虑一个高度 H 和宽度 W 的矩形二维网格。网格上的每个空间都有一个值,要么 0 12。最初,网格上的每个空间都是 0,除了沿着四个边缘的空间,它们最初是 2

然后考虑相邻(水平或垂直)网格空间的任意路径。该路径以 2 开始,并以不同的 2 结束。路径上的每个空格都是 1

该路径将网格划分为两个由 0 空格组成的“扇区”。有一个对象位于未指定的 0 空间上。不包含该对象的“扇区”必须用 2 完全填充。

定义一个算法,在给定值数组(列表)(01)的情况下,确定必须从 0 变为 2 的空格2),对应于网格中的值,从上到下,然后从左到右。换句话说,数组中索引 0 处的元素包含网格中左上角空间的值(最初为 2)。索引 1 处的元素包含网格中左列(从顶部数第二个)中的空间值,依此类推。索引 H 处的元素包含网格中顶行左数第二行的空间值,依此类推。

一旦算法完成并且空的“扇区”被2完全填充,SAME算法必须足以再次执行相同的过程。第二次(以及后续),路径仍然是从 2 到不同的 2 绘制的,跨越 0 的空间,但是“ grid" 较小,因为被其他 2 包围的 2 无法被路径触及(因为路径沿着 0 的空间>)。

我非常感谢任何能够为我解决这个问题的人。这不必使用特定的编程语言;事实上,伪代码或仅仅英文就足够了。再次感谢!如果您有任何疑问,请发表评论,我会指定需要指定的内容。

NOTE: This is a challenging problem for anybody who likes logic problems, etc.

Consider a rectangular two-dimensional grid of height H and width W. Every space on the grid has a value, either 0 1 or 2. Initially, every space on the grid is a 0, except for the spaces along each of the four edges, which are initially a 2.

Then consider an arbitrary path of adjacent (horizontally or vertically) grid spaces. The path begins on a 2 and ends on a different 2. Every space along the path is a 1.

The path divides the grid into two "sectors" of 0 spaces. There is an object that rests on an unspecified 0 space. The "sector" that does NOT contain the object must be filled completely with 2.

Define an algorithm that determines the spaces that must become 2 from 0, given an array (list) of values (0, 1, or 2) that correspond to the values in the grid, going from top to bottom and then from left to right. In other words, the element at index 0 in the array contains the value of the top-left space in the grid (initially a 2). The element at index 1 contains the value of the space in the grid that is in the left column, second from the top, and so forth. The element at index H contains the value of the space in the grid that is in the top row but second from the left, and so forth.

Once the algorithm finishes and the empty "sector" is filled completely with 2s, the SAME algorithm must be sufficient to do the same process again. The second (and on) time, the path is still drawn from a 2 to a different 2, across spaces of 0, but the "grid" is smaller because the 2s that are surrounded by other 2s cannot be touched by the path (since the path is along spaces of 0).

I thank whomever is able to figure this out for me, very very much. This does not have to be in a particular programming language; in fact, pseudo-code or just English is sufficient. Thanks again! If you have any questions, just leave a comment and I'll specify what needs to be specified.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£冰雨忧蓝° 2024-09-09 07:51:21

在我看来,基本的 洪水填充 算法就可以完成工作:

  • 首先扫描数组找到 0,然后从那里开始洪水填充,用其他数字填充 0 区域,比方说 3 - 这将标记您的“部门”之一。
  • 完成后,再次扫描 0,然后从那里进行洪水填充,这次填充 4
  • 在两次填充期间,您可以检查是否找到了对象;无论您在哪个填充过程中找到它,请记录该数字。
  • 两次填充完成后,检查哪个编号区域中有对象 - 再次淹没该区域,这次返回 0
  • 2 填充其他编号区域,就完成了。

这适用于任何网格配置,只要恰好有两个彼此断开连接的 0 扇区即可;因此,多次重新应用相同的算法就可以了。

编辑:小调整,为您节省一两次洪水填充 -

  • 如果您在第一个洪水填充中没有找到您的对象,您可以假设其他部分有它,所以您只需用 2 重新填充当前数字,并保留其他扇区(因为它已经填充了 0)。
  • 或者,如果您确实在第一个洪水填充中找到了对象,则可以直接用2填充另一个扇区,然后用重新填充第一个扇区>0

Seems to me a basic flood fill algorithm would get the job done:

  • Scan your array for the first 0 you find, and then start a flood fill from there, filling the 0 region with some other number, let's say 3 - this will label one of your "sectors".
  • Once that's done, scan again for a 0, and flood fill from there, filling with a 4 this time.
  • During both of the fills, you can be checking whether you found your object or not; whichever fill you find it during, keep track of that number.
  • After both fills are done, check which numbered region had the object in it - flood fill that region again, back with 0 this time.
  • Flood fill the other numbered region with 2, and you're done.

This'll work for any grid configuration, as long as there are exactly two 0 sectors that are disconnected from each other; so re-applying the same algorithm any number of times is fine.

Edit: Minor tweaks, to save you a flood-fill or two -

  • If you don't find your object in the first flood-fill, you can assume that the other sector has it, so you just re-fill the current number with 2 and leave the other sector alone (since it's already 0-filled).
  • Alternatively, if you do find the object in the first flood-fill, you can directly fill the other sector with 2, and then re-fill the first sector with 0.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文