文章目录

  • 步骤
    • 1、创建迷宫
    • 2、寻路
    • 3、绘制迷宫和路由
  • 完整版

步骤

1、创建迷宫

定义【0】为【墙】,【1】为【路】,先创建外墙,再创建内墙

# outer wall
maze=[[0,0,0,0,0,0],[0,1,1,1,1,0],[0,1,1,1,1,0],[0,1,1,1,1,0],[0,1,1,1,1,0],[0,0,0,0,0,0]]
# inner wall
maze[2][1]=maze[1][3]=maze[2][3]=maze[3][3]=0

2、寻路

  • 列表path记录路线
  • 列表footprint记录已经走过的路(以免重复)
  • steps表示上下左右4个可行方向
  • 起点:x=1 y=1
# the way out of maze
path=[(1,1)]
# never again
footprint=[]
# right down left up
steps=((0,1),(1,0),(0,-1),(-1,0))
# entrance
x=y=1
  • 终点:x=4 y=4
if 下一步(没有墙 & 没走过):
记录下一步 & 进入下一步
else:
返回上一步
  • path[-1]表示当前位置
while x!=4 or y!=4:# horizontal verticalfor h,v in steps:if maze[x+h][y+v]==' ' and (x+h,y+v) not in footprint:path.append((x+h,y+v))footprint.append((x+h,y+v))breakelse:path.pop()# locationx,y=path[-1]

3、绘制迷宫和路由

import random
# show the maze or the path
def show_maze(maze):for i in maze:for j in i:if j==0:print('\033[36m',j,sep='',end=' \033[0m')elif j=='+':print('\033[31m',j,sep='',end=' \033[0m')elif j=='O':print('\033[33m',j,sep='',end=' \033[0m')else:print(j,end=' ')print()
# build a maze which the size of maze is [n]
def build_maze(n):maze=[]# outer wallfor i in range(n+2):if i==0:maze.append(['O']+[0]*(n+1))elif i==n+1:maze.append([0]*(n+1)+['O'])else:maze.append([0]+[' ']*n+[0])# random inner wallfor i in range(1,n+1):if i==1:for j in range(n//4):maze[i][random.randint(2,n-1)]=0elif i==n:for j in range(n//4):maze[i][random.randint(1,n-2)]=0else:for j in range(n//4):maze[i][random.randint(1,n-1)]=0show_maze(maze)return maze
# find the exit of the maze
def route():maze=build_maze(9)# the way out of mazepath=[(1,1)]# never againfootprint=[]# right down left upsteps=((0,1),(1,0),(0,-1),(-1,0))# entrancex=y=1while x!=9 or y!=9:# horizontal verticalfor h,v in steps:if maze[x+h][y+v]==' ' and (x+h,y+v) not in footprint:path.append((x+h,y+v))footprint.append((x+h,y+v))breakelse:path.pop()# locationx,y=path[-1]# show the maze and the pathfor x,y in path:maze[x][y]='+'show_maze(maze)
# play
route()

完整版

random迷宫,3种路由

# rectangle maze
import random,math,copy
# show the maze or the path
def show_maze(maze):for i in maze:for j in i:if j=='0':  # wallprint('\033[36m',j,sep='',end='\033[0m')elif j=='O':  # [ entrance & exit ]& pathprint('\033[33m',j,sep='',end='\033[0m')elif j=='X':  # footprintprint('\033[31m',j,sep='',end='\033[0m')else:print(j,end='')print()
# build a maze which the size of maze is [ n * 10n ]
def build_maze(n):maze = []# wallw='0'# entrance & exite='O'# outer wallfor i in range(n+2):if i==0:maze.append([e]+[w]*(n*10+1))elif i==n+1:maze.append([w]*(n*10+1)+[e])else:maze.append([w]+[' ']*n*10+[w])# random inner wallfor i in range(1,n+1):atan=math.atan(n)balance=math.floor(atan**(atan**(atan+0.5)+0.5))*nif i==1:for j in range(balance):maze[i][random.randint(2,n*10-1)]=welif i==n:for j in range(balance):maze[i][random.randint(1,n*10-2)]=welse:for j in range(balance):maze[i][random.randint(1,n*10-1)]=wshow_maze(maze)return maze
# route optimization
def optimize(path):# anterior step, next step, posterior stepanterior=0while anterior<len(path):x,y=path[anterior]next_step=[(x,y-1),(x-1,y),(x,y+1),(x+1,y)]for posterior in range(len(path)-1,anterior+1,-1):if path[posterior] in next_step:del path[anterior+1:posterior]breakanterior+=1
# find the exit of the maze
def route(maze,n,option=0):if option==0:# right down up leftsteps=((0,1),(1,0),(-1,0),(0,-1))elif option==1:# right up down leftsteps=((0,1),(-1,0),(1,0),(0,-1))else:# left up down rightsteps=((0,-1),(-1,0),(1,0),(0,1))# the way out of mazepath=[(1,1)]# never againfootprint=[]# entrancex=y=1while x!=n or y!=n*10:# horizontal verticalfor h,v in steps:if maze[x+h][y+v]==' ' and (x+h,y+v) not in footprint:path.append((x+h,y+v))footprint.append((x+h,y+v))breakelse:if path:path.pop()else:# There is no escape. show the maze and footprintfor x,y in footprint:maze[x][y]='X'show_maze(maze)break# locationif path:x,y=path[-1]else:# show the maze, footprint and pathfor x,y in footprint:maze[x][y]='X'# optimize the path of mazeoptimize(path)for x,y in path:maze[x][y]='O'show_maze(maze)
# play
while True:n=input('the size of maze:').strip()if n.isdigit() and n!='0':n=int(n)else:n=19maze0=build_maze(n)maze1=copy.deepcopy(maze0)maze2=copy.deepcopy(maze0)input('Press any key to continue.')route(maze0,n)input('Press any key to continue.')route(maze1,n,1)input('Press any key to continue.')route(maze2,n,2)

Python 迷宫算法相关推荐

  1. python迷宫算法及实现_Python迷宫递归算法

    所以我盯着这个有一段时间了,我不知道怎么才能回到这个迷宫的正确路径.在 2代表墙MAZE = [[2,2,2,2,1,2], [2,2,1,2,1,2], [2,2,1,2,1,2], [2,1,1, ...

  2. 碉堡的小程序:用 Python 制作演示迷宫算法的 gif 动画

    微信改版,加星标不迷路! 碉堡的小程序:用 Python 制作演示迷宫算法的 gif 动画 作者:neozhaoliang 本文要介绍的是我写的一个有趣的小程序,一个脱离了低级趣味的程序,一个有益于广 ...

  3. python游戏程序编码_python实现的生成随机迷宫算法核心代码分享(含游戏完整代码)...

    最近研究了下迷宫的生成算法,然后做了个简单的在线迷宫游戏.游戏地址和对应的开源项目地址可以通过上面的链接找到.开源项目中没有包含服务端的代码,因为服务端的代码实在太简单了.下面将简单的介绍下随机迷宫的 ...

  4. 迷宫算法之递归回溯python实现

    目录 没有目录了,别看了. 0. 概要 上一张我们谈到prim算法,这一张我们使用递归回溯算法来实现迷宫算法,相对于随机prim算法,这个算法更容易理解,并且提出的概念相对较小.但原理不太一样,,这也 ...

  5. 走迷宫算法 用python实现

    一个迷宫搜索的过程可以用python语言的算法来加以描述: 思路:穷举法.把所有的路都走了,总一条是对的. 首先老鼠不走回头路,它随便沿着一个方向一直走,遇到墙壁后换一个方向,直到没有路可以走,那么这 ...

  6. 用Python代码实现走迷宫算法

    目录 Description 18276走迷宫算法 输入格式 输出格式 总结 Description 在一个二维矩阵中,从给定的起点出发,通过向上.向下.向左.向右四个方向移动,寻找一条到达终点的路径 ...

  7. python制作动图-用Python制作迷宫GIF

    原标题:用Python制作迷宫GIF 安装 可以通过PyPi安装 或者通过Git 为什么你需要这个库? 问:我是一个Python迷,并且对迷宫的生成和迷宫解决的办法非常感兴趣.我很羡慕别人能够做出生成 ...

  8. 【python教程入门学习】用Python制作迷宫GIF

    安装 可以通过PyPi安装 或者通过Git 为什么你需要这个库? 问:我是一个Python迷,并且对迷宫的生成和迷宫解决的办法非常感兴趣.我很羡慕别人能够做出生成迷宫的动画.我如何能够用Python自 ...

  9. python设计迷宫_用Python制作迷宫GIF

    安装 可以通过PyPi安装 或者通过Git 为什么你需要这个库? 问:我是一个Python迷,并且对迷宫的生成和迷宫解决的办法非常感兴趣.我很羡慕别人能够做出生成迷宫的动画.我如何能够用Python自 ...

  10. 迷宫游戏python实现

    在迷宫算法总结篇中我总结了生成迷宫的四种算法,在这一篇文章里面我侧重迷宫游戏的实现以及可视化. 使用python3中的GUI绘图库tkinter实现了一个简陋版的迷宫游戏,地图截图如下图所示. 为了降 ...

最新文章

  1. 皮一皮:年轻人就是胆子大...
  2. imx51-linux的cpuinfo之分析
  3. 嵌入式Linux学习问题解决记录
  4. (转) Android 数字签名
  5. Session对象的清空
  6. DCMTK:测试VR类的compare()运算符
  7. Redis Lua脚本中学教程(上)
  8. 第07讲:入门首选,Requests 库的基本使用
  9. 【CodeForces - 1153D】Serval and Rooted Tree(树形dp)
  10. php oracle 操作 sql语句中能不能添加数组_如何在PHP中使用Oracle数据库_php
  11. android java pipe_Java-使用Dagger 2进行Android单元测试
  12. 知乎究竟走的是什么路线?克隆之路靠谱吗?
  13. css让image不改变大小_如何改变图片大小
  14. linux系统ss命令详解,ss命令 - Linux命令大全 | linux教程
  15. uniapp使用阿里字体图标库
  16. wedo+scratch第一次上课
  17. Echarts绘制各种数据可视化图表案例(效果+代码)
  18. Django基础(16): 模板标签(tags)的介绍及如何自定义模板标签
  19. Android之实现手写电子签名
  20. SQL的LEN函数用法及实例

热门文章

  1. Windows10查看便签
  2. 常见五大开源网络监控软件测评分析
  3. 扫码枪收银有手续费吗_收银系统怎么选
  4. 声卡接口 LINE_IN、MIC_IN、LINE_OUT
  5. S7-200SMART PLC与V20变频器MODBUS通讯示例程序
  6. Java 在Excel中添加水印
  7. 关于Oppenheim不等式的加强
  8. 互联网晚报 | 12月13日 星期一 | 百度将发布元宇宙产品“希壤”;吉利发布国内首款7nm汽车芯片;腾讯云财税管家正式发布...
  9. Vivado ROM IP核
  10. FS4054单节锂电池充电管理芯片,IC电路图