不要脸的放到了Github上面,嘿嘿。

Github:https://github.com/Radium1209/Maze

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

首先生成迷宫:

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

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

具体参考:https://blog.csdn.net/juzihongle1/article/details/73135920

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

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

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

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

最后用pyinstaller生成exe

教程:https://blog.csdn.net/Radium_1209/article/details/82939368

全部代码:

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] = 1Queue = 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)breakif (nc > 0) and (not vis[nr][nc - 1]) and (M[nr][nc][0]):vis[nr][nc] = 1Queue.append((nr, nc - 1))path[nr][nc - 1][0] = nrpath[nr][nc - 1][1] = ncif (nr > 0) and (not vis[nr - 1][nc]) and (M[nr][nc][1]):vis[nr][nc] = 1Queue.append((nr - 1, nc))path[nr - 1][nc][0] = nrpath[nr - 1][nc][1] = ncif (nc < num_cols - 1) and (not vis[nr][nc + 1]) and (M[nr][nc][2]):vis[nr][nc] = 1Queue.append((nr, nc + 1))path[nr][nc + 1][0] = nrpath[nr][nc + 1][1] = ncif (nr < num_rows - 1) and (not vis[nr + 1][nc]) and (M[nr][nc][3]):vis[nr][nc] = 1Queue.append((nr + 1, nc))path[nr + 1][nc][0] = nrpath[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 = 0c = 0history = [(r, c)]while history:M[r, c, 4] = 1check = []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] = 1c = c - 1M[r, c, 2] = 1if move_direction == 'U':M[r, c, 1] = 1r = r - 1M[r, c, 3] = 1if move_direction == 'R':M[r, c, 2] = 1c = c + 1M[r, c, 0] = 1if move_direction == 'D':M[r, c, 3] = 1r = r + 1M[r, c, 1] = 1else:r, c = history.pop()M[0, 0, 0] = 1M[num_rows - 1, num_cols - 1, 2] = 1for 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)] = 255if cell_data[0] == 1:image[range(10 * row + 2, 10 * row + 8), 10 * col] = 255image[range(10 * row + 2, 10 * row + 8), 10 * col + 1] = 255if cell_data[1] == 1:image[10 * row, range(10 * col + 2, 10 * col + 8)] = 255image[10 * row + 1, range(10 * col + 2, 10 * col + 8)] = 255if cell_data[2] == 1:image[range(10 * row + 2, 10 * row + 8), 10 * col + 9] = 255image[range(10 * row + 2, 10 * row + 8), 10 * col + 8] = 255if cell_data[3] == 1:image[10 * row + 9, range(10 * col + 2, 10 * col + 8)] = 255image[10 * row + 8, range(10 * col + 2, 10 * col + 8)] = 255return 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 = 0c = 0history = [(r, c)]while history:r, c = random.choice(history)M[r, c, 4] = 1history.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] = 2if 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] = 2if 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] = 2if 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] = 2if len(check):move_direction = random.choice(check)if move_direction == 'L':M[r, c, 0] = 1c = c - 1M[r, c, 2] = 1if move_direction == 'U':M[r, c, 1] = 1r = r - 1M[r, c, 3] = 1if move_direction == 'R':M[r, c, 2] = 1c = c + 1M[r, c, 0] = 1if move_direction == 'D':M[r, c, 3] = 1r = r + 1M[r, c, 1] = 1M[0, 0, 0] = 1M[num_rows - 1, num_cols - 1, 2] = 1for 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)] = 255if cell_data[0] == 1:image[range(10 * row + 2, 10 * row + 8), 10 * col] = 255image[range(10 * row + 2, 10 * row + 8), 10 * col + 1] = 255if cell_data[1] == 1:image[10 * row, range(10 * col + 2, 10 * col + 8)] = 255image[10 * row + 1, range(10 * col + 2, 10 * col + 8)] = 255if cell_data[2] == 1:image[range(10 * row + 2, 10 * row + 8), 10 * col + 9] = 255image[range(10 * row + 2, 10 * row + 8), 10 * col + 8] = 255if cell_data[3] == 1:image[10 * row + 9, range(10 * col + 2, 10 * col + 8)] = 255image[10 * row + 8, range(10 * col + 2, 10 * col + 8)] = 255return 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 - 1nc = num_cols - 1stack.append((nr, nc + 1))stack.append((nr, nc))while nr or nc:tr = nrtc = ncnr = (int)(path[tr][tc][0])nc = (int)(path[tr][tc][1])stack.append((nr, nc))# stack.append((num_rows, num_cols))pr = 0pc = 0dir = 2color_num = 150while(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_numimage[10 * pr + 5,range(10 * pc + 0, 10 * pc + 10)] = color_numelif (dir == 1):image[10 * pr + 4,range(10 * pc + 4, 10 * pc + 10)] = color_numimage[10 * pr + 5,range(10 * pc + 4, 10 * pc + 10)] = color_numimage[range(10 * pr + 4, 10 * pr + 10),10 * pc + 4] = color_numimage[range(10 * pr + 4, 10 * pr + 10),10 * pc + 5] = color_numelif (dir == 3):image[10 * pr + 4,range(10 * pc + 4, 10 * pc + 10)] = color_numimage[10 * pr + 5,range(10 * pc + 4, 10 * pc + 10)] = color_numimage[range(10 * pr + 0, 10 * pr + 6),10 * pc + 4] = color_numimage[range(10 * pr + 0, 10 * pr + 6),10 * pc + 5] = color_numdir = 2else:# print("L")if (dir == 0):image[10 * pr + 4,range(10 * pc + 0, 10 * pc + 10)] = color_numimage[10 * pr + 5,range(10 * pc + 0, 10 * pc + 10)] = color_numelif (dir == 1):image[10 * pr + 4,range(10 * pc + 0, 10 * pc + 6)] = color_numimage[10 * pr + 5,range(10 * pc + 0, 10 * pc + 6)] = color_numimage[range(10 * pr + 4, 10 * pr + 10),10 * pc + 4] = color_numimage[range(10 * pr + 4, 10 * pr + 10),10 * pc + 5] = color_numelif (dir == 3):image[10 * pr + 4,range(10 * pc + 0, 10 * pc + 6)] = color_numimage[10 * pr + 5,range(10 * pc + 0, 10 * pc + 6)] = color_numimage[range(10 * pr + 0, 10 * pr + 6),10 * pc + 4] = color_numimage[range(10 * pr + 0, 10 * pr + 6),10 * pc + 5] = color_numdir = 0elif (nc == pc):if (nr > pr):# print("D")if (dir == 3):image[range(10 * pr + 0, 10 * pr + 10),10 * pc + 4] = color_numimage[range(10 * pr + 0, 10 * pr + 10),10 * pc + 5] = color_numelif (dir == 0):image[10 * pr + 4,range(10 * pc + 4, 10 * pc + 10)] = color_numimage[10 * pr + 5,range(10 * pc + 4, 10 * pc + 10)] = color_numimage[range(10 * pr + 4, 10 * pr + 10),10 * pc + 4] = color_numimage[range(10 * pr + 4, 10 * pr + 10),10 * pc + 5] = color_numelif (dir == 2):image[10 * pr + 4,range(10 * pc + 0, 10 * pc + 6)] = color_numimage[10 * pr + 5,range(10 * pc + 0, 10 * pc + 6)] = color_numimage[range(10 * pr + 4, 10 * pr + 10),10 * pc + 4] = color_numimage[range(10 * pr + 4, 10 * pr + 10),10 * pc + 5] = color_numdir = 3else:# print("U")if (dir == 1):image[range(10 * pr + 0, 10 * pr + 10),10 * pc + 4] = color_numimage[range(10 * pr + 0, 10 * pr + 10),10 * pc + 5] = color_numelif (dir == 0):image[10 * pr + 4,range(10 * pc + 4, 10 * pc + 10)] = color_numimage[10 * pr + 5,range(10 * pc + 4, 10 * pc + 10)] = color_numimage[range(10 * pr + 0, 10 * pr + 6),10 * pc + 4] = color_numimage[range(10 * pr + 0, 10 * pr + 6),10 * pc + 5] = color_numelif (dir == 2):image[10 * pr + 4,range(10 * pc + 0, 10 * pc + 6)] = color_numimage[10 * pr + 5,range(10 * pc + 0, 10 * pc + 6)] = color_numimage[range(10 * pr + 0, 10 * pr + 6),10 * pc + 4] = color_numimage[range(10 * pr + 0, 10 * pr + 6),10 * pc + 5] = color_numdir = 1pr = nrpc = ncplt.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)

转载于:https://www.cnblogs.com/Radium1209/p/10415341.html

用Python写一个走迷宫的小程序(图形化:matplotlib,dfs,prim)相关推荐

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

    先看一下动态效果图(慢放): 首先生成迷宫: 主要用了两个算法:Prim和dfs 总结:Prim生成的比较像真正的迷宫,所以默认用了Prim生成迷宫 先输入n,m,会生成一个迷宫(prim生成),然后 ...

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

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

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

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

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

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

  5. 用python做毕业设计小程序_用Python写一个模拟qq聊天小程序的代码实例

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

  6. 如何利用python实现qq聊天_用Python写一个模拟qq聊天小程序的代码实例

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

  7. 如何用python写一个计算日期间隔的程序?

    如何用python写一个计算日期间隔的程序? 文章目录 如何用python写一个计算日期间隔的程序? 前言 问题梳理 问题解决 写在后面 前言 为什么想起来写一个这样的程序呢? 前几天聊天的时候,突然 ...

  8. python美元汇率兑换程序代码_还可以这样玩?用Python完成一个在线汇率转换小程序...

    原标题:还可以这样玩?用Python完成一个在线汇率转换小程序 大家好,小数在这里给大家拜个早年啦 今天给大家分享的是用Python完成一个在线汇率转换小程序,是基于一个持续更新的汇率网站实现的,让我 ...

  9. 用python编写一个处理报文的小程序

    最近用python编写了一个处理报文的小程序,最后保存成.txt格式. 主要内容非常简单,介绍如下: 1.报文内容如下所示,比较乱,因为要输入到matalab中进行仿真验证,因此,需要将里面的'\n' ...

最新文章

  1. 原来这就是Java代码生成器的原理啊,太简单了
  2. 【c++】28.虚析构函数、纯虚函数
  3. 机器人学习--Robotics 4:Perception(宾夕法尼亚大学COURSERA课程)
  4. 【Vue】脚手架 Vue CLI 的使用
  5. 【转】java反射--注解
  6. LaTeX tikz初探——利用emoji画GPS卫星2D分布图(2)
  7. python机器学习库sklearn——神经网络
  8. 问题五十一:怎么用ray tracing画tear drop
  9. HTTP所承载的货物(图像、文本、软件等)要满足的条件
  10. 【java算法】二分查找算法详解
  11. STM32或GD32驱动TM1637
  12. adb shell screencap/screenrecord(三级命令)
  13. deepinv2 添加打印机_Deepin系统上安装使用HP惠普打印机的方法
  14. android 距离测量工具,尺子距离测量app
  15. 系统盘清理,便携小助手一键清理系统垃圾
  16. python 实现人脸采集 训练 与人脸识别
  17. compiz在debian上的安装和使用
  18. 解决 E45: 'readonly' option is set (add ! to override)
  19. 电脑控制手机 教你实现多个手机同时打开关闭软件
  20. 【 facenet-retinaface】快速复现 实现 facenet-retinaface-pytorch 人脸识别 windows上 使用cpu实现

热门文章

  1. 有价值项目分享,缺项目可直接搜索(持续更新中)
  2. 软件测试拿了几个20K offer,分享一波面经
  3. WordPress主题-一个极简的免费WordPress博客主题
  4. 408 知识点笔记——操作系统(绪论、进程管理)
  5. gb2312简繁转换js兼容各种浏览器
  6. 小白的jquery学习之路之04效果新闻向上无缝循环显示
  7. 海外服务器的3种体系架构:SMP、NUMA、MPP
  8. 用Python批量从本地导数据到postgres数据库,比人工导入快十倍,爽
  9. 获取连续生成的100-200范围的随机数,直到生成的随机数与前一个随机数相等,停止运行
  10. 初出茅庐的小李第73篇博客之offsetof(type, member-designator)使用