【Tree】迷宫生成算法

参考维基百科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()

运行结果如下所示:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值