先看一下动态效果图(慢放):

首先生成迷宫:

主要用了两个算法:Prim和dfs

总结:Prim生成的比较像真正的迷宫,所以默认用了Prim生成迷宫

先输入n,m,会生成一个迷宫(prim生成),然后会动态的走完整个迷宫。

走迷宫用的是bfs算法,具体不多说了,关键难点在于记录路径:

我是用一个path二维数组来记录路径的,每一个位置存的是这个位置的上一个位置,在输出的时候倒着从最后一个点回溯到第一个点存入一个栈,最后正常出栈就是路径了。

画路径:有上,下,左,右,上左,上右,下左,下右,左下,左上,右上,右下12中情况分类讨论(实在是太麻烦了)。

最后用pyinstaller生成exe

///全部代码:

import random

import numpy as np

from matplotlib import pyplot as plt

import matplotlib.cm as cm

from collections import deque

import tkinter

#显示地图(图形化显示)

def show_maze(Maze):

plt.imshow(Maze, cmap=cm.Wistia_r, interpolation='none')

# plt.ion()

plt.show()

#用bfs查找路径

def find_path_bfs(image, M):

path = np.zeros((num_rows, num_cols, 2))

vis = np.zeros((num_rows, num_cols))

vis[0][0] = 1

Queue = deque()

Queue.append((0, 0))

while(Queue):

temp = Queue.popleft()

nr = temp[0]

nc = temp[1]

if (nc == num_cols - 1) and (nr == num_rows - 1):

show_path(image, path)

break

if (nc > 0) and (not vis[nr][nc - 1]) and (M[nr][nc][0]):

vis[nr][nc] = 1

Queue.append((nr, nc - 1))

path[nr][nc - 1][0] = nr

path[nr][nc - 1][1] = nc

if (nr > 0) and (not vis[nr - 1][nc]) and (M[nr][nc][1]):

vis[nr][nc] = 1

Queue.append((nr - 1, nc))

path[nr - 1][nc][0] = nr

path[nr - 1][nc][1] = nc

if (nc < num_cols - 1) and (not vis[nr][nc + 1]) and (M[nr][nc][2]):

vis[nr][nc] = 1

Queue.append((nr, nc + 1))

path[nr][nc + 1][0] = nr

path[nr][nc + 1][1] = nc

if (nr < num_rows - 1) and (not vis[nr + 1][nc]) and (M[nr][nc][3]):

vis[nr][nc] = 1

Queue.append((nr + 1, nc))

path[nr + 1][nc][0] = nr

path[nr + 1][nc][1] = nc

#prim算法生成地图

def Create_maze_prim():

M = np.zeros((num_rows, num_cols, 5))

image = np.zeros((num_rows * 10, num_cols * 10))

r = 0

c = 0

history = [(r, c)]

while history:

M[r, c, 4] = 1

check = []

if c > 0 and M[r, c - 1, 4] == 0:

check.append('L')

if r > 0 and M[r - 1, c, 4] == 0:

check.append('U')

if c < num_cols - 1 and M[r, c + 1, 4] == 0:

check.append('R')

if r < num_rows - 1 and M[r + 1, c, 4] == 0:

check.append('D')

if len(check):

history.append([r, c])

move_direction = random.choice(check)

if move_direction == 'L':

M[r, c, 0] = 1

c = c - 1

M[r, c, 2] = 1

if move_direction == 'U':

M[r, c, 1] = 1

r = r - 1

M[r, c, 3] = 1

if move_direction == 'R':

M[r, c, 2] = 1

c = c + 1

M[r, c, 0] = 1

if move_direction == 'D':

M[r, c, 3] = 1

r = r + 1

M[r, c, 1] = 1

else:

r, c = history.pop()

M[0, 0, 0] = 1

M[num_rows - 1, num_cols - 1, 2] = 1

for row in range(0, num_rows):

for col in range(0, num_cols):

cell_data = M[row, col]

for i in range(10 * row + 2, 10 * row + 8):

image[i, range(10 * col + 2, 10 * col + 8)] = 255

if cell_data[0] == 1:

image[range(10 * row + 2, 10 * row + 8), 10 * col] = 255

image[range(10 * row + 2, 10 * row + 8), 10 * col + 1] = 255

if cell_data[1] == 1:

image[10 * row, range(10 * col + 2, 10 * col + 8)] = 255

image[10 * row + 1, range(10 * col + 2, 10 * col + 8)] = 255

if cell_data[2] == 1:

image[range(10 * row + 2, 10 * row + 8), 10 * col + 9] = 255

image[range(10 * row + 2, 10 * row + 8), 10 * col + 8] = 255

if cell_data[3] == 1:

image[10 * row + 9, range(10 * col + 2, 10 * col + 8)] = 255

image[10 * row + 8, range(10 * col + 2, 10 * col + 8)] = 255

return M, image

#dfs方法生成地图

def Create_maze_dfs():

M = np.zeros((num_rows, num_cols, 5))

image = np.zeros((num_rows * 10, num_cols * 10))

r = 0

c = 0

history = [(r, c)]

while history:

r, c = random.choice(history)

M[r, c, 4] = 1

history.remove((r, c))

check = []

if c > 0:

if M[r, c - 1, 4] == 1:

check.append('L')

elif M[r, c - 1, 4] == 0:

history.append((r, c - 1))

M[r, c - 1, 4] = 2

if r > 0:

if M[r - 1, c, 4] == 1:

check.append('U')

elif M[r - 1, c, 4] == 0:

history.append((r - 1, c))

M[r - 1, c, 4] = 2

if c < num_cols - 1:

if M[r, c + 1, 4] == 1:

check.append('R')

elif M[r, c + 1, 4] == 0:

history.append((r, c + 1))

M[r, c + 1, 4] = 2

if r < num_rows - 1:

if M[r + 1, c, 4] == 1:

check.append('D')

elif M[r + 1, c, 4] == 0:

history.append((r + 1, c))

M[r + 1, c, 4] = 2

if len(check):

move_direction = random.choice(check)

if move_direction == 'L':

M[r, c, 0] = 1

c = c - 1

M[r, c, 2] = 1

if move_direction == 'U':

M[r, c, 1] = 1

r = r - 1

M[r, c, 3] = 1

if move_direction == 'R':

M[r, c, 2] = 1

c = c + 1

M[r, c, 0] = 1

if move_direction == 'D':

M[r, c, 3] = 1

r = r + 1

M[r, c, 1] = 1

M[0, 0, 0] = 1

M[num_rows - 1, num_cols - 1, 2] = 1

for row in range(0, num_rows):

for col in range(0, num_cols):

cell_data = M[row, col]

for i in range(10 * row + 2, 10 * row + 8):

image[i, range(10 * col + 2, 10 * col + 8)] = 255

if cell_data[0] == 1:

image[range(10 * row + 2, 10 * row + 8), 10 * col] = 255

image[range(10 * row + 2, 10 * row + 8), 10 * col + 1] = 255

if cell_data[1] == 1:

image[10 * row, range(10 * col + 2, 10 * col + 8)] = 255

image[10 * row + 1, range(10 * col + 2, 10 * col + 8)] = 255

if cell_data[2] == 1:

image[range(10 * row + 2, 10 * row + 8), 10 * col + 9] = 255

image[range(10 * row + 2, 10 * row + 8), 10 * col + 8] = 255

if cell_data[3] == 1:

image[10 * row + 9, range(10 * col + 2, 10 * col + 8)] = 255

image[10 * row + 8, range(10 * col + 2, 10 * col + 8)] = 255

return M, image

#显示路径(图形化)

def show_path(image, path):

plt.imshow(image, cmap=cm.Wistia_r, interpolation='none')

plt.ion()

plt.pause(2)

str = ""

stack = []

nr = num_rows - 1

nc = num_cols - 1

stack.append((nr, nc + 1))

stack.append((nr, nc))

while nr or nc:

tr = nr

tc = nc

nr = (int)(path[tr][tc][0])

nc = (int)(path[tr][tc][1])

stack.append((nr, nc))

# stack.append((num_rows, num_cols))

pr = 0

pc = 0

dir = 2

color_num = 150

while(stack):

temp = stack.pop()

nr = temp[0]

nc = temp[1]

if nr or nc:

if (nr == pr):

if (nc > pc):

# print("R")

if (dir == 2):

image[10 * pr + 4,

range(10 * pc + 0, 10 * pc + 10)] = color_num

image[10 * pr + 5,

range(10 * pc + 0, 10 * pc + 10)] = color_num

elif (dir == 1):

image[10 * pr + 4,

range(10 * pc + 4, 10 * pc + 10)] = color_num

image[10 * pr + 5,

range(10 * pc + 4, 10 * pc + 10)] = color_num

image[range(10 * pr + 4, 10 * pr + 10),

10 * pc + 4] = color_num

image[range(10 * pr + 4, 10 * pr + 10),

10 * pc + 5] = color_num

elif (dir == 3):

image[10 * pr + 4,

range(10 * pc + 4, 10 * pc + 10)] = color_num

image[10 * pr + 5,

range(10 * pc + 4, 10 * pc + 10)] = color_num

image[range(10 * pr + 0, 10 * pr + 6),

10 * pc + 4] = color_num

image[range(10 * pr + 0, 10 * pr + 6),

10 * pc + 5] = color_num

dir = 2

else:

# print("L")

if (dir == 0):

image[10 * pr + 4,

range(10 * pc + 0, 10 * pc + 10)] = color_num

image[10 * pr + 5,

range(10 * pc + 0, 10 * pc + 10)] = color_num

elif (dir == 1):

image[10 * pr + 4,

range(10 * pc + 0, 10 * pc + 6)] = color_num

image[10 * pr + 5,

range(10 * pc + 0, 10 * pc + 6)] = color_num

image[range(10 * pr + 4, 10 * pr + 10),

10 * pc + 4] = color_num

image[range(10 * pr + 4, 10 * pr + 10),

10 * pc + 5] = color_num

elif (dir == 3):

image[10 * pr + 4,

range(10 * pc + 0, 10 * pc + 6)] = color_num

image[10 * pr + 5,

range(10 * pc + 0, 10 * pc + 6)] = color_num

image[range(10 * pr + 0, 10 * pr + 6),

10 * pc + 4] = color_num

image[range(10 * pr + 0, 10 * pr + 6),

10 * pc + 5] = color_num

dir = 0

elif (nc == pc):

if (nr > pr):

# print("D")

if (dir == 3):

image[range(10 * pr + 0, 10 * pr + 10),

10 * pc + 4] = color_num

image[range(10 * pr + 0, 10 * pr + 10),

10 * pc + 5] = color_num

elif (dir == 0):

image[10 * pr + 4,

range(10 * pc + 4, 10 * pc + 10)] = color_num

image[10 * pr + 5,

range(10 * pc + 4, 10 * pc + 10)] = color_num

image[range(10 * pr + 4, 10 * pr + 10),

10 * pc + 4] = color_num

image[range(10 * pr + 4, 10 * pr + 10),

10 * pc + 5] = color_num

elif (dir == 2):

image[10 * pr + 4,

range(10 * pc + 0, 10 * pc + 6)] = color_num

image[10 * pr + 5,

range(10 * pc + 0, 10 * pc + 6)] = color_num

image[range(10 * pr + 4, 10 * pr + 10),

10 * pc + 4] = color_num

image[range(10 * pr + 4, 10 * pr + 10),

10 * pc + 5] = color_num

dir = 3

else:

# print("U")

if (dir == 1):

image[range(10 * pr + 0, 10 * pr + 10),

10 * pc + 4] = color_num

image[range(10 * pr + 0, 10 * pr + 10),

10 * pc + 5] = color_num

elif (dir == 0):

image[10 * pr + 4,

range(10 * pc + 4, 10 * pc + 10)] = color_num

image[10 * pr + 5,

range(10 * pc + 4, 10 * pc + 10)] = color_num

image[range(10 * pr + 0, 10 * pr + 6),

10 * pc + 4] = color_num

image[range(10 * pr + 0, 10 * pr + 6),

10 * pc + 5] = color_num

elif (dir == 2):

image[10 * pr + 4,

range(10 * pc + 0, 10 * pc + 6)] = color_num

image[10 * pr + 5,

range(10 * pc + 0, 10 * pc + 6)] = color_num

image[range(10 * pr + 0, 10 * pr + 6),

10 * pc + 4] = color_num

image[range(10 * pr + 0, 10 * pr + 6),

10 * pc + 5] = color_num

dir = 1

pr = nr

pc = nc

plt.clf()

plt.imshow(image, cmap=cm.Wistia_r, interpolation='none')

# plt.ion()

if (stack):

plt.ion()

plt.pause(0.03 / (num_cols * num_rows / 100))

else:

plt.ioff()

plt.show()

# plt.pause(1000)

#主函数

if __name__ == '__main__':

num_rows = int(input("Please input rows: "))

num_cols = int(input("Please input columns: "))

path = np.zeros((num_rows, num_cols, 2))

M = np.zeros((num_rows, num_cols, 5))

image = np.zeros((num_rows * 10, num_cols * 10))

# row_image = np.zeros((num_rows * 10, num_cols * 10))

M, image = Create_maze_prim()

# show_maze(image)

find_path_bfs(image, M)

# show_path(image, path)

python迷宫万花筒代码_用Python3写一个走迷宫的小程序(图形化:matplotlib,dfs,prim)...相关推荐

  1. python软件代码示例-用Python写一个模拟qq聊天小程序的代码实例

    Python 超简单的聊天程序 客户端: import socket, sys host = '10.248.27.23' # host = raw_input("Plz imput des ...

  2. 用Python写一个模拟qq聊天小程序的代码实例

    前言 今天小编就为大家分享一篇关于用Python写一个模拟qq聊天小程序的代码实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧 Python 超简单的聊天 ...

  3. 能不能用python开发qq_用Python写一个模拟qq聊天小程序的代码实例

    用Python写一个模拟qq聊天小程序的代码实例 发布时间:2020-09-09 07:49:29

  4. 迷宫出路代码_如何在软件开发的迷宫中找到自己的出路

    迷宫出路代码 by Tim Kleier 蒂姆·克莱尔(Tim Kleier) 如何在软件开发的迷宫中找到自己的出路 (How to find your way through the corn ma ...

  5. 用Python写一个走迷宫的小程序(图形化:matplotlib,dfs,prim)

    不要脸的放到了Github上面,嘿嘿. Github:https://github.com/Radium1209/Maze 先看一下动态效果图(慢放): 首先生成迷宫: 主要用了两个算法:Prim和d ...

  6. python数字华容道算法_用React写一个数字华容道,你需要知道的秘密

    还在上班?很无聊? 这个叫前言 年末了.哦,不,要过年了.以前只能一路站到公司的我,今早居然是坐着过来的.新的一年,总要学一个新东西来迎接新的未来吧,所以选择了一直未碰的那个据说是全宇宙最牛逼的前端框 ...

  7. 用python画路飞代码_路飞学城Python-Day38(第四模块思维导图)

    Servlet学习三--传输文件 最先在考虑传输文件时,想通过java写一个文件上传案例,传给Servlet,Servlet再保存至数据库中,但苦于一直没找到实例,听说Flex有实际的例子,就直接用F ...

  8. c语言作业迷宫代码,用C语言写的走迷宫的代码

    //走迷宫 普通走法 #include #include #include #include #define Height 25 //迷宫的高度,必须为奇数 #define Width 25 //迷宫 ...

  9. cmd写java程序_用cmd写一个最简单的Java程序

    一,准备: 1.确保电脑中装有eclipse软件并且确保配置好环境变量 (1)环境变量配置方法: 特别提示:jdk和eclipse保存的路径不能有中文字符 1.打开我的电脑--属性--高级--环境变量 ...

最新文章

  1. TensorFlow与PyTorch模型部署性能比较
  2. 面试题:mysql 数据类型
  3. matlab全景图素材,matlab以simulink的方式建立一个vr模型
  4. 【转】linux su和sudo命令的区别——百度知道
  5. redis-4.0.10集群安装(3台机器,6个node),以及在Spring项目中的集成,redis操作工具类
  6. 腾讯GaiaStack容器平台负责人罗韩梅:All on GaiaStack
  7. 划分VLAN,以及VLAN间通信
  8. 底层系统如何实现数据一致性/系统底层如何保证有序性
  9. VMware Workstation与VMware vSphere的区别
  10. open函数和close函数的使用
  11. 请详细描述listview与gridview的异同点_一建考试中,实在不会的怎么办?教你从题目中获取得分点!...
  12. oracle更换年,Oracle数据库更换服务器10分钟切换方案
  13. C#中@字符的三个作用
  14. vmware10中开启Intel VT-x
  15. You Probably Dont Need Derived State
  16. 科学计算机角度值改为弧度制,弧度制换算(角度换算弧度计算器)
  17. 汽车线性二自由度动力学模型-simulink仿真
  18. 山西最新五大姓氏排名发布,排名第一的是王,第二的竟是……
  19. 坚果pro android版本,坚果pro升级安卓10 更新系统Smartisan OSv7.5.0
  20. vsftp创建虚拟账户

热门文章

  1. python用户输入的是q吗编码_Day01-Python基础3-变量/字符编码/用户输入
  2. oracle 手机客户端_Oracle Client(客户端) 安装与配置
  3. 国服hypixel改JAVA_《我的世界》国服新版本Hypixel服务器MOD安装教程
  4. office套件_Microsoft Office 2019 for Mac(office办公套件)
  5. 安装kali linux后的准备工作
  6. Linux安装GCC+升级GCC流程详解
  7. python pip安装selenium_python+selenium安装
  8. 管易云·奇门对接打通金蝶云星空历史订单查询接口与销售出库新增接口
  9. 户外探险9种必备装备选购指南
  10. 交换机介绍及选购全攻略