Problem:


Score : 400 points

Problem Statement

A maze is composed of a grid of H \times W squares - H vertical, W horizontal.

The square at the i-th row from the top and the j-th column from the left - (i,j) - is a wall if S_{ij} is # and a road if S_{ij} is ..

There is a magician in (C_h,C_w). He can do the following two kinds of moves:

Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in.

Move B: Use magic to warp himself to a road square in the 5\times 5 area centered at the square he is currently in.

In either case, he cannot go out of the maze.

At least how many times does he need to use the magic to reach (D_h, D_w)?

Constraints

1 \leq H,W \leq 10^3

1 \leq C_h,D_h \leq H

1 \leq C_w,D_w \leq W

S_{ij} is # or ..

S_{C_h C_w} and S_{D_h D_w} are ..

(C_h,C_w) \neq (D_h,D_w)

Input

Input is given from Standard Input in the following format:

H W
C_h C_w
D_h D_w
S_{11}\ldots S_{1W}
\vdots
S_{H1}\ldots S_{HW}

Output

Print the minimum number of times the magician needs to use the magic. If he cannot reach (D_h,D_w), print -1 instead.

Sample Input 1

4 4
1 1
4 4
..#.
..#.
.#..
.#..

Sample Output 1

1

For example, by walking to (2,2) and then using the magic to travel to (4,4), just one use of magic is enough.

Note that he cannot walk diagonally.

Sample Input 2

4 4
1 4
4 1
.##.
####
####
.##.

Sample Output 2

-1

He cannot move from there.

Sample Input 3

4 4
2 2
3 3
....
....
....
....

Sample Output 3

0

No use of magic is needed.

Sample Input 4

4 5
1 2
2 5
#.###
####.
#..##
#..##

Sample Output 4

2



Sample input:
4 4
1 1
4 4
..#.
..#.
.#..
.#..


Sample output:
1

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

from collections import deque

q = deque()

h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())

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

# mylist = [[]]
ban_list = [[-1 for j in range(w)] for i in range(h)]
cost_list = [[-1 for j in range(w)] for i in range(h)]

# print(mylist)

for i in range(h):
  # 文字列の入力
  s = input()
  for j in range(len(s)):
    ban_list[i][j] = s[j]

ban_list[ch][cw] = 0
# cost_list[ch][cw] = 0

cost = 0
ph = ch
pw = cw

q.append([ph, pw])


# # 最後デバッグ
# for li in ban_list:
#   print(li)



loop = True
while loop:
  # print(len(q))
  ph, pw = q.popleft()
  cost = ban_list[ph][pw]

  # print(ph, pw)
  # print(cost)
  # 上
  if ph == 0:
    pass
  else:
    if ban_list[ph-1][pw] != "#" and ban_list[ph-1][pw] != cost:
      if ban_list[ph-1][pw] == "." or cost < ban_list[ph-1][pw]:
        ban_list[ph-1][pw] = cost
        q.append([ph-1, pw])
    else:
      pass

  # 右
  if pw == w -1:
    pass
  else:
    if ban_list[ph][pw + 1] != "#" and ban_list[ph][pw + 1] != cost:
      if ban_list[ph][pw + 1] == "." or cost < ban_list[ph][pw + 1]:
        ban_list[ph][pw + 1] = cost
        q.append([ph, pw+1])
    else:
      pass

  # 下
  if ph == h -1:
    pass
  else:
    if ban_list[ph+1][pw] != "#" and ban_list[ph+1][pw] != cost:
      if ban_list[ph+1][pw] == "." or cost < ban_list[ph+1][pw]:
        ban_list[ph+1][pw] = cost
        q.append([ph+1, pw])
    else:
      pass

  # 左
  if pw == 0:
    pass
  else:
    if ban_list[ph][pw - 1] != "#" and ban_list[ph][pw - 1] != cost:
      if ban_list[ph][pw - 1] == "." or cost < ban_list[ph][pw - 1]:
        ban_list[ph][pw - 1] = cost
        q.append([ph, pw-1])
    else:
      pass


  # ジャンプ--------------
  for i in range(-2, 3):
    if (ph + i < 0) or (h - 1 < ph + i):
      continue

    for j in range(-2, 3):
      if (pw + j < 0) or (w - 1 < pw + j) or (i==0 and j == 0) or (i==0 and j == -1) or (i==0 and j == 1) or (i==-1 and j == 0) or (i==1 and j == 0):
        continue
      if ban_list[ph + i][pw + j] == ".":
        # print(ph, pw)
        # print(i, j)
        # print(cost)
        ban_list[ph + i][pw + j] = cost + 1
        q.append([ph + i, pw + j])

  if len(q) == 0:
    loop = False


result = ban_list[dh][dw]
if result == ".":
  result = -1

# # 最後デバッグ
# for li in ban_list:
#   print(li)

print(result)

Submission status: Wrong Answer

Solution code:
H,W = [int(a) for a in input().split()]
s = [int(a) for a in input().split()]
e = [int(a) for a in input().split()]
S = [None]*H
walk=[[] for _ in range(H*W)]
warp=[[] for _ in range(H*W)]
for i in range(H):
    S[i] = [1 if c=='.' else 0 for c in input()]
#print( H,W,s,e,S,sep='\n')
#print(walk,warp,sep='\n')
def check_walk(n):
    i=n//W
    j=n%W
    if i>0 and S[i-1][j]:waiting_warp.append((i-1)*W+j)
    if i<H-1 and S[i+1][j]:waiting_warp.append((i+1)*W+j)
    if j>0 and S[i][j-1]:waiting_warp.append(i*W+j-1)
    if j<W-1 and S[i][j+1]:waiting_warp.append(i*W+j+1)
def check_warp(n):
    i=n//W
    j=n%W
    if i>0 and j>0 and S[i-1][j-1]:waiting_warp.append((i-1)*W+j-1)
    if i>0 and j<W-1 and S[i-1][j+1]:waiting_warp.append((i-1)*W+j+1)
    if i<H-1 and j>0 and S[i+1][j-1]:waiting_warp.append((i+1)*W+j-1)
    if i<H-1 and j<W-1 and S[i+1][j+1]:waiting_warp.append((i+1)*W+j+1)
    if i>1 and S[i-2][j]:waiting_warp.append((i-2)*W+j)
    if i>1 and j<W-1 and S[i-2][j+1]:waiting_warp.append((i-2)*W+j+1)
    if i>1 and j<W-2 and S[i-2][j+2]:waiting_warp.append((i-2)*W+j+2)
    if i>0 and j<W-2 and S[i-1][j+2]:waiting_warp.append((i-1)*W+j+2)
    if j<W-2 and S[i][j+2]:waiting_warp.append(i*W+j+2)
    if i<H-1 and j<W-2 and S[i+1][j+2]:waiting_warp.append((i+1)*W+j+2)
    if i<H-2 and j<W-2 and S[i+2][j+2]:waiting_warp.append((i+2)*W+j+2)
    if i<H-2 and j<W-1 and S[i+2][j+1]:waiting_warp.append((i+2)*W+j+1)
    if i<H-2 and S[i+2][j]:waiting_warp.append((i+2)*W+j)
    if i<H-2 and j>0 and S[i+2][j-1]:waiting_warp.append((i+2)*W+j-1)
    if i<H-2 and j>1 and S[i+2][j-2]:waiting_warp.append((i+2)*W+j-2)
    if i<H-1 and j>1 and S[i+1][j-2]:waiting_warp.append((i+1)*W+j-2)
    if j>1 and S[i][j-2]:waiting_warp.append(i*W+j-2)
    if i>0 and j>1 and S[i-1][j-2]:waiting_warp.append((i-1)*W+j-2)
    if i>1 and j>1 and S[i-2][j-2]:waiting_warp.append((i-2)*W+j-2)
    if i>1 and j>0 and S[i-2][j-1]:waiting_warp.append((i-2)*W+j-1)
#print(walk)
#print(warp)
from collections import deque
done = [0]*(H*W)
waiting_walk = deque()
waiting_warp = deque()
start=(s[0]-1)*W+s[1]-1
end=(e[0]-1)*W+e[1]-1
done[start] = 1
#print(start)
cnt=0
check_warp(start)
check_walk(start)
warping=0
if len(waiting_walk)==0:warping=1
found=0
while len(waiting_walk) or len(waiting_warp):
    if warping:
        #print("warping")
        for i in range(len(waiting_warp)):
            to=waiting_warp[i]
            #print(to)
            if to==end:
                found=1
                cnt+=1
                break
            if done[to]!=1:
                waiting_walk.append(to)
        if to==end:break
        warping=0
        cnt+=1
        waiting_warp.clear()
    else:
        cur_node = waiting_walk.popleft()
        if done[cur_node]!=1:
            done[cur_node]=1
            #print(cur_node)
            if cur_node==end:
                found=1
                #print("found")
                break
            check_warp(cur_node)
            check_walk(cur_node)
        if len(waiting_walk)==0:warping=1
if found:print(cnt)
else:print(-1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

from collections import deque

q = deque()

h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())

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

# mylist = [[]]
ban_list = [[-1 for j in range(w)] for i in range(h)]
cost_list = [[-1 for j in range(w)] for i in range(h)]

# print(mylist)

for i in range(h):
  # 文字列の入力
  s = input()
  for j in range(len(s)):
    ban_list[i][j] = s[j]

ban_list[ch][cw] = 0
# cost_list[ch][cw] = 0

cost = 0
ph = ch
pw = cw

q.append([ph, pw])


# # 最後デバッグ
# for li in ban_list:
#   print(li)



loop = True
while loop:
  # print(len(q))
  ph, pw = q.popleft()
  cost = ban_list[ph][pw]

  # print(ph, pw)
  # print(cost)
  # 上
  if ph == 0:
    pass
  else:
    if ban_list[ph-1][pw] != "#" and ban_list[ph-1][pw] != cost:
      if ban_list[ph-1][pw] == "." or cost < ban_list[ph-1][pw]:
        ban_list[ph-1][pw] = cost
        q.append([ph-1, pw])
    else:
      pass

  # 右
  if pw == w -1:
    pass
  else:
    if ban_list[ph][pw + 1] != "#" and ban_list[ph][pw + 1] != cost:
      if ban_list[ph][pw + 1] == "." or cost < ban_list[ph][pw + 1]:
        ban_list[ph][pw + 1] = cost
        q.append([ph, pw+1])
    else:
      pass

  # 下
  if ph == h -1:
    pass
  else:
    if ban_list[ph+1][pw] != "#" and ban_list[ph+1][pw] != cost:
      if ban_list[ph+1][pw] == "." or cost < ban_list[ph+1][pw]:
        ban_list[ph+1][pw] = cost
        q.append([ph+1, pw])
    else:
      pass

  # 左
  if pw == 0:
    pass
  else:
    if ban_list[ph][pw - 1] != "#" and ban_list[ph][pw - 1] != cost:
      if ban_list[ph][pw - 1] == "." or cost < ban_list[ph][pw - 1]:
        ban_list[ph][pw - 1] = cost
        q.append([ph, pw-1])
    else:
      pass


  # ジャンプ--------------
  for i in range(-2, 3):
    if (ph + i < 0) or (h - 1 < ph + i):
      continue
    for j in range(-2, 3):
      if (pw + j < 0) or (w - 1 < pw + j) or (i==0 and j == 0):
        continue
      if ban_list[ph + i][pw + j] == ".":
        # print(ph, pw)
        # print(i, j)
        # print(cost)
        ban_list[ph + i][pw + j] = cost + 1
        q.append([ph + i, pw + j])

  if len(q) == 0:
    loop = False


result = ban_list[dh][dw]
if result == ".":
  result = -1

# # 最後デバッグ
# for li in ban_list:
#   print(li)

print(result)

Submission status: Wrong Answer

Solution code:
from collections import deque
h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())
maze = [[9]*(w+4)]
maze.append([9]*(w+4))
for i in range(h):
  s = input()
  tmp = [9,9]
  for i in s:
    if i == "#":
      tmp.append(9)
    else:
      tmp.append(0)
  tmp.append(9)
  tmp.append(9)
  maze.append(tmp)
maze.append([9]*(w+4))
maze.append([9]*(w+4))
#print(maze)

pos = deque()
pos.append([ch+1,cw+1,0])
while len(pos)>0:
  x, y, hop = pos.popleft()
  print(pos)
  #print[x,y,hop)
  if (x,y) == (dh+1,dw+1):
    print(hop)
    break
  maze[x][y] = 2
  
  if maze[x-1][y] <2:
    pos.appendleft([x-1,y,hop])
    
  if maze[x+1][y] <2:
    pos.appendleft([x+1,y,hop])
    
  if maze[x][y-1] <2:
    pos.appendleft([x,y-1,hop])
    
  if maze[x][y+1] <2:
    pos.appendleft([x,y+1,hop])


  move =[(-2,0),(-2,1),(-2,2),(-2,-1),(-2,-2),(2,0),(2,1),(2,2),(2,-1),(2,-2),(0,-2),(-1,-2),(1,-2),(0,2),(-1,2),(1,2),(-1,1),(-1,-1),(1,1),(1,-1)]
  for dx,dy in move:
    if maze[x+dx][y+dy] <2:
      if ([x+dx,y+dy,hop+1] not in pos):
        pos.append([x+dx,y+dy,hop+1])
  
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque
h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())
maze = [[9]*(w+4)]
maze.append([9]*(w+4))
for i in range(h):
  s = input()
  tmp = [9,9]
  for i in s:
    if i == "#":
      tmp.append(9)
    else:
      tmp.append(0)
  tmp.append(9)
  tmp.append(9)
  maze.append(tmp)
maze.append([9]*(w+4))
maze.append([9]*(w+4))
#print(maze)

pos = deque()
pos.append([ch+1,cw+1,0])
while len(pos)>0:
  x, y, hop = pos.popleft()
  print(pos)
  #print[x,y,hop)
  if (x,y) == (dh+1,dw+1):
    print(hop)
    break
  maze[x][y] = 2
  
  if maze[x-1][y] <2:
    pos.appendleft([x-1,y,hop])
    
  if maze[x+1][y] <2:
    pos.appendleft([x+1,y,hop])
    
  if maze[x][y-1] <2:
    pos.appendleft([x,y-1,hop])
    
  if maze[x][y+1] <2:
    pos.appendleft([x,y+1,hop])


  move =[(-2,0),(-2,1),(-2,2),(-2,-1),(-2,-2),(2,0),(2,1),(2,2),(2,-1),(2,-2),(0,-2),(-1,-2),(1,-2),(0,2),(-1,2),(1,2),(-1,1),(-1,-1),(1,1),(1,-1)]
  for dx,dy in move:
    if maze[x+dx][y+dy] <2:
      if ([x+dx,y+dy,hop+1] not in pos) or ([x-dx,y,hop] not in pos):
        pos.append([x+dx,y+dy,hop+1])
  
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque
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=[]
for i in range(H):
    S.append(list(input()))
INF=10**9
path=[[INF for j in  range(W)]for i in range(H)]
q=deque()
path[Ch][Cw]=0
q.append((Ch,Cw,0))

move_a=[(0,1),(0,-1),(1,0),(1,-1)]
move_b=[(i, j) for i in range(-2, 3) for j in range(-2, 3) if (i, j) not in [(0,0)]+move_a]

while q:
    x,y,cost=q.popleft()
    for dx,dy in move_a:
        next_x=x+dx
        next_y=y+dy
        if(0<next_x<H and 0<next_y<W and S[next_x][next_y]=="."and path[next_x][next_y]>cost):
            path[next_x][next_y]=cost
            q.append((next_x,next_y,cost))
    for dx,dy in move_b:
        next_x=x+dx
        next_y=y+dy
        if(0<next_x<H and 0<next_y<W and S[next_x][next_y]=="."and path[next_x][next_y]>cost+1):
            path[next_x][next_y]=cost+1
            q.append((next_x,next_y,cost+1))
ans=path[Dh][Dw]  if path[Dh][Dw] < INF else '-1'
print(ans)

Submission status: Wrong Answer

Solution code:
import math
from itertools import product
from collections import deque

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() for _ in range(h)]

reached = [[False for _ in range(w)] for _ in range(h)]
distance = [[math.inf for _ in range(w)] for _ in range(h)]
distance[ch][cw] = 0
queue = deque([(ch, cw)])

while queue:
    i, j = queue.popleft()
    reached[i][j] = True
    adj = []
    if i > 0 and S[i - 1][j] == '.':
        adj.append((i-1, j))
    if j > 0 and S[i][j - 1] == '.':
        adj.append((i, j - 1))
    if i < h-1 and S[i + 1][j] == '.':
        adj.append((i+1, j))
    if j < w-1 and S[i][j+1] == '.':
        adj.append((i, j+1))
    for u, v in adj:
        if not reached[u][v]:
            distance[u][v] = distance[i][j]
            queue.appendleft((u,v))

    warp = product(range(max(i-2, 0), min(i+3, h)),
                   range(max(j-2, 0), min(j+3, w)))
    for x, y in warp:
        if (x,y) != (i,j) and (x,y) not in adj and S[x][y] == '.' and not reached[x][y]:
            distance[x][y] = distance[i][j] + 1
            queue.append((x,y))

print(distance[dh][dw] if distance[dh][dw] != math.inf else -1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

from collections import deque

q = deque()

h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())

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

# mylist = [[]]
ban_list = [[-1 for j in range(w)] for i in range(h)]
cost_list = [[-1 for j in range(w)] for i in range(h)]

# print(mylist)

for i in range(h):
  # 文字列の入力
  s = input()
  for j in range(len(s)):
    ban_list[i][j] = s[j]

ban_list[ch][cw] = 0
# cost_list[ch][cw] = 0

cost = 0
ph = ch
pw = cw

q.append([ph, pw])


# # 最後デバッグ
# for li in ban_list:
#   print(li)



loop = True
while loop:
  # print(len(q))
  ph, pw = q.popleft()
  cost = ban_list[ph][pw]

  # print(ph, pw)
  # print(cost)
  # 上
  if ph == 0:
    pass
  else:
    if ban_list[ph-1][pw] != "#" and ban_list[ph-1][pw] != cost:
      ban_list[ph-1][pw] = cost
      q.append([ph-1, pw])
    else:
      pass

  # 右
  if pw == w -1:
    pass
  else:
    if ban_list[ph][pw + 1] != "#" and ban_list[ph][pw + 1] != cost:
      ban_list[ph][pw + 1] = cost
      q.append([ph, pw+1])
    else:
      pass

  # 下
  if ph == h -1:
    pass
  else:
    if ban_list[ph+1][pw] != "#" and ban_list[ph+1][pw] != cost:
      ban_list[ph+1][pw] = cost
      q.append([ph+1, pw])
    else:
      pass

  # 左
  if pw == 0:
    pass
  else:
    if ban_list[ph][pw - 1] != "#" and ban_list[ph][pw - 1] != cost:
      ban_list[ph][pw - 1] = cost
      q.append([ph, pw-1])
    else:
      pass


  # ジャンプ--------------
  for i in range(-2, 3):
    if (ph + i < 0) or (h - 1 < ph + i):
      continue
    for j in range(-2, 3):
      if (pw + j < 0) or (w - 1 < pw + j) or (i==0 and j == 0):
        continue
      if ban_list[ph + i][pw + j] == ".":
        # print(ph, pw)
        # print(i, j)
        # print(cost)
        ban_list[ph + i][pw + j] = cost + 1
        q.append([ph + i, pw + j])

  if len(q) == 0:
    loop = False


result = ban_list[dh][dw]
if result == ".":
  result = -1

# # 最後デバッグ
# for li in ban_list:
#   print(li)

print(result)

Submission status: Wrong Answer

Solution code:
import math
from itertools import product
from collections import deque

h, w = map(int, input().split())
c = tuple(map(lambda x: int(x) - 1, input().split()))
d = tuple(map(lambda x: int(x) - 1, input().split()))
S = {}
roads = set()
for i in range(h):
    s = input()
    for j in range(w):
        S[i, j] = s[j]
        if s[j] == '.':
            roads.add((i, j))

reached = {k: False for k in roads}
distance = {v: math.inf for v in roads}
distance[c] = 0
reached = {k: False for k in roads}
queue = deque([c])

while queue:
    u = queue.popleft()
    reached[u] = True
    i, j = u
    adj = []
    if i > 0 and S[i - 1, j] == '.':
        adj.append((i-1, j))
    if j > 0 and S[i, j - 1] == '.':
        adj.append((i, j - 1))
    if i < h-1 and S[i + 1, j] == '.':
        adj.append((i+1, j))
    if j < w-1 and S[i, j+1] == '.':
        adj.append((i, j+1))
    for v in adj:
        if not reached[v]:
            distance[v] = distance[u]
            queue.appendleft(v)

    warp = product(range(max(i-2, 0), min(i+3, h)),
                   range(max(j-2, 0), min(j+3, w)))
    for wa in warp:
        if wa not in adj and S[wa] == '.' and not reached[wa]:
            distance[wa] = distance[u] + 1
            queue.append(wa)

print(distance[d] if distance[d] != math.inf else 0)

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split())
ch,cw = map(int, input().split())
dh, dw = map(int, input().split())
f = []
for i in range(h):
    ff = []
    jj = input()
    for j in jj:
        ff.append(j) 
    f.append(ff)
ch -= 1
cw -= 1
dh -= 1
dw -= 1
dist = [[float("inf")] * w] * h
dy = [0, 1, 0, -1]
dx = [1, 0, -1, 0]

dist[ch][cw] = 0
from collections import deque
q = deque()
q.append((ch, cw))
while(len(q) != 0):
    y, x = q.popleft()
    d = dist[y][x]
    for dd in range(4):
        ny = y + dy[dd]
        nx = x + dx[dd]
        if (ny < 0 or h <= ny or nx < 0 or w <= nx):
            continue
        if (f[ny][nx] == "#"):
            continue
        if (dist[ny][nx] <= d):
            continue
        dist[ny][nx] = d
        q.appendleft((ny, nx))
    
    for yy in range(-2, 3):
        for xx in range(-2, 3):
            ny = y + yy
            nx = x + xx
            if (ny < 0 or h <= ny or nx < 0 or w <= nx):
                continue
            if (f[ny][nx] == "#"):
                continue
            if dist[ny][nx] <= d + 1:
                continue
            dist[ny][nx] = d + 1 
            q.append((ny, nx))
ans = dist[dh][dw]
if ans == float("inf"):
    ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

H,W=map(int,input().split())
si,sj=[int(x)-1 for x in input().split()]
ti,tj=[int(x)-1 for x in input().split()]
A=list(sys.stdin)

dirs=[-2,-1,0,1,2]
dist=[[10000]*W for _ in range(H)]
dist[si][sj]=0
dq=deque()
dq.append((0,si,sj))
ok=False
while dq:
    k,i,j=dq.popleft()
    if k>dist[i][j]:
        continue
    if i==ti and j==tj:
        ok=True
        break
    for di in dirs:
        for dj in dirs:
            if di==dj==0:
                continue
            ni,nj=i+di,j+dj
            if not (0<=ni<H and 0<=nj<W):
                continue
            if A[ni][nj]=='#':
                continue
            if abs(di)+abs(dj)==1:
                append=dq.appendleft
                nk=k
            else:
                append=dq.append
                nk=k+1
            if k>=dist[ni][nj]:
                continue
            dist[ni][nj]=nk
            append((nk,ni,nj))
      
if ok:
    ans=dist[ti][tj]
else:
    ans=-1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

H,W=map(int,input().split())
si,sj=[int(x)-1 for x in input().split()]
ti,tj=[int(x)-1 for x in input().split()]
A=list(sys.stdin)

dirs=[-2,-1,0,1,2]
dist=[[1000]*W for _ in range(H)]
dist[si][sj]=0
dq=deque()
dq.append((0,si,sj))
ok=False
while dq:
    k,i,j=dq.popleft()
    if k>dist[i][j]:
        continue
    if i==ti and j==tj:
        ok=True
        break
    for di in dirs:
        for dj in dirs:
            if di==dj==0:
                continue
            ni,nj=i+di,j+dj
            if not (0<=ni<H and 0<=nj<W):
                continue
            if A[ni][nj]=='#':
                continue
            if abs(di)+abs(dj)==1:
                append=dq.appendleft
                nk=k
            else:
                append=dq.append
                nk=k+1
            if k>=dist[ni][nj]:
                continue
            dist[ni][nj]=nk
            append((nk,ni,nj))
      
if ok:
    ans=dist[ti][tj]
else:
    ans=-1

print(ans)

Submission status: Wrong Answer

Solution code:
H,W = [int(a) for a in input().split()]
s = [int(a) for a in input().split()]
e = [int(a) for a in input().split()]
S = [None]*H
walk=[[] for _ in range(H*W)]
warp=[[] for _ in range(H*W)]
for i in range(H):
    S[i] = [1 if c=='.' else 0 for c in input()]
    for j in range(W):
        if S[i][j]==0:continue
        if i==0:
            if j==0:pass
            elif j==1:
                if S[i][j-1]:
                    walk[i*W+j].append(i*W+j-1)
                    walk[i*W+j-1].append(i*W+j)
            else:
                if S[i][j-1]:
                    walk[i*W+j].append(i*W+j-1)
                    walk[i*W+j-1].append(i*W+j)
                if S[i][j-2]:
                    warp[i*W+j].append(i*W+j-2)
                    warp[i*W+j-2].append(i*W+j)
        elif i==1:
            if j==0:
                if S[i-1][j]:
                    walk[i*W+j].append((i-1)*W+j)
                    walk[(i-1)*W+j].append(i*W+j)
            elif j==1:
                if S[i][j-1]:
                    walk[i*W+j].append(i*W+j-1)
                    walk[i*W+j-1].append(i*W+j)
                if S[i-1][j]:
                    walk[i*W+j].append((i-1)*W+j)
                    walk[(i-1)*W+j].append(i*W+j)
                if S[i-1][j-1]:
                    warp[i*W+j].append((i-1)*W+j-1)
                    warp[(i-1)*W+j-1].append(i*W+j)
            else:
                if S[i][j-1]:
                    walk[i*W+j].append(i*W+j-1)
                    walk[i*W+j-1].append(i*W+j)
                if S[i-1][j]:
                    walk[i*W+j].append((i-1)*W+j)
                    walk[(i-1)*W+j].append(i*W+j)
                if S[i-1][j-1]:
                    warp[i*W+j].append((i-1)*W+j-1)
                    warp[(i-1)*W+j-1].append(i*W+j)
                if S[i][j-2]:
                    warp[i*W+j].append(i*W+j-2)
                    warp[i*W+j-2].append(i*W+j)
                if S[i-1][j-2]:
                    warp[i*W+j].append((i-1)*W+j-2)
                    warp[(i-1)*W+j-2].append(i*W+j)
            if j<W-1:
                if S[i-1][j+1]:
                    warp[i*W+j].append((i-1)*W+j+1)
                    warp[(i-1)*W+j+1].append(i*W+j)
                if j<W-2:
                    if S[i-1][j+2]:
                        warp[i*W+j].append((i-1)*W+j+2)
                        warp[(i-1)*W+j+2].append(i*W+j)
        else:
            if j==0:
                if S[i-1][j]:
                    walk[i*W+j].append((i-1)*W+j)
                    walk[(i-1)*W+j].append(i*W+j)
                if S[i-2][j]:
                    warp[i*W+j].append((i-2)*W+j)
                    warp[(i-2)*W+j].append(i*W+j)
            elif j==1:
                if S[i][j-1]:
                    walk[i*W+j].append(i*W+j-1)
                    walk[i*W+j-1].append(i*W+j)
                if S[i-1][j]:
                    walk[i*W+j].append((i-1)*W+j)
                    walk[(i-1)*W+j].append(i*W+j)
                if S[i-1][j-1]:
                    warp[i*W+j].append((i-1)*W+j-1)
                    warp[(i-1)*W+j-1].append(i*W+j)
                if S[i-2][j]:
                    warp[i*W+j].append((i-2)*W+j)
                    warp[(i-2)*W+j].append(i*W+j)
                if S[i-2][j-1]:
                    warp[i*W+j].append((i-2)*W+j-1)
                    warp[(i-2)*W+j-1].append(i*W+j)
            else:
                if S[i][j-1]:
                    walk[i*W+j].append(i*W+j-1)
                    walk[i*W+j-1].append(i*W+j)
                if S[i-1][j]:
                    walk[i*W+j].append((i-1)*W+j)
                    walk[(i-1)*W+j].append(i*W+j)
                if S[i-1][j-1]:
                    warp[i*W+j].append((i-1)*W+j-1)
                    warp[(i-1)*W+j-1].append(i*W+j)
                if S[i][j-2]:
                    warp[i*W+j].append(i*W+j-2)
                    warp[i*W+j-2].append(i*W+j)
                if S[i-1][j-2]:
                    warp[i*W+j].append((i-1)*W+j-2)
                    warp[(i-1)*W+j-2].append(i*W+j)
                if S[i-2][j]:
                    warp[i*W+j].append((i-2)*W+j)
                    warp[(i-2)*W+j].append(i*W+j)
                if S[i-2][j-1]:
                    warp[i*W+j].append((i-2)*W+j-1)
                    warp[(i-2)*W+j-1].append(i*W+j)
                if S[i-2][j-2]:
                    warp[i*W+j].append((i-2)*W+j-2)
                    warp[(i-2)*W+j-2].append(i*W+j)
            if j<W-1:
                if S[i-1][j+1]:
                    warp[i*W+j].append((i-1)*W+j+1)
                    warp[(i-1)*W+j+1].append(i*W+j)
                if S[i-2][j+1]:
                    warp[i*W+j].append((i-2)*W+j+1)
                    warp[(i-2)*W+j+1].append(i*W+j)
                if j<W-2:
                    if S[i-1][j+2]:
                        warp[i*W+j].append((i-1)*W+j+2)
                        warp[(i-1)*W+j+2].append(i*W+j)
                    if S[i-2][j+2]:
                        warp[i*W+j].append((i-2)*W+j+2)
                        warp[(i-2)*W+j+2].append(i*W+j)
#print( H,W,s,e,S,sep='\n')
#print(walk,warp,sep='\n')
#print(walk)
#print(warp)
from collections import deque
done = [0]*(H*W)
waiting_walk = deque()
waiting_warp = deque()
start=(s[0]-1)*W+s[1]-1
end=(e[0]-1)*W+e[1]-1
done[start] = 1
#print(start)
cnt=0
for n in warp[start]:
    waiting_warp.append(n)
for n in walk[start]:
    waiting_walk.append(n)
    if n==end:
        waiting_walk=[]
        waiting_warp=[]
warping=0
if len(waiting_walk)==0:warping=1
found=0
while len(waiting_walk) or len(waiting_warp):
    if warping:
        #print("warping")
        for i in range(len(waiting_warp)):
            to=waiting_warp[i]
            #print(to)
            if to==end:
                found=1
                cnt+=1
                break
            if done[to]!=1:
                waiting_walk.append(to)
        if to==end:break
        warping=0
        cnt+=1
        waiting_warp.clear()
    else:
        cur_node = waiting_walk.popleft()
        if done[cur_node]!=1:
            done[cur_node]=1
            #print(cur_node)
            if cur_node==end:
                found=1
                #print("found")
                break
            for n in walk[cur_node]:
                if done[n]!=1:
                    waiting_walk.append(n)
            for n in warp[cur_node]:
                if done[n]!=1:
                    waiting_warp.append(n)
        if len(waiting_walk)==0:warping=1
if found:print(cnt)
else:print(-1)