对递归回溯生成随机迷宫的演示

news/2024/7/20 20:15:42 标签: 算法, 深度优先, python

回顾: 

[python实现] 递归回溯(深度优先)构造随机迷宫_☆迷茫狗子的秘密基地☆-CSDN博客icon-default.png?t=LA92https://blog.csdn.net/qq_39391544/article/details/121306611


在上次的基础上稍加改动,可以更加直观地欣赏整个过程

美中不足的是我想不停地原地输出并刷新,可惜找了很多文章都没能达到理想的效果,

希望有大佬有实现过类似的情况可以指点一二 hhh

 

 

python"># # -*- coding: utf-8 -*-
# """
# Created on Thu Nov 18 13:33:53 2021

# @author: Knight
# """

import sys
import time
import os
from enum import Enum
from random import randint, choice


class DIRECTOIN(Enum):
    UP = 0,
    LEFT = 1,
    DOWN = 2,
    RIGHT = 3,


class TYPE(Enum):
    EMPTY = 0,
    BLOCK = 1


class Map():
    def __init__(self, width, height):
        self.width = width
        self.height = height

        # 全部初始化成墙体
        self.map = [[1 for x in range(self.width)] for y in range(self.height)]

    def setMap(self, x, y, value):
        if value == TYPE.EMPTY:
            self.map[y][x] = 0
        elif value == TYPE.BLOCK:
            self.map[y][x] = 1

    def checkVisited(self, x, y):
        return self.map[y][x] != 1

    def showMap(self):
        for row in self.map:
            mp = ''
            for item in row:
                if item == 0:
                    mp += '  '
                elif item == 1:
                    mp += ' @'
            print(mp)


def checkPosition(map, x, y, w, h, waitingList):
    direction = []
    if y > 0:
        if not map.checkVisited(2 * x + 1, 2 * (y - 1) + 1):
            direction.append(DIRECTOIN.UP)
    if x > 0:
        if not map.checkVisited(2 * (x - 1) + 1, 2 * y + 1):
            direction.append(DIRECTOIN.LEFT)
    if y < h - 1:
        if not map.checkVisited(2 * x + 1, 2 * (y + 1) + 1):
            direction.append(DIRECTOIN.DOWN)
    if x < w - 1:
        if not map.checkVisited(2 * (x + 1) + 1, 2 * y + 1):
            direction.append(DIRECTOIN.RIGHT)

    if len(direction):
        # 随机选择方向
        direc = choice(direction)
        if direc == DIRECTOIN.UP:
            map.setMap(2 * x + 1, 2 * (y - 1) + 1, TYPE.EMPTY)
            map.setMap(2 * x + 1, 2 * y, TYPE.EMPTY)
            waitingList.append((x, y - 1));
        elif direc == DIRECTOIN.LEFT:
            map.setMap(2 * (x - 1) + 1, 2 * y + 1, TYPE.EMPTY)
            map.setMap(2 * x, 2 * y + 1, TYPE.EMPTY)
            waitingList.append((x - 1, y));
        elif direc == DIRECTOIN.DOWN:
            map.setMap(2 * x + 1, 2 * (y + 1) + 1, TYPE.EMPTY)
            map.setMap(2 * x + 1, 2 * y + 2, TYPE.EMPTY)
            waitingList.append((x, y + 1));
        elif direc == DIRECTOIN.RIGHT:
            map.setMap(2 * (x + 1) + 1, 2 * y + 1, TYPE.EMPTY)
            map.setMap(2 * x + 2, 2 * y + 1, TYPE.EMPTY)
            waitingList.append((x + 1, y));

        map.showMap()
        sys.stdout.flush()
        time.sleep(0.05)
        return True
    else:
        return False


def recursive(map, w, h):
    x0, y0 = (randint(0, w - 1)), (randint(0, h - 1))
    map.setMap(2 * x0 + 1, 2 * y0 + 1, TYPE.EMPTY)

    waitingList = []
    waitingList.append((x0, y0))
    cnt = 0
    while len(waitingList):
        if not checkPosition(map, waitingList[-1][0], waitingList[-1][1], w, h, waitingList):
            waitingList.remove(waitingList[-1])


# def setStartEnd(map):
#     x0, y0 =

# 开始构造迷宫
def startCreateMap(map):
    recursive(map, map.width // 2, map.height // 2)
    # setStartEnd(map)


def run():
    WIDTH = 31
    HEIGHT = 37
    map = Map(WIDTH, HEIGHT)
    startCreateMap(map)



if __name__ == "__main__":
    run()



http://www.niftyadmin.cn/n/816562.html

相关文章

typescript 教程集锦

https://www.ctolib.com/semlinker-awesome-typescript.html转载于:https://www.cnblogs.com/alww/p/10195055.html

C#学习随笔(一)

此篇截止到结构体,仅为一点归纳,主要参考 C# 教程 | 菜鸟教程 (runoob.com)https://www.runoob.com/csharp/csharp-tutorial.html Console.ReadKey(); 是针对 VS.NET 用户的。这使得程序会等待一个按键的动作&#xff0c;防止程序从 Visual Studio .NET 启动时屏幕会快速运行并…

H2数据库攻略

H2是一个开源的嵌入式数据库引擎&#xff0c;采用java语言编写&#xff0c;不受平台的限制&#xff0c;同时H2提供了一个十分方便的web控制台用于操作和管理数据库内容。H2还提供兼容模式&#xff0c;可以兼容一些主流的数据库&#xff0c;因此采用H2作为开发期的数据库非常方便…

[OpenGL学习笔记] 初识,环境配置

参考文档 Learn OpenGL, extensive tutorial resource for learning Modern OpenGLhttps://learnopengl.com/ 初识 早期的OpenGL使用立即渲染模式&#xff08;Immediate mode&#xff0c;也就是固定渲染管线&#xff09;,大多数功能都被库隐藏起来,虽然容易使用和理解&#…

SpringBoot动态配置加载

1、SpringBoot对配置文件集中化进行管理&#xff0c;方便进行管理&#xff0c;也可以使用HttpClient进行对远程的配置文件进行获取。 创建一个类实现EnvironmentPostProcessor 接口&#xff0c;然后可以对配置文件获取或者添加等等操作。 1 package com.bie.springboot;2 3 imp…

[OpenGL学习笔记] 窗口的创建&简单响应, 双缓冲的机理

目录 初始化窗口 双缓冲(Double Buffer)防止图形闪烁 第一个窗窗(#^.^#) LearnOpenGL - Hello Windowhttps://learnopengl.com/Getting-started/Hello-Window 初始化窗口 初始化GLFW&#xff0c;然后使用glfwWindowHint函数来配置GLFW, 若未顺利地链接GLFW库, 编译后会出现…

AcWing第26场周赛 4077. k显性字符

4077. k显性字符 - AcWing题库高质量的算法题库https://www.acwing.com/problem/content/4080/ 输入样例1&#xff1a; abacaba输出样例1&#xff1a; 2输入样例2&#xff1a; zzzzz输出样例2&#xff1a; 1输入样例3&#xff1a; abcde输出样例3&#xff1a; 3 要求k显性…

格式测试

一级标题 二级标题 三级标题 转载于:https://www.cnblogs.com/benjaminfee/p/10200493.html