算法:确定由任意路径描绘的两个扇形的形状,然后填充其中一个
注意:对于任何喜欢逻辑问题等的人来说,这都是一个具有挑战性的问题。
考虑一个高度 H 和宽度 W 的矩形二维网格。网格上的每个空间都有一个值,要么 0
1
或 2
。最初,网格上的每个空间都是 0
,除了沿着四个边缘的空间,它们最初是 2
。
然后考虑相邻(水平或垂直)网格空间的任意路径。该路径以 2
开始,并以不同的 2
结束。路径上的每个空格都是 1
。
该路径将网格划分为两个由 0
空格组成的“扇区”。有一个对象位于未指定的 0
空间上。不包含该对象的“扇区”必须用 2
完全填充。
定义一个算法,在给定值数组(列表)(0
、1)的情况下,确定必须从
或 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 2
s, 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 2
s that are surrounded by other 2
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,基本的 洪水填充 算法就可以完成工作:
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:
0
you find, and then start a flood fill from there, filling the0
region with some other number, let's say3
- this will label one of your "sectors".0
, and flood fill from there, filling with a4
this time.0
this time.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 -
2
and leave the other sector alone (since it's already0
-filled).2
, and then re-fill the first sector with0
.