python实现二叉树以及解决“迷宫问题”

使用list实现栈
使用栈解决迷宫问题
#用list来实现栈
#定义顺序栈的类
class Zhan:
    #定义栈的基本
    def __init__(self):
        self.__zhan = []
    #定义显示栈的接口
    def see(self):
        return self.__zhan
    #定义入栈操作
    def push(self,object):
        self.__zhan.append(object)
    #定义出栈操作
    def pop(self):
        if len(self.__zhan) == 0:
            print("栈为空")
            return None
        else:
            return self.__zhan.pop()
    def top(self):
        if len(self.__zhan) == 0:
            print("栈为空")
            return None
        else:
            return self.__zhan[-1]
migong_zhan = Zhan()
migong_juzhen = \
    [[1,1,1,1,1,1,1,1,1],
     [1,0,1,0,1,1,1,1,1],
     [1,0,0,0,0,0,1,1,1],
     [1,0,0,0,1,0,1,1,1],
     [1,1,1,0,1,1,1,1,1],
     [1,1,1,0,0,0,0,1,1],
     [1,1,1,1,0,0,0,1,1],
     [1,1,1,1,1,1,0,0,1],
     [1,1,1,1,1,1,1,1,1]]
fuzhu_juzhen = \
    [[1,1,1,1,1,1,1,1,1],
     [1,0,1,0,1,1,1,1,1],
     [1,0,0,0,0,0,1,1,1],
     [1,0,0,0,1,0,1,1,1],
     [1,1,1,0,1,1,1,1,1],
     [1,1,1,0,0,0,0,1,1],
     [1,1,1,1,0,0,0,1,0],
     [1,1,1,1,1,1,0,0,0],
     [1,1,1,1,1,1,1,1,0]]
# 当前坐标
x = 1 #第几行
y = 1 #第几列
index = (x,y)  #这是起点
final = (7,7)  #终点是(8,8)
#开始运动:
migong_zhan.push(index)
while True:

    if index==(1,1):
        if fuzhu_juzhen[x-1][y]==1 and fuzhu_juzhen[x+1][y]==1 and fuzhu_juzhen[x][y-1]==1 and fuzhu_juzhen[x][y+1]==1:
            print("迷宫走不通")
            break
        if fuzhu_juzhen[x+1][y] ==0:
            fuzhu_juzhen[x][y] = 1
            x = x + 1
            migong_zhan.push((x, y))
            index = (x,y)
            continue
        elif fuzhu_juzhen[x][y + 1] == 0:
            fuzhu_juzhen[x][y] = 1
            y = y + 1
            migong_zhan.push((x, y))
            index = (x,y)
            continue

    elif index==final:
        print(migong_zhan.see())
        print("迷宫已走通")
        break
    else:
        if fuzhu_juzhen[x-1][y] == 0:
            fuzhu_juzhen[x][y]=1
            x = x-1
            migong_zhan.push((x,y))
            index = (x, y)

            continue
        elif fuzhu_juzhen[x+1][y] == 0:
            fuzhu_juzhen[x][y] = 1
            x = x+1
            migong_zhan.push((x,y))
            index = (x,y)

            continue
        elif fuzhu_juzhen[x][y-1] == 0:
            fuzhu_juzhen[x][y] = 1
            y = y-1
            migong_zhan.push((x,y))
            index = (x,y)

            continue
        elif fuzhu_juzhen[x][y+1] == 0:
            fuzhu_juzhen[x][y] = 1
            y = y+1
            migong_zhan.push((x,y))
            index = (x,y)

            continue
        else:
            fuzhu_juzhen[x][y]=1
            migong_zhan.pop()
            index = migong_zhan.top()
            x = index[0]
            y = index[1]





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值