Maze generator(迷宫生成器)

一、迷宫满足条件

  1. There are no circles in the maze, which means all roads in the maze lead to an dead end or to the exit.
  2. There are no wall blocks in the maze. Each wall is 1 unit in width, each road is also 1 unit in width.

二、具体流程

  1. if this cell is out of boundary, do not dig it, return to last step
  2. if this cell is already digged, do not dig it again, return to last step
  3. if there are more than 1 of its 4 neighbor cells are digged, do not dig it, return to last step (since it will cause circles in path)
  4. else dig it. and move to this cell, jump to step 1.
map = [][] # initialize the map with full of walls
for each (i, j) cell in map:
  DFS(i,j)

def DFS(i, j):
  if i, j is out of boundary:
    return
  if (i,j) is visited:
    return 
  if number of visited neighbors of (i, j) > 1:
    return 
  mark cell as visited
  for each neighbor (ni, nj) of cell(random order):
    DFS(ni, nj)

三、代码

#include<iostream>
#include<ctime>
#include<random>
using namespace std;

const int WIDTH = 20;
const int HEIGHT = 20;
class Solution {
private:
	char map[HEIGHT][WIDTH];
	int dir[4][2] = { {1,0},{0,1},{-1,0},{0,-1} };
public:
	void init() {
		for (int i = 0; i < HEIGHT; i++) {
			for (int j = 0; j < WIDTH; j++) {
				map[i][j] = '#';
			}
		}
		dfs(0, 0);
	}
	void dfs(int x, int y) {
		if (x < 0 || x >= HEIGHT || y < 0 || y >= WIDTH)
			return;
		if (map[x][y] == ' ')
			return;
		if (visitedNeighborNumber(x,y) > 1)
			return;
		map[x][y] = ' ';
		shuffle();
		for (int i = 0; i < 4; i++) {
			int tx = x + dir[i][1];
			int ty = y + dir[i][0];
			dfs(tx, ty);
		}
	}
	void shuffle() {
		std::default_random_engine e(time(0));
		std::uniform_int_distribution<int> idx(0,3);//闭区间
		for (int i = 0; i < 4; i++) {
			swap(dir[i], dir[idx(e)]);
		}
	}
	int visitedNeighborNumber(int x,int y) {
		int cnt = 0;
		for (int i = 0; i < 4; i++) {
			int tx = x + dir[i][1];
			int ty = y + dir[i][0];
			if (tx < 0 || tx >= HEIGHT || ty < 0 || ty >= WIDTH)
				continue;
			if (map[tx][ty] == ' ')
				cnt++;
		}
		return cnt;
	}
	void Show() {
		for (int i = 0; i < HEIGHT; i++) {
			for (int j = 0; j < WIDTH; j++) {
				cout << map[i][j] << " ";
			}
			cout << "\n";
		}
	}
};
int main() {
	Solution s = Solution();
	s.init();
	s.Show();
	return 0;
}

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Python生成随机迷宫的示例代码: ```python import random # 迷宫的大小 maze_size = 10 # 生成迷宫的二维数组 maze = [[1 for i in range(maze_size)] for j in range(maze_size)] # 设置起点和终点 start = (0, 0) end = (maze_size - 1, maze_size - 1) # 随机选择起点和终点 while start == end: start = (random.randint(0, maze_size - 1), random.randint(0, maze_size - 1)) end = (random.randint(0, maze_size - 1), random.randint(0, maze_size - 1)) # 将起点和终点设为通路 maze[start[0]][start[1]] = 0 maze[end[0]][end[1]] = 0 # 迷宫生成算法 def generate_maze(x, y): directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] random.shuffle(directions) for direction in directions: next_x = x + direction[0] next_y = y + direction[1] if next_x < 0 or next_x >= maze_size or next_y < 0 or next_y >= maze_size: continue if maze[next_x][next_y] == 1: maze[x + direction[0] // 2][y + direction[1] // 2] = 0 maze[next_x][next_y] = 0 generate_maze(next_x, next_y) # 从起点开始生成迷宫 generate_maze(start[0], start[1]) # 输出迷宫 for i in range(maze_size): for j in range(maze_size): if maze[i][j] == 0: print(" ", end="") else: print("#", end="") print() ``` 在这个示例中,我们使用递归回溯算法生成迷宫。该算法通过随机选择一个未访问的邻居结点,将它与当前结点之间的墙壁打通,然后将该邻居结点作为新的当前结点,并重复上述步骤,直到所有结点都被访问。 我们使用一个二维数组来表示迷宫,其中0表示通路,1表示墙壁。在迷宫生成过程中,我们首先随机选择起点和终点,并将它们设为通路。然后,从起点开始,递归调用generate_maze函数,该函数随机选择一个未访问的邻居结点,并将它与当前结点之间的墙壁打通。最后,我们输出生成的迷宫

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值