Problem p02579 (python)

Fixed submission:
from collections import deque
f_inf = float('inf')
H, W = map(int, input().split())
sh, sw = map(int, input().split())
sh, sw = sh-1, sw-1
eh, ew = map(int, input().split())
eh, ew = eh-1, ew-1
S = [list(input()) for _ in range(H)]
m = [[f_inf]*W for _ in range(H)]
m[sh][sw] = 0
que = deque([])
que.appendleft([sh,sw])
while que:
  h, w = que.popleft()
  for dh, dw in ((1,0), (-1,0), (0, 1), (0, -1)):
    next_h, next_w = h + dh, w + dw
    if 0 <= next_h < H and 0 <= next_w < W and S[next_h][next_w] == ".":
        if m[next_h][next_w] > m[h][w]:
          m[next_h][next_w] = m[h][w] # 移動Aで行けるところにmの値を合わせる
          que.appendleft([next_h,next_w])
  for dh in range(-2, 3):
    for dw in range(-2, 3): # 移動Bで行ける範囲
      next_h, next_w = h + dh, w + dw
      if 0 <= next_h < H and 0 <= next_w < W and S[next_h][next_w] == ".":
        if m[next_h][next_w] > m[h][w] + 1: # h,wから移動Aですでに行けているところは更新されない
          m[next_h][next_w] = m[h][w] + 1 
          que.append([next_h, next_w]) # appendleftではなくappend 優先順位が後
print(m[eh][ew] if m[eh][ew] != f_inf else -1)

Buggy submission (verdict: Runtime Error):
from collections import deque
f_inf = float('inf')
H, W = map(int, input().split())
ch, cw = map(int, input().split())
ch, cw = ch-1, cw-1
dh, dw = map(int, input().split())
dh, dw = dh-1, dw-1
S = [list(input()) for _ in range(H)]
m = [[f_inf]*W for _ in range(H)]
m[ch][cw] = 0
que = deque([])
que.appendleft([ch,cw])
while que:
  h, w = que.popleft()
  for dh, dw in ((1,0), (-1,0), (0, 1), (0, -1)):
    next_h, next_w = h + dh, w + dw
    if 0 <= next_h < H and 0 <= next_w < W and S[next_h][next_w] == ".":
        if m[next_h][next_w] > m[h][w]:
          m[next_h][next_w] = m[h][w] # 移動Aで行けるところにmの値を合わせる
          que.appendleft([next_h,next_w])
  for dh in range(-2, 3):
    for dw in range(-2, 3): # 移動Bで行ける範囲
      next_h, next_w = h + dh, w + dw
      if 0 <= next_h < H and 0 <= next_w < W and S[next_h][next_w] == ".":
        if m[next_h][next_w] > m[h][w] + 1: # h,wから移動Aですでに行けているところは更新されない
          m[next_h][next_w] = m[h][w] + 1 
          que.append([next_h, next_w]) # appendleftではなくappend 優先順位が後
print(m[dh][dw] if m[dh][dw] != f_inf else -1)

Buggy submission (verdict: Runtime Error):
from collections import deque
f_inf = float('inf')
H, W = map(int, input().split())
ch, cw = map(int, input().split())
ch, cw = ch-1, cw-1
dh, dw = map(int, input().split())
dh, dw = dh-1, dw-1
S = [list(input()) for _ in range(H)]
m = [[f_inf]*W for _ in range(H)]
m[ch][cw] = 0
que = deque([])
que.appendleft([ch,cw])
while que:
  print(que)
  print(m)
  h, w = que.popleft()
  for dh, dw in ((1,0), (-1,0), (0, 1), (0, -1)):
    next_h, next_w = h + dh, w + dw
    if 0 <= next_h < H and 0 <= next_w < W and S[next_h][next_w] == ".":
        if m[next_h][next_w] > m[h][w]:
          m[next_h][next_w] = m[h][w] # 移動Aで行けるところにmの値を合わせる
          que.appendleft([next_h,next_w])
  for dh in range(-2, 3):
    for dw in range(-2, 3): # 移動Bで行ける範囲
      next_h, next_w = h + dh, w + dw
      if 0 <= next_h < H and 0 <= next_w < W and S[next_h][next_w] == ".":
        if m[next_h][next_w] > m[h][w] + 1: # h,wから移動Aですでに行けているところは更新されない
          m[next_h][next_w] = m[h][w] + 1 
          que.append([next_h, next_w]) # appendleftではなくappend 優先順位が後
print(m[dh][dw] if m[dh][dw] != f_inf else -1)

Buggy submission (verdict: Runtime Error):
from collections import deque
H, W = map(int, input().split())
C = list(map(int, input().split()))
D = list(map(int, input().split()))
S = [list(map(str, input())) for _ in range(H)]
T = [[-1 for _ in range(W)] for __ in range(H)] #その点にスタート地点から到達するためのワープの回数
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
ddx = [1,1,-1,-1,2,2,2,2,2,1,1,0,0,-1,-1,-2,-2,-2,-2,-2]
ddy = [1,-1,1,-1,-2,-1,0,1,2,2,-2,-2,2,-2,2,-2,-1,0,1,2]
def dfs(y,x,warp):
  # 移動Aで行けるところを埋める
  T[y][x] = warp
  for i in range(4):
    nx = x + dx[i]
    ny = y + dy[i]
    if 0 <= nx < W and 0 <= ny < H:
      if S[ny][nx] != "#" and T[ny][nx] == -1:
        dfs(ny,nx,warp)
  for i in range(len(ddx)):
    nx = x + ddx[i]
    ny = y + ddy[i]
    if 0 <= nx < W and 0 <= ny < H:
      if S[ny][nx] != "#" and T[ny][nx] == -1:
        dfs(ny,nx,warp+1)
  

dfs(C[0]-1,C[1]-1,0)
print(T[D[0]-1][D[1]-1])

Buggy submission (verdict: Runtime Error):
from collections import deque
H, W = map(int, input().split())
C = list(map(int, input().split()))
D = list(map(int, input().split()))
S = [list(map(str, input())) for _ in range(H)]
T = [[-1 for _ in range(W)] for __ in range(H)] #その点にスタート地点から到達するためのワープの回数
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
ddx = [1,1,-1,-1,2,2,2,2,2,1,1,0,0,-1,-1,-2,-2,-2,-2,-2]
ddy = [1,-1,1,-1,-2,-1,0,1,2,2,-2,-2,2,-2,2,-2,-1,0,1,2]
def dfs(y,x,warp):
  # 移動Aで行けるところを埋める
  T[y][x] = warp
  for i in range(4):
    nx = x + dx[i]
    ny = y + dy[i]
    if 0 <= nx < H and 0 <= ny < W:
      if S[ny][nx] != "#" and T[ny][nx] == -1:
        dfs(ny,nx,warp)
  for i in range(len(ddx)):
    nx = x + ddx[i]
    ny = y + ddy[i]
    if 0 <= nx < H and 0 <= ny < W:
      if S[ny][nx] != "#" and T[ny][nx] == -1:
        dfs(ny,nx,warp+1)
  

dfs(C[0]-1,C[1]-1,0)
print(T[D[0]-1][D[1]-1])

Buggy submission (verdict: Runtime Error):
from collections import deque
H, W = map(int, input().split())
C = list(map(int, input().split()))
D = list(map(int, input().split()))
S = [list(map(str, input())) for _ in range(H)]
T = [[-1 for _ in range(W)] for __ in range(H)] #その点にスタート地点から到達するためのワープの回数
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
ddx = [1,1,-1,-1,2,2,2,2,2,1,1,0,0,-1,-1,-2,-2,-2,-2,-2]
ddy = [1,-1,1,-1,-2,-1,0,1,2,2,-2,-2,2,-2,2,-2,-1,0,1,2]
def dfs(y,x,warp):
  # 移動Aで行けるところを埋める
  T[y][x] = warp
  for i in range(4):
    nx = x + dx[i]
    ny = y + dy[i]
    if 0 <= nx < W and 0 <= ny < H:
      if S[ny][nx] != "#" and T[ny][nx] == -1:
        dfs(ny,nx,warp)
  for i in range(len(ddx)):
    nx = x + ddx[i]
    ny = y + ddy[i]
    if 0 <= nx < W and 0 <= ny < H:
      if S[ny][nx] != "#" and T[ny][nx] == -1:
        dfs(ny,nx,warp+1)
  

dfs(C[0]-1,C[1]-1,0)
print(T[D[0]-1][D[1]-1])