#30817: python BFS, but TLE


forkidlai (forkidlai)

學校 : 不指定學校
編號 : 192336
來源 : [220.130.18.196]
最後登入時間 :
2024-06-03 11:17:14
d453. 三、最短距離 -- 98學年度板橋高中校內資訊學科能力競賽 | From: [211.21.129.5] | 發表日期 : 2022-06-14 11:52

n = int(input())
for i in range(n):
    r,c,r1,c1,r2,c2 = map(int,input().split())
    r1 -=1
    c1 -=1
    r2 -=1
    c2 -=1
    w = []
    for j in range(r):
        w.append([int(k) for k in input()])
    w[r1][c1] = 1
    #check top,bottom,left,right
    qlist = [[r1,c1]]
    qidx = 0
    minlen = 0
    while qidx < len(qlist):
        ridx,cidx = qlist[qidx]
        qidx +=1
        if ridx-1>=0:
            if w[ridx-1][cidx]!=1: 
                w[ridx-1][cidx] = w[ridx][cidx]+1
                if ridx-1==r2 and cidx==c2:
                    minlen = w[ridx-1][cidx]
                    break
                qlist.append([ridx-1,cidx])
        if ridx+1<r:
            if w[ridx+1][cidx]!=1: 
                w[ridx+1][cidx] = w[ridx][cidx]+1
                if ridx+1==r2 and cidx==c2:
                    minlen = w[ridx+1][cidx]
                    break
                qlist.append([ridx+1,cidx])
            
        if cidx-1>=0:
            if w[ridx][cidx-1]!=1: 
                w[ridx][cidx-1] = w[ridx][cidx]+1
                if ridx==r2 and cidx-1==c2:
                    minlen = w[ridx][cidx-1]
                    break    
                qlist.append([ridx,cidx-1])
            
        if cidx+1<c:
            if w[ridx][cidx+1]!=1: 
                w[ridx][cidx+1] = w[ridx][cidx]+1
                if ridx==r2 and cidx+1==c2:
                    minlen = w[ridx][cidx+1]
                    break        
                qlist.append([ridx,cidx+1]) 
        # print(w)              
    print(minlen)

 

 
#30821: Re: python BFS, but TLE


forkidlai (forkidlai)

學校 : 不指定學校
編號 : 192336
來源 : [220.130.18.196]
最後登入時間 :
2024-06-03 11:17:14
d453. 三、最短距離 -- 98學年度板橋高中校內資訊學科能力競賽 | From: [211.21.129.5] | 發表日期 : 2022-06-14 14:35

n = int(input())
for i in range(n):
    r,c,r1,c1,r2,c2 = map(int,input().split())
    r1 -=1
    c1 -=1
    r2 -=1
    c2 -=1
    w = []
    for j in range(r):
        w.append([int(k) for k in input()])
    w[r1][c1] = 1
    #check top,bottom,left,right
    qlist = [[r1,c1]]
    qidx = 0
    minlen = 0
    while qidx < len(qlist):
        ridx,cidx = qlist[qidx]
        qidx +=1
        if ridx-1>=0:
            if w[ridx-1][cidx]!=1: 
                w[ridx-1][cidx] = w[ridx][cidx]+1
                if ridx-1==r2 and cidx==c2:
                    minlen = w[ridx-1][cidx]
                    break
                qlist.append([ridx-1,cidx])
        if ridx+1
            if w[ridx+1][cidx]!=1: 
                w[ridx+1][cidx] = w[ridx][cidx]+1
                if ridx+1==r2 and cidx==c2:
                    minlen = w[ridx+1][cidx]
                    break
                qlist.append([ridx+1,cidx])
            
        if cidx-1>=0:
            if w[ridx][cidx-1]!=1: 
                w[ridx][cidx-1] = w[ridx][cidx]+1
                if ridx==r2 and cidx-1==c2:
                    minlen = w[ridx][cidx-1]
                    break    
                qlist.append([ridx,cidx-1])
            
        if cidx+1
            if w[ridx][cidx+1]!=1: 
                w[ridx][cidx+1] = w[ridx][cidx]+1
                if ridx==r2 and cidx+1==c2:
                    minlen = w[ridx][cidx+1]
                    break        
                qlist.append([ridx,cidx+1]) 
        # print(w)              
    print(minlen)

 

已找到bug
上下左右下一步,判斷是否要進qlist只需判斷 w[][] == 0的才要進qlist

 
ZeroJudge Forum