参考维基百科http://en.wikipedia.org/wiki/Maze_generation_algorithm

1 深度优先搜索

  1. Start at a particular cell and call it the "exit."
  2. Mark the current cell as visited, and get a list of its neighbors. For each neighbor, starting with a randomly selected neighbor:
    1. If that neighbor hasn't been visited, remove the wall between this cell and that neighbor, and then recurse with that neighbor as the current cell.

2 随机Kruskal算法

  1. Create a list of all walls, and create a set for each cell, each containing just that one cell.
  2. For each wall, in some random order:
    1. If the cells divided by this wall belong to distinct sets:

      1. Remove the current wall.
      2. Join the sets of the formerly divided cells.

3 随机Prim算法

  1. Start with a grid full of walls.
  2. Pick a cell, mark it as part of the maze. Add the walls of the cell to the wall list.
  3. While there are walls in the list:
    1. Pick a random wall from the list. If the cell on the opposite side isn't in the maze yet:

      1. Make the wall a passage and mark the cell on the opposite side as part of the maze.
      2. Add the neighboring walls of the cell to the wall list.
    2. If the cell on the opposite side already was in the maze, remove the wall from the list.

Kruskal算法和Prim算法:

http://www.cnblogs.com/visayafan/archive/2012/02/25/2367360.html

http://www.cnblogs.com/visayafan/archive/2012/04/11/2443130.html

一个Python源码实现:

 1 import numpy as np
 2 from numpy.random import random_integers as rnd
 3 import matplotlib.pyplot as plt
 4
 5 def maze(width=81, height=51, complexity=.75, density =.75):
 6     # Only odd shapes
 7     shape = ((height//2)*2+1, (width//2)*2+1)
 8     # Adjust complexity and density relative to maze size
 9     complexity = int(complexity*(5*(shape[0]+shape[1])))
10     density    = int(density*(shape[0]//2*shape[1]//2))
11     # Build actual maze
12     Z = np.zeros(shape, dtype=bool)
13     # Fill borders
14     Z[0,:] = Z[-1,:] = 1
15     Z[:,0] = Z[:,-1] = 1
16     # Make isles
17     for i in range(density):
18         x, y = rnd(0,shape[1]//2)*2, rnd(0,shape[0]//2)*2
19         Z[y,x] = 1
20         for j in range(complexity):
21             neighbours = []
22             if x > 1:           neighbours.append( (y,x-2) )
23             if x < shape[1]-2:  neighbours.append( (y,x+2) )
24             if y > 1:           neighbours.append( (y-2,x) )
25             if y < shape[0]-2:  neighbours.append( (y+2,x) )
26             if len(neighbours):
27                 y_,x_ = neighbours[rnd(0,len(neighbours)-1)]
28                 if Z[y_,x_] == 0:
29                     Z[y_,x_] = 1
30                     Z[y_+(y-y_)//2, x_+(x-x_)//2] = 1
31                     x, y = x_, y_
32     return Z
33
34 plt.figure(figsize=(10,5))
35 plt.imshow(maze(80,40),cmap=plt.cm.binary,interpolation='nearest')
36 plt.xticks([]),plt.yticks([])37 plt.show()

运行结果如下所示:

【Tree】迷宫生成算法相关推荐

  1. [迷宫中的算法实践]迷宫生成算法——Prim算法

    普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点(英语:Vertex (graph theory)), ...

  2. c语言 迷宫深度遍历 算法,图的遍历迷宫生成算法浅析

    1. 引言 在平常的游戏中,我们常常会碰到随机生成的地图.这里我们就来看看一个简单的随机迷宫是如何生成. 2. 迷宫描述随机生成一个m * n的迷宫,可用一个矩阵maze[m][n]来表示,如图:   ...

  3. 迷宫生成算法和迷宫寻路算法

    迷宫生成算法和迷宫寻路算法 大学二年级的时候,作为对栈这个数据结构的复习,我制作了一个迷宫生成算法的小程序,当时反响十分好,过了几天我又用自己已经学的DirectX技术制作了DirectX版的程序.这 ...

  4. C#三大迷宫生成算法

    今天介绍一下很经典的三大迷宫算法的C#实现,即随机普利姆算法,深度优先算法和十字分割(也就是递归分割算法).实现参考了[ActionScript 3] 三大迷宫生成算法一文(生成的迷宫预览图也使用的该 ...

  5. 随机迷宫生成算法——深度优先算法

    迷宫是我们小时候经常玩的游戏,如何用代码来快速生成上面这种迷宫呢? 迷宫算法有三大算法:深度优先算法.prim算法和递归分割算法.这里用的是深度优先算法,在此说一下算法思路,希望对各位有所帮助. 首先 ...

  6. 迷宫生成算法---深度优先算法(基于python)

    迷宫生成算法---深度优先算法 总体的目录 版权及协议声明 更加舒服的阅读方式 一. 深度优先算法的原理与思路 二.迷宫的制作 迷宫的总体的创建. 三.代码的实现 总体的目录 版权及协议声明 本文章遵 ...

  7. 随机迷宫生成算法——prime算法

    本以为Prime迷宫生成算法和图论的Prime算法有什么关联,貌似并没有. Prime迷宫生成算法的原理: (1)初始地图所有位置均设为墙 (2)任意插入一个墙体进墙队列 (3)判断此时墙体是否可以设 ...

  8. 随机迷宫生成算法浅析

    摘要 本文对随机迷宫生成进行了初步的研究和分析,并给出了两种不同的生成算法.最终的算法结合了图的深度优先遍历.通过对比两种算法之间,可发现,在实际问题中,结合了离散数学的方法往往非更有效率且效果更佳. ...

  9. HTML+CSS+JavaScript 迷宫生成算法 【建议收藏】

    最近发现很多人都在写算法类的博客,今天就献丑了使用HTML,CSS和JavaScript制作一个简单的迷宫生成小代码.证明了自己对一些简单的小算法还是可以驾驭的,基本功没有荒废. 迷宫生成有很多种算法 ...

  10. android迷宫生成算法,【Unity算法实现】简单回溯法随机生成 Tile Based 迷宫

    算法学习自 作者eastecho 在IndieNova上发表的文章 简单的使用回溯法生成 Tile Based 迷宫 , 我只是简单做了一下Unity的实现. 基础算法的简单说明 简单说明一下算法步骤 ...

最新文章

  1. brave浏览器_兼容Chrome 插件的Brave浏览器,带给你更快速的上网冲浪体验
  2. for in在python中什么意思_python for in中的in
  3. pytorch实现简易分类模型
  4. Python用泰勒公式模拟函数
  5. 让Win32窗口程序拥有控制台窗口
  6. 安装和卸载C#写的 windows service
  7. linux守护进程fifo,linux守护进程配置文件
  8. Python开发之--前端 HTML基础
  9. oracle 压力测试工具benchmarksql
  10. 浙大三维视觉团队提出Animatable NeRF,从RGB视频中重建可驱动人体模型 (ICCV'21)
  11. 注册Nocos配置中心失败:Could not resolve placeholder ‘config.info‘ in value “${config.info}
  12. Linux服务器创建及维护记录
  13. js怎么函数怎么给另一个函数传值并且不调用_2020年最火爆的Vue.js面试题
  14. c语言void nzp,二级C语言考试辅导教程第五章:函数[5]
  15. copula 重现期 matlab,合肥市干旱识别及基于Copula的特征值重现期分析
  16. 怎么检查计算机和打印机是否连接网络,检查电脑是否正确连接网络打印机
  17. 精品软件 推荐 淘宝 天猫 秒杀助手
  18. 前端JS-页面延迟刷新
  19. 南通全国计算机等级考试,南通大学2017年3月全国计算机等级考试报名通知
  20. 天数换算月份_EXCEL如何算出指定的月份有多少天计算方法

热门文章

  1. *Boosting*笔记
  2. BZOJ1086 [SCOI2005]王室联邦 【dfs + 贪心】
  3. codevs 1376 帕秋莉•诺蕾姬
  4. 利用正则来判断一个数字的范围
  5. Windows的Java_HOME环境变更配置
  6. Web前端优化最佳实践及工具集锦
  7. C#中as与is的用法
  8. angler前端框架_2019几大主流的前端框架,几款目前最热门的前端框架
  9. 苹果怎么换行打字_停课不停学!苹果电脑学习类软件推荐,丰富您的假期生活...
  10. mysql ignore index_mysql use index、ignore index、force index用法