教你怎么用Python实现多路径迷宫
作者:明年十八岁 发布时间:2022-03-11 15:07:52
标签:Python,多路径迷宫
一、思路介绍
在已有的单路径迷宫基础上打开一块合适的墙就可以构成2路径的迷宫。
打开的墙不能和已有的路径过近。
1。从开始和终点开始进行广度优先搜索,并为迷宫中的每个单元格记录单元格远离开始和终点的步数。
2。通过将距离开头较近的所有单元格放入 start 集合,并将更接近目标的所有单元格放入end集合来将迷宫分成两个部分。
3。 选择分开两个区域的任意一面墙拆开就可以形成2通路的迷宫。
如想生成最短的通路可以选择相邻格子距离差值最大的那面墙拆开,一般情况下这两条路距离也比较远。
二、图示
三、分区域演示代码
#!/usr/bin/python3.7
# -*- coding: utf-8 -*-
import random
import pygame
#import depth_maze
import maze
#import aldous_broder_maze
pygame.init() # 初始化pygame
size = width, height = 800, 600 # 设置窗口大小
screen = pygame.display.set_mode(size) # 显示窗口
# 颜色
diamond_color_size = 8
COLOR_RED, COLOR_BLUE, COLOR_GREEN, COLOR_YELLOW, COLOR_BLACK, COLOR_GREY, COLOR_GOLDEN, COLOR_NO_DIAMOND = list(range(
diamond_color_size))
COLOR = {
COLOR_RED: (255, 0, 0),
COLOR_BLUE: (0, 0, 255),
COLOR_GREEN: (0, 255, 0),
COLOR_YELLOW: (255, 255, 0),
COLOR_BLACK: (0, 0, 0),
COLOR_GREY: (250, 240, 230),
COLOR_GOLDEN : (255,215,0),
COLOR_NO_DIAMOND: (100, 100, 100),
}
# 格子大小
DIAMOND_LEN = 20
DIAMOND_SIZE = (DIAMOND_LEN, DIAMOND_LEN)
# 蓝格子
DIAMOND=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND.fill(COLOR[COLOR_BLUE])
# 绿格子
DIAMOND_GREEN=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND_GREEN.fill(COLOR[COLOR_GREEN])
# 红格子
DIAMOND_RED=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND_RED.fill(COLOR[COLOR_RED])
# 黄格子
DIAMOND_YELLOW=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND_YELLOW.fill(COLOR[COLOR_YELLOW])
# 灰的格子
DIAMOND_GREY=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND_GREY.fill(COLOR[COLOR_GREY])
# 字体
use_font = pygame.font.Font("FONT.TTF", 16)
use_font12 = pygame.font.Font("FONT.TTF", 12)
# 背景
background=pygame.surface.Surface(size).convert()
background.fill(COLOR[COLOR_BLACK])
# 文字
score_surface = use_font.render("找到终点", True, COLOR[COLOR_BLACK], COLOR[COLOR_GREY])
# 时间
clock = pygame.time.Clock()
##############################################
# 格子访问标记x,y,0,右墙x,y,1,下墙x,y,2
##############################################
#标记
NOWALL=maze.NOWALL # 无墙
WALL=maze.WALL # 有墙
WALL2=maze.WALL2 # 有墙
VISIT=maze.VISIT # 到访过
NOVISIT=maze.NOVISIT # 没到过
VERTICAL = maze.VERTICAL # 垂直的
HORIZONTAL = maze.HORIZONTAL# 水平的
INFINITE = maze.INFINITE # 无穷远
INFINITE = maze.INFINITE # 无穷远
#
def FindNext(pathList, walls, grids, rows, cols):
nextList = [] # 下一步
for node in pathList:
r, c = node
l = grids[r][c]
nl=l+1
# 可以到达的位置
if r>0 and NOWALL == walls[r][c][1] and INFINITE == grids[r-1][c]:
# move = 'u'
nr=r-1
nc=c
if (nr,nc) not in nextList:
nextList.append((nr,nc))
grids[nr][nc] = l+1
if c>0 and NOWALL == walls[r][c][0] and INFINITE == grids[r][c-1]:
# move = 'l'
nr=r
nc=c-1
if (nr,nc) not in nextList:
nextList.append((nr,nc))
grids[nr][nc] = l+1
if c<cols-1 and NOWALL == walls[r][c+1][0] and INFINITE == grids[r][c+1] :
# move='r'
nr=r
nc=c+1
if (nr,nc) not in nextList:
nextList.append((nr,nc))
grids[nr][nc] = l+1
if r<rows-1 and NOWALL == walls[r+1][c][1] and INFINITE == grids[r+1][c] :
# move='d'
nr=r+1
nc=c
if (nr,nc) not in nextList:
nextList.append((nr,nc))
grids[nr][nc] = l+1
return nextList
def draw_diamond(r,c, screen, POSX, POSY, diamod):
px,py=POSX + 1 + (c) * DIAMOND_SIZE[0], POSY + 1 + (r) * DIAMOND_SIZE[1]
# 标记访问过的格子
screen.blit(diamod, (px, py))
return
def draw_diamond_and_str(r,c, screen, POSX, POSY, diamod, use_font, string, color, color_back):
px,py=POSX + 1 + (c) * DIAMOND_SIZE[0], POSY + 1 + (r) * DIAMOND_SIZE[1]
# 标记访问过的格子
screen.blit(diamod, (px, py))
distance_surface = use_font.render(string, True, color, color_back)
screen.blit(distance_surface, (px, py))
return
# Sample algorithm
def multipath_maze_demo(rows, cols):
#walls = maze.aldous_broder_maze(rows, cols)
#walls = maze.depth_maze(rows, cols)
#walls = maze.kruskal_maze(rows, cols)
#walls = maze.prim_maze(rows, cols)
#walls = maze.wilson_maze(rows, cols)
walls = maze.wilson_maze(rows, cols)
POSX=40
POSY=40
# 初始化未访问
grids=[[ INFINITE for i in range(cols)]for j in range(rows)]
# 起点
# 标记迷宫
r=0
c=0
findEndPoint=False
findPath=False
# 起点
startPoint=(r,c)
# 终点
stopPoint=(rows-1,cols-1)
#
mainList=[] # 主路径
beginList=[startPoint]
endList=[stopPoint]
grids[r][c]=0 # 标记已经到过格子距离
grids[stopPoint[0]][stopPoint[1]]=0
# 没有访问过的格子
notUseGrids = []
for tr in range(rows):
for tc in range(cols):
notUseGrids.append((tr,tc))
beginMap=beginList
endMap=endList
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if notUseGrids:
beginNextList = [] # 下一步
for node in beginList:
r, c = node
l = grids[r][c]
# 可以到达的位置
if r>0 and NOWALL == walls[r][c][1] and INFINITE == grids[r-1][c]:
# move = 'u'
nr=r-1
nc=c
if (nr,nc) not in beginNextList:
beginNextList.append((nr,nc))
grids[nr][nc] = l+1
if c>0 and NOWALL == walls[r][c][0] and INFINITE == grids[r][c-1]:
# move = 'l'
nr=r
nc=c-1
if (nr,nc) not in beginNextList:
beginNextList.append((nr,nc))
grids[nr][nc] = l+1
if c<cols-1 and NOWALL == walls[r][c+1][0] and INFINITE == grids[r][c+1] :
# move='r'
nr=r
nc=c+1
if (nr,nc) not in beginNextList:
beginNextList.append((nr,nc))
grids[nr][nc] = l+1
if r<rows-1 and NOWALL == walls[r+1][c][1] and INFINITE == grids[r+1][c] :
# move='d'
nr=r+1
nc=c
if (nr,nc) not in beginNextList:
beginNextList.append((nr,nc))
grids[nr][nc] = l+1
# 下一圈
beginList = beginNextList
beginMap = beginMap + beginNextList
# end
endNextList = [] # 下一步
for node in endList:
r, c = node
l = grids[r][c]
# 可以到达的位置
if r>0 and NOWALL == walls[r][c][1] and INFINITE == grids[r-1][c]:
# move = 'u'
nr=r-1
nc=c
if (nr,nc) not in endNextList:
endNextList.append((nr,nc))
grids[nr][nc] = l+1
if c>0 and NOWALL == walls[r][c][0] and INFINITE == grids[r][c-1]:
# move = 'l'
nr=r
nc=c-1
if (nr,nc) not in endNextList:
endNextList.append((nr,nc))
grids[nr][nc] = l+1
if c<cols-1 and NOWALL == walls[r][c+1][0] and INFINITE == grids[r][c+1] :
# move='r'
nr=r
nc=c+1
if (nr,nc) not in endNextList:
endNextList.append((nr,nc))
grids[nr][nc] = l+1
if r<rows-1 and NOWALL == walls[r+1][c][1] and INFINITE == grids[r+1][c] :
# move='d'
nr=r+1
nc=c
if (nr,nc) not in endNextList:
endNextList.append((nr,nc))
grids[nr][nc] = l+1
# 下一圈
endList = endNextList
endMap = endMap + endNextList
elif findEndPoint and not findPath:
mainList.append((r,c))
l = grids[r][c]
nl=l-1
# 最近的
if r>0 and NOWALL == walls[r][c][1] and nl == grids[r-1][c]:
# move = 'u'
nr=r-1
nc=c
if c>0 and NOWALL == walls[r][c][0] and nl == grids[r][c-1]:
# move = 'l'
nr=r
nc=c-1
beginNextList.append((nr,nc))
if c<cols-1 and NOWALL == walls[r][c+1][0] and nl == grids[r][c+1] :
# move='r'
nr=r
nc=c+1
if r<rows-1 and NOWALL == walls[r+1][c][1] and nl == grids[r+1][c] :
# move='d'
nr=r+1
nc=c
# 找到起点
if 0 == nl:
mainList.append((nr,nc))
findPath = True
r,c=nr,nc
screen.blit(background, (0, 0))
# 格子
for cx in range(cols):
for ry in range(rows):
px,py=POSX + 1 + (cx) * DIAMOND_SIZE[0], POSY + 1 + (ry) * DIAMOND_SIZE[1]
# 标记访问过的格子
if maze.INFINITE == grids[ry][cx]:
draw_diamond(ry, cx, screen, POSX, POSY, DIAMOND)
else:
s = "{}".format(grids[ry][cx])
draw_diamond_and_str(ry, cx, screen, POSX,POSY, DIAMOND_GREY, use_font12, s, COLOR[COLOR_BLACK], COLOR[COLOR_GREY])
# 圈地
for pos in beginMap:
s = "{}".format(grids[pos[0]][pos[1]])
draw_diamond_and_str(pos[0], pos[1], screen, POSX,POSY, DIAMOND_GREEN, use_font12, s, COLOR[COLOR_BLACK], COLOR[COLOR_GREEN])
for pos in endMap:
s = "{}".format(grids[pos[0]][pos[1]])
draw_diamond_and_str(pos[0], pos[1], screen, POSX,POSY, DIAMOND_YELLOW, use_font12, s, COLOR[COLOR_BLACK], COLOR[COLOR_YELLOW])
# 循环外圈
if beginList and not mainList:
for pos in beginList:
s = "{}".format(grids[pos[0]][pos[1]])
draw_diamond_and_str(pos[0], pos[1], screen, POSX,POSY, DIAMOND_RED, use_font12, s, COLOR[COLOR_BLACK], COLOR[COLOR_RED])
for pos in endList:
s = "{}".format(grids[pos[0]][pos[1]])
draw_diamond_and_str(pos[0], pos[1], screen, POSX,POSY, DIAMOND_RED, use_font12, s, COLOR[COLOR_BLACK], COLOR[COLOR_RED])
# 路径
if mainList:
for pos in mainList:
s = "{}".format(grids[pos[0]][pos[1]])
draw_diamond_and_str(pos[0], pos[1], screen, POSX,POSY, DIAMOND_YELLOW, use_font12, s, COLOR[COLOR_BLACK], COLOR[COLOR_YELLOW])
# r,c
px,py=POSX + 1 + (c) * DIAMOND_SIZE[0], POSY + 1 + (r) * DIAMOND_SIZE[1]
screen.blit(DIAMOND_GREEN, (px, py))
s = "{}".format(grids[r][c])
distance_surface = use_font12.render(s, True, COLOR[COLOR_BLACK], COLOR[COLOR_GREEN])
screen.blit(distance_surface, (px, py))
# 画外墙
pygame.draw.rect(screen, COLOR[COLOR_RED], (POSX + 0, POSY + 0, DIAMOND_LEN*cols+1, DIAMOND_LEN*rows+1), 2)
# 画没打通的墙
for cx in range( cols):
for ry in range(rows):
px,py=POSX + 1 + (cx) * DIAMOND_SIZE[0], POSY + 1 + (ry) * DIAMOND_SIZE[1]
color = COLOR[COLOR_BLACK]
if maze.WALL == walls[ry][cx][0]:
pygame.draw.line(screen, color, (px, py), (px, py+DIAMOND_LEN), 2)
if maze.WALL == walls[ry][cx][1]:
pygame.draw.line(screen, color, (px, py), (px+DIAMOND_LEN, py), 2)
# 打印文字提示
if findEndPoint:
screen.blit(score_surface, (POSX+50, POSY+rows*22))
# 帧率
clock.tick(25)
pygame.display.update()
return
# main
if __name__ == "__main__":
'''main'''
multipath_maze_demo(20, 30)
来源:https://blog.csdn.net/defaultbyzt/article/details/116238007


猜你喜欢
- 1. 官方代码FUSE_MODULESTORCH.AO.QUANTIZATION.FUSE_MODULES的源代码2. fuse_modul
- 常见的数据增强操作有:按比例放大或缩小图片、旋转、平移、水平翻转、改变图像通道等。1.按比例放大和缩小扩展缩放只是改变图像的尺寸大小。Ope
- 大家好,我是启航。本文将给大家分享一个实用的Python办公自动化脚本 「利用Python批量翻译英文Word文档并保留格式」,最终效果甚至
- 本文实例讲述了js+CSS实现弹出居中背景半透明div层的方法。分享给大家供大家参考。具体实现方法如下:<!DOCTYPE html
- 在我们的web应用中,虽然PHP、JSP等脚本均提供了MySQL的接口,但是显然直接使用C语言具有更好的安全性和性能,在这篇文章中能够有所体
- 最近写运维自动化平台,需要用python写很多的小功能模块。这里就分享一个用Python的paramiko来实现功能的一段代码:复制远程服务
- 1. 什么是XSLT 大家可能听说过XSL(eXtensible Stylesheet Language),XSL和我们这里说的XSLT从狭
- 首先呢,我们来看看一般项目路由是怎么划分的。为什么这么划分呢?如果大项目业务非常多,单纯的单页面很难维护,我们只有这样规范化,才能高效率。模
- 目录plsql141. 安装注册使用 1.激活了会提示激活成功plsql developer14是由Allround Automa
- 前言MySQL的binlog日志是MySQL日志中非常重要的一种日志,记录了数据库所有的DML操作。通过binlog日志我们可以进行数据库的
- 一、什么是xml?有何特征?xml即可扩展标记语言,它可以用来标记数据、定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言。例子:
- 该方是基于uiautomator2如下版本进行验证的:PS C:\windows\system32> pip show uiautom
- I. Strict Mode阐述根据 mysql5.0以上版本 strict mode (STRICT_TRANS_TABLES) 的限制:
- 1 sample(序列a,n)sample(序列a,n)功能:从序列a中随机抽取n个元素,并将n个元素生以list形式返回。例:from r
- 什么是锁锁的本质,就是一种资源,是由操作系统维护的一种专门用于同步的资源比如说互斥锁,说白了就是一种互斥的资源。只能有一个进程(线程)占有。
- 在Python面向对象编程中的类构建中,有时候会遇到@classmethod的用法。总感觉有这种特殊性说明的用法都是高级用法,在我这个层级的
- 前言PyCharm是一种Python 的IDE工具(集成开发环境),带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,内部
- 最近一个开发需求中要求用pandas实现该需求:逐行对比两列,选出每行两列中较大的值加到第三列翻了下好像没有类似的函数,所以没办法要自己造轮
- 有一个多选的需求,在网上找到了这个插件:multiselect https://github.com/ehynds/jquery-ui-mu
- 导入库和数据首先,我们需要导入PyTorch和PyG库,然后准备好我们的数据。例如,我们可以使用以下方式生成一个简单的随机数据集:from