python小练1

int maze[5][5] = {

   0, 1, 0, 0, 0,

   0, 1, 0, 1, 0,

   0, 0, 0, 0, 0,

   0, 1, 1, 1, 0,

   0, 0, 0, 1, 0,

};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线和返回所有路线可以可行路线([[0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [1, 2], [0, 2], [0, 3], [0, 4], [1, 4], [2, 4], [2, 3], [2, 2]]这样的环路也返回)。
import numpy as np
mg = np.array([[0, 1, 0, 0, 0],
               [0, 1, 0, 1, 0],
               [0, 0, 0, 0, 0],
               [0, 1, 1, 1, 0],
               [0, 0, 0, 1, 0]])
[lenx, leny] = np.shape(mg)
pathes = []
#----------------------------------------------------------------返回非墙处---------------------------------------------
for i in range(lenx):
    for j in range(leny):
        if mg[i][j] == 1:
            continue
        else:
            pathes.append([i, j])
# print(pathes)
len_pathes = len(pathes)
# print(len_pathes)
#--------------------------------------------------------------查询表------------------------------------------------------
end_pathes=[]
for i in range(len_pathes):
    twopathes = [[pathes[i][0], pathes[i][1]]]
    if pathes[i][0] != 0 and [pathes[i][0] - 1, pathes[i][1]] in pathes:
        b = [pathes[i][0] - 1, pathes[i][1]]
        twopathes.append(b)
    if pathes[i][0] != lenx - 1 and [pathes[i][0] + 1, pathes[i][1]] in pathes:
        b =  [pathes[i][0] + 1, pathes[i][1]]
        twopathes.append(b)
    if pathes[i][1] != 0 and [pathes[i][0], pathes[i][1] - 1] in pathes:
        b =  [pathes[i][0], pathes[i][1] - 1]
        twopathes.append(b)
    if pathes[i][1] != leny - 1 and [pathes[i][0], pathes[i][1] + 1] in pathes:
        b =  [pathes[i][0], pathes[i][1] + 1]
        twopathes.append(b)
    end_pathes.append(twopathes)
set_pathes=[]
xz=[[0,0]]
pd=1
n_js=1
num=[]
num1=[]
bbt=0
#---------------------------------------------搜索路径--------------------------------------------------------------------------
//pd 为计数岔口的变动信息。
while pd!=0:
    #if bbt>=20:
        #break
    b=[]
    my=[]
    n_js=n_js+1
    for i in xz:
        pd=pd-1
        #print(pd)
        #print(i)
        #print(nn)
        leny=0
        nn=[]
        copp=[]
        if i == [0, 0]:
            copp.append(i)
            nn.append(i)
        else:
            for opm in i:
                copp.append(opm)
                nn.append(opm)
        #print(copp)
        if i==[0,0]:
            x_index = pathes.index(i)
        else:
            #print(i[-1])
            x_index = pathes.index(i[-1])
        while len(nn)!=leny:
            jsn=0
            bbt+=1
            leny=len(nn)
            uf1=0
            x1_index=pathes.index(nn[-1])
            if copp[-1] == [0, 0] or (len(end_pathes[x1_index]) == 3):
                jsn+=1
                for j in end_pathes[x1_index]:

                    if j not in copp:
                        nn.append(j)
                        #print(j)
                        copp.append(j)
                        x1_index = pathes.index(j)
                        break
                    if copp[-1] != [0, 0] and  nn[-2]!=j and nn[-1]!=j:
                        nn.append(j)
                        set_pathes.append(nn)
                        uf1=1
                        break
            if uf1==1:
                break

            if jsn==2:
                break
            if (len(end_pathes[x1_index]) == 2 and (copp[-1] != [0, 0]))or(copp[-1]==[lenx-1,leny-1]):
                set_pathes.append(nn)
                break
            #print(end_pathes[x1_index])
            #print( len(end_pathes[x1_index])  > 3)
            if len(end_pathes[x1_index]) > 3:
                bb=[]
                num1.append(copp[-1])
                for j in end_pathes[x1_index]:
                    #print(end_pathes[x1_index])
                    #print(j not in copp)
                    if (j not in copp) and (j not in b):
                        #print(j)
                        bb.append(j)
                        pd = pd + 1
                        continue

                b.append(bb)
                #print(b)
                #print(bb)
                my.append(nn)
                #print(nn)
                break
    xz=[]
    #print(b)
    #print(copp)
    for k,i in enumerate(my):
        #print(b[k])
        for j in b[k]:
            #print(j)
            ob=list.copy(i)
            ob.append(j)
            xz.append(ob)
    #[print(i) for i in xz]
    #[print(i) for i in set_pathes]
    #print(pd)
    con=len(b)
    num.append(con)
[print(i) for i in set_pathes]
#[[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [4, 1], [4, 2]]
#[[0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [1, 2], [0, 2], [0, 3], [0, 4], [1, 4], [2, 4], [3, 4], [4, 4]]
#[[0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [1, 2], [0, 2], [0, 3], [0, 4], [1, 4], [2, 4], [2, 3], [2, 2]]
#[[0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [1, 4], [0, 4], [0, 3], [0, 2], [1, 2], [2, 2]]
#[[0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [3, 4], [4, 4]]
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值