我搜索了很多迷宫算法收集网站,但没有一个能满足我需要的以下条件。
为了明确,我需要一个无限迷宫生成算法,其遵循以下规则:
- 生成一个“完美迷宫”,即:
- 二维和基于网格
- 每个网格都是空格或墙壁
- 每两个空格之间都有一条路径,且只有一条路径
- 没有2x2的正方形全是空格/墙壁(使其看起来漂亮)
- 可以提供类似于
f(s, x, y)
的函数,其中s
用于随机种子
或类似的东西- 返回
(x, y)
处的网格类型 - 对于0~(32768或其他)范围内的不同
s
,给出不同的结果
- 返回
- 无限(可能受到64位整数的限制)
- 允许额外的空间(我的意思是在程序中)
澄清:
- 这里的无限含义:类似于这样的东西
function f(s, x, y){
// for each x,y it gives a result, so we consider it "infinite"
return (s*x+y)%32768<30 ? "wall" : "space";
}
这是一个有限算法(满足1)
init: filled with walls
choose a grid, tag it and add it to list
while (list is not empty)
{
choose <x> randomly from the list
delete <x> from the list
if (<x> is tagged)
{
delete <x>
continue
}
tag <x>
if(number of surrounding walls ≤ 1)
{
add 4 surrounding walls to list
}
}
某些满足第1点和第3点的内容 我们从Eller算法开始
it is generated row by row, and saves a set of regions
first row: all regions into a set, wall between regions (randomly)
while (not last row) {
foreach (neighbour regions) {
if (not in the same set) {
break their wall and merge the regions (randomly)
}
}
foreach (region) {
break the wall below(randomly,and at least one does this)
}
generate next row
for this row: {
foreach (region) {
if (connected with above) {
merge to prev-set
}
}
throw away prev-prev-set
}
}
last row: connect all regions that are not in the same set
如果我们从中心开始逐圈生成,它可以是无限的;不幸的是,我们有规则2。