python利用栈实现迷宫(非递归)

#本文通过利用栈后进先出的性质实现了迷宫路径的探索,采用了非递归的方式。
整个过程主要思想如下:

1、创建迷宫地图,可以用二维数组表示,01分别表示墙和路
2、设置迷宫的起点和终点
3、将起点push进保存路径的栈。从栈顶元素开始,搜寻其上下左右格子,如果可达,则将搜寻到的可达的格子push到当前路径中(并标记该格子已经遍历过),如果一个格子周围的四个格子均不可走,则将该格子从路径中pop()(并标记该格子已经遍历过)。重复上述过程

直到搜寻的格子==终点(找到可通行的迷宫路径),或者栈为空(不存在满足条件的路径)
具体代码如下:

# -*- coding:utf-8 -*-
# author :dyl time:2020/5/3

#创建迷宫
#0代表墙,1road
import numpy as np

def findPath(mazeMap,startPoint,endPoint):
    path = []  # 保存路径
    path.append(startPoint)
    mapSize = 10
    flag = np.zeros([mapSize, mapSize])  # 标记遍历过的路径
    if startPoint==endPoint:
        return  path
    flag[startPoint[0]][startPoint[1]] = 2
    direction = [(0, -1), (0, 1), (-1, 0), (1, 0)]  # 左右上下
    while len(path)>0:
        node=path[-1]
        find=0#标记node周围的点有没有可走路径,初始,没有
        for d in direction:
            i = node[0] + d[0]
            j = node[1] + d[1]
            if i >= 0 and i < mapSize and j >= 0 and j < mapSize and flag[i][j] == 0 and mazeMap[i][j] == 1:#节点可通
                if ([i, j] == endPoint):
                    return path
                else:
                    find=1
                    path.append([i, j])
                    flag[i][j] = 2
                    break
        if(find==0):
            path.pop()
    return []

主函数:

if __name__=="__main__":
    MazeMap=[[1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
         [1 , 1 , 0 , 0 , 1 , 1 , 1 , 0 , 1 , 0],
         [0 , 1 , 1 , 1 , 1 , 0 , 1 , 0 , 1 , 0],
         [0 , 0 , 1 , 0 , 0 , 0 , 1 , 1 , 1 , 0],
         [0 , 0 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0],
         [0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 0 , 0],
         [0 , 1 , 1 , 1 , 1 , 1 , 0 , 1 , 0 , 0],
         [0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 0],
         [0 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 1 , 0],
         [0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0]
         ]#10*10
    startPoint = [1, 0]
    endPoint = [9, 8]
    path=[]
    path=findPath(MazeMap,startPoint,endPoint)
    if path==[]:
        print("can not find the path")
    else:
        for p in path:
            print(p,'->',end='')
        print(endPoint)
        newMaze=MazeMap
        for L in path:#标记路径为2
            newMaze[L[0]][L[1]]=2
        for row in newMaze:
            for e in row:
                print('{:3}'.format(e),end='')
            print("\n")

运行结果如下:2代表找到的路径

[1, 0] ->[1, 1] ->[2, 1] ->[2, 2] ->[3, 2] ->[4, 2] ->[4, 3] ->[4, 4] ->[4, 5] ->[5, 5] ->[5, 6] ->[5, 7] ->[6, 7] ->[7, 7] ->[7, 8] ->[8, 8] ->[9, 8]
  1  0  0  0  0  0  0  0  0  0

  2  2  0  0  1  1  1  0  1  0

  0  2  2  1  1  0  1  0  1  0

  0  0  2  0  0  0  1  1  1  0

  0  0  2  2  2  2  0  0  0  0

  0  0  0  0  0  2  2  2  0  0

  0  1  1  1  1  1  0  2  0  0

  0  1  0  1  0  1  0  2  2  0

  0  1  1  1  0  0  0  0  2  0

  0  0  0  0  0  0  0  0  1  0

[Finished in 0.4s]
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值