Problem p02579 (python)

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# ---------標準入力時--------------
h,w=map(int,readline().split())
ch, cw=map(int,readline().split())
dh, dw=map(int,readline().split())
m=list(map(lambda x: list(x.decode()), readlines()))
# ---------標準入力時--------------

ch-=1; cw-=1; dh-=1; dw-=1

ad = [(-1,0), (0,-1), (1,0), (0,1)]
warp = [(-2, -2),(-2, -1),(-2, 0),(-2, 1),(-2, 2),
 (-1, -2),(-1, -1),(-1, 1),(-1, 2),
 (0, -2),(0, 2),
 (1, -2),(1, -1),(1, 1),(1, 2),
 (2, -2),(2, -1),(2, 0),(2, 1),(2, 2)]

for i in range(h):
    m[i] = ['#']*2 + m[i] + ['#']*2
    
m = [['#']*(w+4)]*2 + m + [['#']*(w+4)]*2
ch+=2; cw+=2; dh+=2; dw+=2

def bfs4():
    '''
    bfs3とほぼ同じ. 迷路の上下左右に2列ずつ壁追加
    '''
    que1=[]
    que2=[] # queueというよりはメモリ
    m[ch][cw]=str(0)
    que1.append((ch,cw))

    while que1!=[]:
        l1=len(que1)
        r1=0 # que1における使用済みindex

        # que1の要素(座標)から歩いていけるマス
        while r1<l1:
            i, j=que1[r1]
            r1+=1
            # 隣接したマスへの移動
            for si, sj in ad:
                if m[i+si][j+sj]=='.': 
                    m[i+si][j+sj]=m[i][j]
                    que1.append((i+si, j+sj))
                    l1+=1

        # 歩いていけないマス
        for i,j in que1:
            for si, sj in warp:
                if m[i+si][j+sj]=='.':
                    m[i+si][j+sj]=str(int(m[i][j])+1)
                    que2.append((i+si, j+sj))

        que1=que2
        que2=[]
###---------------------------------------

bfs4()


if m[dh][dw] == '.':
    print(-1)
else:
    print(m[dh][dw])

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# ---------標準入力時--------------
h,w=map(int,readline().split())
ch, cw=map(int,readline().split())
dh, dw=map(int,readline().split())
m=list(map(lambda x: list(x.decode()), readlines()))
# ---------標準入力時--------------

ch-=1; cw-=1; dh-=1; dw-=1

ad = [(-1,0), (0,-1), (1,0), (0,1)]
warp = [(-2, -2),(-2, -1),(-2, 0),(-2, 1),(-2, 2),
 (-1, -2),(-1, -1),(-1, 1),(-1, 2),
 (0, -2),(0, 2),
 (1, -2),(1, -1),(1, 1),(1, 2),
 (2, -2),(2, -1),(2, 0),(2, 1),(2, 2)]

def bfs4():
    '''
    bfs3とほぼ同じ. 迷路の上下左右に2列ずつ壁追加
    '''
    for i in range(h):
        m[i] = ['#']*2 + m[i] + ['#']*2
    
    m = [['#']*(w+4)]*2 + m + [['#']*(w+4)]*2
    ch+=2; cw+=2; dh+=2; dw+=2
    
    que1=[]
    que2=[] # queueというよりはメモリ
    m[ch][cw]=str(0)
    que1.append((ch,cw))

    while que1!=[]:
        l1=len(que1)
        r1=0 # que1における使用済みindex

        # que1の要素(座標)から歩いていけるマス
        while r1<l1:
            i, j=que1[r1]
            r1+=1
            # 隣接したマスへの移動
            for si, sj in ad:
                if m[i+si][j+sj]=='.': 
                    m[i+si][j+sj]=m[i][j]
                    que1.append((i+si, j+sj))
                    l1+=1

        # 歩いていけないマス
        for i,j in que1:
            for si, sj in warp:
                if m[i+si][j+sj]=='.':
                    m[i+si][j+sj]=str(int(m[i][j])+1)
                    que2.append((i+si, j+sj))

        que1=que2
        que2=[]
###---------------------------------------

bfs4()


if m[dh][dw] == '.':
    print(-1)
else:
    print(m[dh][dw])

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

h,w=map(int,readline().split())
ch, cw=map(int,readline().split())
dh, dw=map(int,readline().split())
m=list(map(lambda x: list(x.decode()), readlines()))
ch-=1; cw-=1; dh-=1; dw-=1

def valid(i, j):
    if 0<=i<=h-1 and 0<=j<=w-1:
        return True
    else:
        return False

def bfs(i, j, ans):
    f=False
    if m[i][j]==".":
        m[i][j] = str(ans)
        f=True
    else:
        if int(m[i][j])>ans:
            m[i][j] = str(ans)
            f=True
            
    if (i,j)==(dh,dw):
        return 0
    
    if f:
        for si in range(-2, 3):
            for sj in range(-2, 3):
                if valid(i+si, j+sj) and m[i+si][j+sj]!='#':
                    if (si, sj) == (0, 0):
                        pass
                    elif (si, sj) == (-1, 0) or (si, sj) == (0, -1) or (si, sj) == (1, 0) or (si, sj) == (0, 1):
                        bfs(i+si, j+sj, ans)
                    else:
                        bfs(i+si, j+sj, ans+1)
    
bfs(ch, cw, 0)

if m[dh][dw] == '.':
    print(-1)
else:
    print(m[dh][dw])

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

h,w=map(int,readline().split())
ch, cw=map(int,readline().split())
dh, dw=map(int,readline().split())
m=list(map(lambda x: x.decode(), readlines()))
ch-=1; cw-=1; dh-=1; dw-=1

def valid(i, j):
    if 0<=i<=h-1 and 0<=j<=w-1:
        return True
    else:
        return False

def bfs(i, j, ans):
    f=False
    if m[i][j]==".":
        m[i]=m[i][:j]+str(ans)+m[i][j+1:]
        f=True
    else:
        if int(m[i][j])>ans:
            m[i]=m[i][:j]+str(ans)+m[i][j+1:]
            f=True
            
    if (i,j)==(dh,dw):
        return 0
    
    if f:
        for si in range(-2, 3):
            for sj in range(-2, 3):
                if valid(i+si, j+sj) and m[i+si][j+sj]!='#':
                    if (si, sj) == (0, 0):
                        pass
                    elif (si, sj) == (-1, 0) or (si, sj) == (0, -1) or (si, sj) == (1, 0) or (si, sj) == (0, 1):
                        bfs(i+si, j+sj, ans)
                    else:
                        bfs(i+si, j+sj, ans+1)
    
bfs(ch, cw, 0)

if m[dh][dw] == '.':
    print(-1)
else:
    print(m[dh][dw])

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

h,w=map(int,input().split())
ch, cw=map(int,input().split())
dh, dw=map(int,input().split())
m=[]
for i in range(h):
    m.append(input())
# h,w=map(int,readline().split())
# ch, cw=map(int,readline().split())
# dh, dw=map(int,readline().split())
# m=readlines()
ch-=1; cw-=1; dh-=1; dw-=1

ans=0

def valid(i, j):
    if 0<=i<=h-1 and 0<=j<=w-1:
        return True
    else:
        return False

def bfs(i, j, ans):
    #m[i][j]=str(ans)
    m[i]=m[i][:j]+str(ans)+m[i][j+1:]
    
    if (i,j)==(dh,dw):
        return 0
    
    if 0<=i-1 and (m[i-1][j]=='.' or (m[i-1][j]!='#' and int(m[i-1][j])>ans)):
        bfs(i-1, j, ans)
    if 0<=j-1 and (m[i][j-1]=='.' or (m[i][j-1]!='#' and int(m[i][j-1])>ans)):
        bfs(i, j-1, ans)
    if i+1<=h-1 and (m[i+1][j]=='.' or (m[i+1][j]!='#' and int(m[i+1][j])>ans)):
        bfs(i+1, j, ans)
    if j+1<=w-1 and (m[i][j+1]=='.' or (m[i][j+1]!='#' and int(m[i][j+1])>ans)):
        bfs(i, j+1, ans)
    
    for si in range(-2, 3):
        for sj in range(-2, 3):
            if valid(i+si, j+sj):
                if (si, sj) == (0, 0) or (si, sj) == (-1, 0) or (si, sj) == (0, -1) or (si, sj) == (1, 0) or (si, sj) == (0, 1):
                    pass
                else:
                    if m[i+si][j+sj]=='.' or (m[i+si][j+sj]!='#' and int(m[i+si][j+sj])>ans):
                        bfs(i+si, j+sj, ans+1)

bfs(ch, cw, 0)

print(m[dh][dw])