python - 使用 Prim 算法创建 'hard' 迷宫

标签 python maze prims-algorithm

我正在使用 Prim 算法创建迷宫。我已经成功地做到了,但我现在正试图通过改变它选择要添加到迷宫中的潜在细胞的方式来让它变得“更难”。在我看来,“困难”介于两个极端之间:

Extreme #1 是完全随机选择潜在 channel 列表中的单元格,其中每个分支以大致相等的速度发展。这有很多不同的分支,但是一旦到达原点,您几乎可以沿着直线前往所需位置。这是一张显示这种方法的图片:

enter image description here

Extreme #2 是选择最后添加到列表的地方,创建一个漫长、乏味、简单的迷宫。当您只选择放入潜在 channel 列表的最后一项时,它就会形成。这是一张显示这种方法的图片:

enter image description here

我试图通过对最近放置的单元格进行优先排序来对此进行平衡,但是很难创建分支,正如在第一个中看到的那样,但仍然有一条路径可以绕过整个迷宫。

尝试这样做的最有趣的方法是当我尝试有 50% 的机会放置最后一个 block 时,如果失败,则有 50% 的机会放置下一个,依此类推。然而,我把它搞砸了,并尝试先做 [-0] 的索引,有 50% 的机会添加第一个 block ,然后是最后一个,然后是倒数第二个,依此类推。这创建了一个有趣的迷宫,但当我“修复”它时,迷宫看起来很像第二个极端。

我尝试的另一种方法是我的代码中使用的方法:

for i in range(1, len(potential_passage_list) + 1):
        if randint(0, int(len(passage_list) / 50)) == 0:
            maze_passage(potential_passage_list[-i][0], potential_passage_list[-i][1])

这是为了尝试并有合理的可能性将一个 block 添加到 potential_passage_list 之前放置。

所以,我的问题是,您如何创建一个“硬”迷宫,其中包含许多分支,但模式不可预测?可以使用什么算法来做到这一点?

我正在使用 python 3 和 pygame 库来显示所有内容。

这是我的代码,如果你能理解的话:

import pygame
from random import shuffle, randint

# variables
######
# changeable variables
cell_size = 7  # cannot be less than 3
maze_length = 160 * cell_size + 1
maze_height = 100 * cell_size + 1
######

# colours
black = (0, 0, 0)
white = (245, 245, 245)
red = (255, 0, 0)
blue = (0, 0, 255)

# other variables
passage_list = []
potential_passage_list = []
impossible_passage = []
random_cell = []
done = False

# initialize pygame and display screen
pygame.init()
screen = pygame.display.set_mode((maze_length, maze_height))
pygame.display.flip()


def one_connection(cell_x, cell_y):
    # ensure that it will only touch one passage
    count = 0

    if [cell_x + cell_size, cell_y] in passage_list:
        count += 1
    if [cell_x - cell_size, cell_y] in passage_list:
        count += 1
    if [cell_x, cell_y + cell_size] in passage_list:
        count += 1
    if [cell_x, cell_y - cell_size] in passage_list:
        count += 1

    if count <= 1:
        return True
    else:
        return False


def valid_cell(cell_x, cell_y):
    # check if already in potential_passage_list
    if [cell_x, cell_y] in potential_passage_list:
        impossible_passage.append([cell_x, cell_y])
    # check if in impossible list
    elif [cell_x, cell_y] in impossible_passage:
        impossible_passage.append([cell_x, cell_y])
    # check if out of boundary
    elif cell_x < 0 or cell_x >= maze_length - cell_size or cell_y < 0 or cell_y >= maze_height - cell_size:
        impossible_passage.append([cell_x, cell_y])
    # ensure that it will only touch one passage
    elif not one_connection(cell_x, cell_y):
        impossible_passage.append([cell_x, cell_y])
    # check if it isolates any walls / cut off unconnected corners
    elif (([cell_x + cell_size, cell_y + cell_size] in passage_list and [cell_x + cell_size, cell_y] not in
           passage_list and [cell_x, cell_y + cell_size] not in passage_list) or
          ([cell_x + cell_size, cell_y - cell_size] in passage_list and [cell_x + cell_size, cell_y] not in
           passage_list and [cell_x, cell_y - cell_size] not in passage_list) or
          ([cell_x - cell_size, cell_y + cell_size] in passage_list and [cell_x - cell_size, cell_y] not in
           passage_list and [cell_x, cell_y + cell_size] not in passage_list) or
          ([cell_x - cell_size, cell_y - cell_size] in passage_list and [cell_x - cell_size, cell_y] not in
           passage_list and [cell_x, cell_y - cell_size] not in passage_list)):

        impossible_passage.append([cell_x, cell_y])
    # check if already in passage_list
    elif [cell_x, cell_y] not in passage_list:
        return True


# functions
def maze_passage(cell_x, cell_y):
    # reset block_passage_list
    block_passage_list = []

    # remove from list so it does not interfere with valid_cell procedure
    potential_passage_list.remove([cell_x, cell_y])
    if valid_cell(cell_x, cell_y):
        # display rectangle
        pygame.draw.rect(screen, white, [cell_x, cell_y, cell_size, cell_size])
        pygame.display.update()

        passage_list.append([cell_x, cell_y])

        # add valid walls to block_passage_list
        if valid_cell(cell_x + cell_size, cell_y):
            block_passage_list.append([cell_x + cell_size, cell_y])
        if valid_cell(cell_x - cell_size, cell_y):
            block_passage_list.append([cell_x - cell_size, cell_y])
        if valid_cell(cell_x, cell_y + cell_size):
            block_passage_list.append([cell_x, cell_y + cell_size])
        if valid_cell(cell_x, cell_y - cell_size):
            block_passage_list.append([cell_x, cell_y - cell_size])

        shuffle(block_passage_list)

        for j in block_passage_list:
            potential_passage_list.append(j)


# create initial cell
start_cell = [randint(0, int(maze_height / cell_size))*cell_size, randint(0, int(maze_height / cell_size))*cell_size]
potential_passage_list.append([start_cell[0], start_cell[1]])


# loop for creating maze
while not done:
    for event in pygame.event.get():
        # exit screen when exit pressed in pygame
        if event.type == pygame.QUIT:
            done = True

    # select cell
    for i in range(1, len(potential_passage_list) + 1):
        if randint(0, int(len(passage_list) / 50)) == 0:
            maze_passage(potential_passage_list[-i][0], potential_passage_list[-i][1])
            break

    # check if maze completion finished
    if not potential_passage_list:
        # create start and end
        passage_list.sort()
        pygame.draw.rect(screen, red, [passage_list[0][0] + 1, passage_list[0][1] + 1, cell_size - 2, cell_size - 2])
        pygame.draw.rect(screen, blue, [passage_list[-1][0] + 1, passage_list[-1][1] + 1, cell_size - 2, cell_size - 2])
        pygame.display.update()

请随意使用我的代码,试用它,并分享您发现的有效代码。

谢谢!

最佳答案

我喜欢使用 Kruskal 算法并指定不同的选择权重来删除不同配置中的边,而不是优先考虑新单元格和旧单元格。

这让您可以创建具有各种不同特征的迷宫。您可以在此处尝试演示:https://mtimmerm.github.io/webStuff/maze.html

如果您喜欢扩展现有路径的选项( slider 1、2 和 3),它会使迷宫变得更难。

关于python - 使用 Prim 算法创建 'hard' 迷宫,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53887926/

相关文章:

algorithm - Prim 算法的最坏情况图

C++,当从列表中获取值时,我填充其中的数组没有返回任何内容

algorithm - Prim 和 Kruskal 的应用,而不是寻找 MST

python - CrawlerProcess 中的多个蜘蛛 - 如何获取每个蜘蛛的日志?

python - 在 Pandas 中将分类变量重新编码为整数

python - Http POST 重音编码

python - PyGTK 和 Visual-Python

algorithm - 在有地雷和有限生命的迷宫中找到最短路径

c++ - 找出迷宫解算器的最佳解决方案,带动画输出

c - 如何优化我的递归迷宫求解器?