Problem p02579 (python)

Fixed submission:
# -*- coding: utf-8 -*-
from collections import deque
d = deque() # 初期化

H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Dh, Dw = map(int, input().split())
Shw = [input() for _ in range(H)]

d.appendleft((Ch-1, Cw-1, 0))
score = [[-1] * W for _ in range(H)]
score[Ch-1][Cw-1] = 0

walk = [(-1, 0), (1, 0), (0, -1), (0, 1)]
warp = list()
for i in range(-2, 3):
  for j in range(-2, 3):
    if (i, j) != (0, 0) and (i, j) not in walk:
      warp.append((i, j))

while len(d) > 0:
  
  y, x, s = d.popleft()
  
  # walk
  for i in range(len(walk)):
    dy, dx = walk[i]
    if 0 <= y+dy < H and 0 <= x+dx < W: 
      if score[y+dy][x+dx] == -1 or score[y+dy][x+dx] > s:
        if Shw[y+dy][x+dx] == ".":
          d.appendleft((y+dy, x+dx, s))
          score[y+dy][x+dx] = s
          
  # warp
  for i in range(len(warp)):
    dy, dx = warp[i]
    if 0 <= y+dy < H and 0 <= x+dx < W:
      if score[y+dy][x+dx] == -1 or score[y+dy][x+dx] > s+1:
        if Shw[y+dy][x+dx] == ".":
          d.append((y+dy, x+dx, s+1))
          score[y+dy][x+dx] = s+1

print(score[Dh-1][Dw-1])

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from collections import deque
d = deque() # 初期化

H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Dh, Dw = map(int, input().split())
Shw = [input() for _ in range(H)]

d.appendleft((Ch-1, Cw-1))
score = dict()
score[(Ch-1, Cw-1)] = 0

walk = [(-1, 0), (1, 0), (0, -1), (0, 1)]
warp = list()
for i in range(-2, 3):
  for j in range(-2, 3):
    if (i, j) != (0, 0) and (i, j) not in walk:
      warp.append((i, j))

while len(d) > 0:
  
  y, x = d.popleft()
  
  # walk
  for i in range(len(walk)):
    dy, dx = walk[i]
    if 0 <= y+dy < H and 0 <= x+dx < W: 
      if score[(y+dy, x+dx)] > score[(y, x)]:
        if Shw[y+dy][x+dx] == ".":
          d.appendleft((y+dy, x+dx))
          score[(y+dy, x+dx)] = score[(y, x)]
          
  # warp
  for i in range(len(warp)):
    dy, dx = warp[i]
    if 0 <= y+dy < H and 0 <= x+dx < W:
      if score[(y+dy, x+dx)] > score[(y, x)]+1:
        if Shw[y+dy][x+dx] == ".":
          d.append((y+dy, x+dx))
          score[(y+dy, x+dx)] = score[(y, x)]+1

if (Dh-1, Dw-1) in score:
  print(score[(Dh-1, Dw-1)])
else:
  print(-1)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from collections import deque
d = deque() # 初期化

H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Dh, Dw = map(int, input().split())
Shw = ["" for _ in range(W)]
for i in range(H):
  Shw[i] = input()

d.appendleft((Ch-1, Cw-1))
score = dict()
score[(Ch-1, Cw-1)] = 0

while len(d) > 0:
  
  here = d.popleft()
      
  # 右のマス
  if here[1] != W-1:
    if (here[0], here[1]+1) not in score or ((here[0], here[1]+1) in score and (score[(here[0], here[1]+1)] > score[here])):
      if Shw[here[0]][here[1]+1] == ".":
        d.appendleft((here[0], here[1]+1))
        score[(here[0], here[1]+1)] = score[here]

  # 下のマス
  if here[0] != H-1:
    if (here[0]+1, here[1]) not in score or ((here[0]+1, here[1]) in score and (score[(here[0]+1, here[1])] > score[here])):
      if Shw[here[0]+1][here[1]] == ".":
        d.appendleft((here[0]+1, here[1]))
        score[(here[0]+1, here[1])] = score[here]
  
  # 左のマス
  if here[1] != 0:
    if (here[0], here[1]-1) not in score or ((here[0], here[1]-1) in score and (score[(here[0], here[1]-1)] > score[here])):
      if Shw[here[0]][here[1]-1] == ".":
        d.appendleft((here[0], here[1]-1))
        score[(here[0], here[1]-1)] = score[here]
    
  # 上のマス
  if here[0] != 0:
    if (here[0]-1, here[1]) not in score or ((here[0]-1, here[1]) in score and (score[(here[0]-1, here[1])] > score[here])):
      if Shw[here[0]-1][here[1]] == ".":
        d.appendleft((here[0]-1, here[1]))
        score[(here[0]-1, here[1])] = score[here]
  
  # ワープのマス
  for i in range(here[0]-2, here[0]+3, 1):
    if i < 0 or i > H-1:
      continue
    for j in range(here[1]-2, here[1]+3, 1):
      if j < 0 or j > W-1 or (i, j) in score:
        continue
      else:
        if Shw[i][j] == ".":
          d.append((i, j))
          score[(i, j)] = score[here] + 1

#print(score)
if (Dh-1, Dw-1) in score:
  print(score[(Dh-1, Dw-1)])
else:
  print(-1)