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

本文共 2642 字,大约阅读时间需要 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()

运行结果如下所示:

 

 

转载于:https://www.cnblogs.com/visayafan/archive/2012/04/11/2443173.html

你可能感兴趣的文章
灵巧还是笨重?利用Textarea从浏览器复制字符到剪贴板
查看>>
CentOS 7.4利用Iptables开启远程访问
查看>>
linux下日志监控分析工具webalizer的安装及使用
查看>>
HTTPS 原理详解
查看>>
[转载] 七龙珠第一部——第073话 必杀恶魔光线
查看>>
Get和Post请求区别
查看>>
开源史上最成功的8个开源产品
查看>>
PHP操作MySQL
查看>>
单臂路由与三层交换机动态配置
查看>>
MyBatis学习总结(六)——调用存储过程
查看>>
Docker命令详解
查看>>
servlet jsp 各种路径获取。。
查看>>
应用系统架构设计
查看>>
【POJ】第二章 简单计算题之课后题
查看>>
MFC建立应用程序启示录(创世纪新篇)
查看>>
自定义实现session持久化
查看>>
如何处理xencenter中无法显示vm的performance信息
查看>>
oracle远程导出导入
查看>>
Citrix XenApp/XenDesktop产品发布策略调整
查看>>
我的友情链接
查看>>