迷宫最短路径算法(深度优先搜索)

算法介绍

介绍

1。访问起点;
2。依次从起点的未被访问的邻点出发,对迷宫进行深度优先遍历;直至迷宫中起点有路径相通的格子被访问或者找到终点;

编程思路

  • 1。访问起点,起点加入路径表。
  • 2。找可到达且未访问的相邻点。
    • 如果有:
    • 1。移动到相邻的点
    • 2。设置相邻点为当前点
    • 3。当前点加入路径
    • 4。判断是否为终点,否继续循环。
    • 没有:
    • 1。从路径列表中删除当前点
    • 2。设置当前点为路径列表中的最后一个点

图示

在这里插入图片描述

演示代码

#!/usr/bin/python3.7
# -*- coding: utf-8 -*-
import random
import pygame
#import depth_maze
import kruskal_maze
#import aldous_broder_maze

pygame.init()  # 初始化pygame
size = width, height = 800, 600  # 设置窗口大小
screen = pygame.display.set_mode(size)  # 显示窗口
# 颜色
diamond_color_size = 8
COLOR_RED, COLOR_BLUE, COLOR_GREEN, COLOR_YELLOW, COLOR_BLACK, COLOR_GREY, COLOR_GOLDEN, COLOR_NO_DIAMOND = list(range(
    diamond_color_size))
COLOR = {
    COLOR_RED: (255, 0, 0),
    COLOR_BLUE: (0, 0, 255),
    COLOR_GREEN: (0, 255, 0),
    COLOR_YELLOW: (255, 255, 0),
    COLOR_BLACK: (0, 0, 0),
    COLOR_GREY: (250, 240, 230),
    COLOR_GOLDEN : (255,215,0),
    COLOR_NO_DIAMOND: (100, 100, 100),
}
# 格子大小
DIAMOND_SIZE = (20, 20)
# 蓝格子
DIAMOND=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND.fill(COLOR[COLOR_BLUE])
# 绿格子 
DIAMOND_GREEN=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND_GREEN.fill(COLOR[COLOR_GREEN])
# 红格子 
DIAMOND_RED=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND_RED.fill(COLOR[COLOR_RED])
# 黄格子 
DIAMOND_YELLOW=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND_YELLOW.fill(COLOR[COLOR_YELLOW])
# 灰的格子 
DIAMOND_GREY=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND_GREY.fill(COLOR[COLOR_GREY])

# 字体
use_font = pygame.font.Font("FONT.TTF", 16)
# 背景
background=pygame.surface.Surface(size).convert()
background.fill(COLOR[COLOR_BLACK])
# 时间
clock = pygame.time.Clock()

##############################################
#   格子访问标记x,y,0,右墙x,y,1,下墙x,y,2
##############################################

# 

# 深度优先寻路演示代码
def depth_pathfinding(rows, cols, walls, startPoint=(0,0), endPoint=None):
    # walls = depth_maze.depth_maze(rows, cols)
    grids=[[ [0] for i in range(cols)]for j in range(rows)]
    # 标记迷宫
    findEndPoint=False
    # 起点
    r = startPoint[0]
    c = startPoint[1]
    # 终点
    if endPoint:
        stopPoint=endPoint
    else:
        stopPoint=(rows-1,cols-1)

    pathList=[(r,c)] # 路径
    grids[r][c][0]=1 # 1标记已经到过格子
    # 当存在未访问细胞时:
    while pathList and not findEndPoint:        
        move=None
        if r>0 and walls[r][c][1] and 1 != grids[r-1][c][0]:
            move = 'u'
            nr=r-1
            nc=c
        elif c>0 and walls[r][c][0] and 1 != grids[r][c-1][0] :
            move='l'
            nr=r
            nc=c-1
        elif c<cols-1 and walls[r][c+1][0] and 1 != grids[r][c+1][0] :
            move='r'
            nr=r
            nc=c+1
        elif r<rows-1 and walls[r+1][c][1] and 1 != grids[r+1][c][0] :
            move='d'
            nr=r+1
            nc=c
        if move:
            # 加入路径
            pathList.append((r,c))
            # move到下个点
            r=nr
            c=nc
            if (r,c) == stopPoint:
                # 终点加入路径
                pathList.append((r,c))
                findEndPoint=True
            else:
                grids[r][c][0]=1
        else:
            (r,c) = pathList.pop()
    return pathList


# 深度优先寻路演示代码
def depth_pathfinding_demo(rows, cols):
    #walls = depth_maze.depth_maze(rows, cols)
    walls = kruskal_maze.kruskal_maze(rows, cols)
    # fpath = depth_pathfinding(rows,cols, walls)
    POSX=40
    POSY=40
    # 墙 [0]表示格子访问标记,左[1]竖墙,上[2]横墙,最右边竖墙和最下边横墙没有记录。
    # (最左和最上墙不能打通,r,c右和r,c+1左共用墙。r,c和r+1,c共用横墙)
    # 初始化未访问,墙未打通
    grids=[[ [0,2,2] for i in range(cols)]for j in range(rows)]
    # 标记迷宫
    r=0
    c=0
    findEndPoint=False
    # 起点
    startPoint=(r,c)
    # 终点
    stopPoint=(rows-1,cols-1)
    pathList=[(r,c)] # 路径
    grids[r][c][0]=1 # 1标记已经到过格子
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
        # 当存在未访问细胞时:
        if  pathList and not findEndPoint:        
            move=None
            if r>0 and walls[r][c][1] and 1 != grids[r-1][c][0]:
                move = 'u'
                nr=r-1
                nc=c
            elif c>0 and walls[r][c][0] and 1 != grids[r][c-1][0] :
                move='l'
                nr=r
                nc=c-1
            elif c<cols-1 and walls[r][c+1][0] and 1 != grids[r][c+1][0] :
                move='r'
                nr=r
                nc=c+1
            elif r<rows-1 and walls[r+1][c][1] and 1 != grids[r+1][c][0] :
                move='d'
                nr=r+1
                nc=c
            if move:
                # 加入路径
                pathList.append((r,c))
                # move到下个点
                r=nr
                c=nc
                if (r,c) == stopPoint:
                    # 终点加入路径
                    pathList.append((r,c))
                    findEndPoint=True
                else:
                    grids[r][c][0]=1
            else:
                (r,c) = pathList.pop()



        screen.blit(background, (0, 0))
        # 格子
        for cx in range(cols):
            for ry in range(rows):
                px,py=POSX + 1 + (cx) * DIAMOND_SIZE[0], POSY + 1 + (ry) * DIAMOND_SIZE[1]
                # 标记访问过的格子
                if grids[ry][cx][0]:
                    screen.blit(DIAMOND, (px, py))
                else:
                    screen.blit(DIAMOND_GREY, (px, py))
        
        if pathList:
            # 路径
            for pos in pathList:
                px,py=POSX + 1 + (pos[1]) * DIAMOND_SIZE[0], POSY + 1 + (pos[0]) * DIAMOND_SIZE[1]
                screen.blit(DIAMOND_GREEN, (px, py))
            # 当前r,c 
            px,py=POSX + 1 + (c) * DIAMOND_SIZE[0], POSY + 1 + (r) * DIAMOND_SIZE[1]
            screen.blit(DIAMOND_RED, (px, py))
        # 画外墙
        pygame.draw.rect(screen, COLOR[COLOR_RED], (POSX + 0, POSY + 0, 20*cols+1, 20*rows+1), 2)
        # 画没打通的墙
        for cx in range( cols):
            for ry in range(rows):
                px,py=POSX + 1 + (cx) * DIAMOND_SIZE[0], POSY + 1 + (ry) * DIAMOND_SIZE[1]
                color = COLOR[COLOR_BLACK]
                color2 = COLOR[COLOR_GREY]
                color2 = COLOR[COLOR_RED]
                color3 = COLOR[COLOR_GREY]
                if 0 == walls[ry][cx][0]:
                    pygame.draw.line(screen, color, (px, py), (px, py+20), 2)
                if 0 == walls[ry][cx][1]:
                    pygame.draw.line(screen, color, (px, py), (px+20, py), 2)
                #if 2 == walls[ry][cx][0]:
                #    pygame.draw.line(screen, color2, (px, py), (px, py+20), 2)
                #if 2 == walls[ry][cx][1]:
                #    pygame.draw.line(screen, color2, (px, py), (px+20, py), 2)
                #if 1 == walls[ry][cx][0]:
                #    pygame.draw.line(screen, color3, (px, py), (px, py+20), 2)
                #if 1 == walls[ry][cx][1]:
                #    pygame.draw.line(screen, color3, (px, py), (px+20, py), 2)
        # 
        if findEndPoint:
            score_surface = use_font.render("找到终点", True, COLOR[COLOR_BLACK], COLOR[COLOR_GREY])
            screen.blit(score_surface, (POSX+50, POSY+rows*22))
        time_passed = clock.tick(30)

        pygame.display.update()
    return 



# main
if __name__ == "__main__":
    '''main'''
    depth_pathfinding_demo(20, 30)

GIF演示

在这里插入图片描述

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值