博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Tree】迷宫生成算法
阅读量:6842 次
发布时间:2019-06-26

本文共 2572 字,大约阅读时间需要 8 分钟。

参考维基百科

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  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算法:

 

一个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 maze12     Z = np.zeros(shape, dtype=bool)13     # Fill borders14     Z[0,:] = Z[-1,:] = 115     Z[:,0] = Z[:,-1] = 116     # Make isles17     for i in range(density):18         x, y = rnd(0,shape[1]//2)*2, rnd(0,shape[0]//2)*219         Z[y,x] = 120         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_] = 130                     Z[y_+(y-y_)//2, x_+(x-x_)//2] = 131                     x, y = x_, y_32     return Z33  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()

运行结果如下所示:

 

 

转载地址:http://ejzul.baihongyu.com/

你可能感兴趣的文章
理解引用
查看>>
[LintCode/LeetCode] Merge Two Sorted Lists
查看>>
CSS进阶篇--div中的内容垂直居中的五种方法
查看>>
Apache Tomcat 7.0.93 发布,开源 Java Web 应用服务器
查看>>
Unity制作即时战略游戏毕设
查看>>
微软小冰首席科学家武威解读 EMNLP 论文:聊天机器人的深度学习模型 ...
查看>>
凌动智行被纽交所暂停交易、未来还将被除名,已启动退市程序 ...
查看>>
hadoop3.x的安装
查看>>
记一次性能压测
查看>>
webpack4 中的最新 React全家桶实战使用配置指南! ...
查看>>
用ASP.NET做一个简单的数据流动展示
查看>>
声扬科技VoiceAI获千万级Pre-A轮融资,香港X科技基金领投 ...
查看>>
jenkins下载安装及插件使用配置
查看>>
九州云箭完成A轮融资,峰瑞资本投资
查看>>
工具,算法驱动嵌入式视觉快速发展
查看>>
门店订货及在线签名免费开源方案
查看>>
Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现
查看>>
Android底层学习之Linux基础
查看>>
手摸手教你写 Kubernetes 的 golang 服务
查看>>
JAVA学习day03
查看>>