Problem p02579 (python)

Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline

H, W = map(int, input().split())
Ch, Cw = map(lambda x: int(x) - 1, input().split())
Dh, Dw = map(lambda x: int(x) - 1, input().split())
S = [input().rstrip() for _ in range(H)]
visited = [[False] * W for _ in range(H)]
dq = deque([(0, Ch, Cw)])
while dq:
    d, i, j = dq.popleft()
    if (i, j) == (Dh, Dw):
        print(d)
        exit()
    if visited[i][j]:
        continue
    visited[i][j] = True
    for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
        ni, nj = i + di, j + dj
        if 0 <= ni < H and 0 <= nj < W and S[ni][nj] == '.':
            dq.appendleft((d, ni, nj))
    for di in range(-2, 3):
        for dj in range(-2, 3):
            if di == dj == 0:
                continue
            ni, nj = i + di, j + dj
            if 0 <= ni < H and 0 <= nj < W and S[ni][nj] == '.' and not visited[ni][nj]:
                dq.append((d + 1, ni, nj))
print(-1)

Buggy submission (verdict: Runtime Error):
import copy
import sys
from collections import deque
H, W = map(int,input().split())
C_h, C_w = list(map(lambda x: int(x) - 1, input().split()))
D = list(map(lambda x: int(x) - 1, input().split()))
S = [input() for _ in range(H)]
check_visited = [[False] * W for _ in range(H)]
dq = deque([[0, C_h, C_w]])
while dq:
   d, i, j = dq.popleft()
   if [i, j] == D:
      print(d)
      exit()
   if check_visited[i][j]:
      continue
   check_visited[i][j] = True
   for move_h, move_w in [[-1,0], [1,0], [0,-1], [0,1]]:
      i += move_h
      j += move_w
      if 0 <= i <= H-1 and 0 <= j <= W-1 and S[i][j] == '.' and (not check_visited[i][j]) :
         dq.appendleft([d, i, j])
   for move_h in range(-2, 3):
      for move_w in range(-2, 3):
         if [move_h, move_w] == [0, 0]:
            continue
         i += move_h
         j += move_w
         if 0 <= i <= H-1 and 0 <= j <= W-1 and S[i][j] == '.' and (not check_visited[i][j]) :
            dp.append([d+1, i, j])
print(-1)

Buggy submission (verdict: Runtime Error):
import copy
import sys
from collections import deque
H, W = map(int,input().split())
C_h, C_w = list(map(lambda x: int(x) - 1, input().split()))
D = list(map(lambda x: int(x) - 1, input().split()))
S = [input() for _ in range(H)]
check_visited = [[False] * W for _ in range(H)]
dq = deque([0, C_h, C_w])
while dq:
   d, i, j = dq.popleft()
   if [i, j] == D:
      print(d)
      exit()
   if check_visited[i][j]:
      continue
   check_visited[i][j] = True
   for move_h, move_w in [[-1,0], [1,0], [0,-1], [0,1]]:
      i += move_h
      j += move_c
      if 0 <= i <= H-1 and 0 =< j <= W-1 and S[i][j] == '.' and (not check_visited[i][j]) :
         dq.appendleft([d, i, j])
   for move_h in range(-2, 3):
      for move_c in range(-2, 3):
         if move_h, move_c == 0, 0:
            continue
         i += move_h
         j += move_c
         if 0 <= i <= H-1 and 0 =< j <= W-1 and S[i][j] == '.' and (not check_visited[i][j]) :
            dp.append([d+1, i, j])
print(-1)

Buggy submission (verdict: Runtime Error):
import copy
import sys
from collections import deque
H, W = map(int,input().split())
C_h, C_w = list(map(lambda x: int(x) - 1, input().split()))
D = list(map(lambda x: int(x) - 1, input().split()))
S = [input() for _ in range(H)]
check_visited = [[False] * W for _ in range(H)]
dq = deque([0, C_h, C_w])
while dq:
   d, i, j = dq.popleft()
   if [i, j] == D:
      print(d)
      exit()
   if check_visited[i][j]:
      continue
   check_visited[i][j] = True
   for move_h, move_w in [[-1,0], [1,0], [0,-1], [0,1]]:
      i += move_h
      j += move_c
      if 0 =< i <= H-1 and 0 =< j <= W-1 and (not check_visited[i][j]) and S[i][j] == '.':
         dq.appendleft([d, i, j])
   for move_h in range(-2, 3):
      for move_c in range(-2, 3):
         if move_h, move_c == 0, 0:
            continue
         i += move_h
         j += move_c
         if 0 =< i <= H-1 and 0 =< j <= W-1 and (not check_visited[i][j]) and S[i][j] == '.':
            dp.append([d+1, i, j])
print(-1)