#53995: python 負索引為捨麼錯


tjmmy0123456789@gmail.com (12345)


通過的程式

n = int(input())
maze = [[0 if k == "." else -1 for k in input()] for i in range(n)]

spot = [(1,1)]
newspot=[]
maze[1][1] = 1
step = 1
ans = True

while True :

    if maze[n-2][n-2] == 1:
        break
    if spot == []:
        ans = False
        break

    for r,c in spot:
        if maze[r + 1][c] == 0:
            maze[r + 1][c] = 1
            newspot.append((r + 1, c)) 
        if maze[r][c + 1] == 0:
            maze[r][c + 1] = 1
            newspot.append((r, c + 1))
        if maze[r - 1][c] == 0:
            maze[r - 1][c] = 1
            newspot.append((r - 1, c))
        if maze[r][c - 1] == 0:
            maze[r][c - 1] = 1
            newspot.append((r, c - 1))    
    step += 1
    spot = newspot
    newspot = []

if ans :
    print(step)
else :
    print("No solution!")

沒通過的

n = int(input())
maze = [([0 if k == "." else -1 for k in input()]) for i in range(n)]

spot = [(1,1)]
newspot=[]
maze[1][1] = 1
step = 1
ans = True

while True :

    if maze[-2][-2] == 1:
        break
    if spot == []:
        ans = False
        break

    for r,c in spot:
        if maze[r + 1][c] == 0:
            maze[r + 1][c] = 1
            newspot.append((r + 1, c)) 
        if maze[r][c + 1] == 0:
            maze[r][c + 1] = 1
            newspot.append((r, c + 1))
        if maze[r - 1][c] == 0:
            maze[r - 1][c] = 1
            newspot.append((r - 1, c))
        if maze[r][c - 1] == 0:
            maze[r][c - 1] = 1
            newspot.append((r, c - 1))    
    step += 1
    spot, newspot = newspot, []
    
if ans :
    print(step)
else :
    print("No solution!")
#53996: Re: python 負索引為捨麼錯


tjmmy0123456789@gmail.com (12345)


通過的程式

n = int(input())
maze = [[0 if k == "." else -1 for k in input()] for i in range(n)]

spot = [(1,1)]
newspot=[]
maze[1][1] = 1
step = 1
ans = True

while True :

    if maze[n-2][n-2] == 1:
        break
    if spot == []:
        ans = False
        break

    for r,c in spot:
        if maze[r + 1][c] == 0:
            maze[r + 1][c] = 1
            newspot.append((r + 1, c)) 
        if maze[r][c + 1] == 0:
            maze[r][c + 1] = 1
            newspot.append((r, c + 1))
        if maze[r - 1][c] == 0:
            maze[r - 1][c] = 1
            newspot.append((r - 1, c))
        if maze[r][c - 1] == 0:
            maze[r][c - 1] = 1
            newspot.append((r, c - 1))    
    step += 1
    spot = newspot
    newspot = []

if ans :
    print(step)
else :
    print("No solution!")

沒通過的

n = int(input())
maze = [([0 if k == "." else -1 for k in input()]) for i in range(n)]

spot = [(1,1)]
newspot=[]
maze[1][1] = 1
step = 1
ans = True

while True :

    if maze[-2][-2] == 1:
        break
    if spot == []:
        ans = False
        break

    for r,c in spot:
        if maze[r + 1][c] == 0:
            maze[r + 1][c] = 1
            newspot.append((r + 1, c)) 
        if maze[r][c + 1] == 0:
            maze[r][c + 1] = 1
            newspot.append((r, c + 1))
        if maze[r - 1][c] == 0:
            maze[r - 1][c] = 1
            newspot.append((r - 1, c))
        if maze[r][c - 1] == 0:
            maze[r][c - 1] = 1
            newspot.append((r, c - 1))    
    step += 1
    spot, newspot = newspot, []
    
if ans :
    print(step)
else :
    print("No solution!")

只更改了

    if maze[n-2][n-2] == 1:

改成

    if maze[-2][-2] == 1:



#53998: Re: python 負索引為捨麼錯


pofly (不挖鼻孔有害身心健康)


因為測資檔可能包含多餘的空白字元

input() 不會忽略他們,而是保留

input() 時順便 rstrip() 就可以了