/maze

用python和matplotlib库生成迷宫

Primary LanguagePython

用Python生成迷宫的源代码

算法简介

  1. 生成一张网格,把网格里面的所有都存进一个列表edgeList里面.
  2. (0, 0)开始,做DFS。每次DFS的时候,随机地选择四周一个没有走过的格子,凿墙过去,把道路打通。凿墙的时候,把edgeList列表中相对应的那堵墙删除掉。
  3. 将剩下的没有凿开过的墙画出来,就是一个完整的迷宫了。

Generate a maze using Python

A Brief Description of the Algorithm

  1. Generate a grid of a given width and height. Save all the edges into list named edgeList.
  2. Starting from the cell (0, 0), perform a DFS search. In each step, randomly pick a unvisited neighbour cell, and break the wall in between. More specifically, when breaking the wall, what we need to do is just to remove the corresponding edge from the edgeList.
  3. Sketch all the rest edges in the edgeList and then we may get a maze.

Execution

The program depends on the matplotlib module. You need to install the module first to run the code.

pip install matplotlib

To execute the code, just simply type:

python maze.py