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:
from collections import deque

H, W = [int(a) for a in input().split()]
Ch, Cw = [int(a)-1 for a in input().split()]
Dh, Dw = [int(a)-1 for a in input().split()]

S = []
dist = []

for _ in range(H):
    S.append([a for a in input()])
    dist.append([-1 for _ in range(len(S[-1]))])

dist[Ch][Cw] = 0
q = deque()
q.append((Ch, Cw))
cost = 0

def p_dist():
    print('---')
    for row in dist:
        print(row)

while q:
    jump_list = []
    while q:
        v = q.popleft()
        jump_list.append(v)
        # print('n', v)
        for i in [v[0]-1, v[0]+1]:
            j = v[1]
            if i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                dist[i][j] = cost
                q.append((i, j))
        for j in [v[1]-1, v[1]+1]:
            i = v[0]
            if i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                dist[i][j] = cost
                q.append((i, j))

    cost += 1
    for v in jump_list:
        # print('j', v)
        for i in range(v[0]-2, v[0]+3):
            for j in range(v[1]-2, v[1]+3):
                if ((i == v[0] + 1) or (i == v[0] -1)) and j == 0:
                    pass
                elif ((j == v[1] + 1) or (j == v[1] -1)) and i == 0:
                    pass
                elif i == v[0] and j == v[1]:
                    pass
                elif i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                    # print(i, j, dist[i][j])
                    dist[i][j] = cost
                    q.append((i, j))

# p_dist()
print(dist[Dh][Dw])

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())
S = [" "]
for _ in range(H):
    S.append(" " + input())
INF = 100000
warp_min = [[INF] * (W + 1) for _ in range(H + 1)]
warp_min[Ch][Cw] = 0
d = deque()
d.append((Ch, Cw))
dx = (0, 1, 0, -1)
dy = (1, 0, -1, 0)
ndx_0 = (0, -1, -2, -1, -2)
ndy_0 = (1, 0, 0, 1, 1)
ndx_1 = (1, 0, 0, 1, 1)
ndy_1 = (1, 2, 1, 2, 0)
ndx_2 = (1, 0, 2, 1, 2)
ndy_2 = (0, -1, 0, -1, -1)
ndx_3 = (0, 0, -1, -1, -1)
ndy_3 = (-2, -1, 0, -1, -2)
dic = {}
dic[(Ch, Cw)] = 1

#while warp_min[Dh][Dw] == INF:
while d:
    wall_pos = []
    pos = d.popleft()
    #next_pos = []
    for i in range(4):
        next_y = pos[0] + dy[i]
        next_x = pos[1] + dx[i]
        if next_x <= 0 or next_x > W or next_y <= 0 or next_y > H:
            continue
        if warp_min[next_y][next_x] != INF and warp_min[pos[0]][pos[1]] >= warp_min[next_y][next_x]:
            dic[(next_y, next_x)] = 1
            continue
        if S[next_y][next_x] == ".":
            warp_min[next_y][next_x] = min(warp_min[pos[0]][pos[1]], warp_min[next_y][next_x])
            if (next_y, next_x) not in dic.keys():
                d.append((next_y, next_x))
                dic[(next_y, next_x)] = 1
        else:
            for j in range(5):
                if i == 0:
                    warp_to_y = next_y + ndy_0[j]
                    warp_to_x = next_x + ndx_0[j]
                elif i == 1:
                    warp_to_y = next_y + ndy_1[j]
                    warp_to_x = next_x + ndx_1[j]
                elif i == 2:
                    warp_to_y = next_y + ndy_2[j]
                    warp_to_x = next_x + ndx_2[j]
                elif i == 3:
                    warp_to_y = next_y + ndy_3[j]
                    warp_to_x = next_x + ndx_3[j]
                if warp_to_x <= 0 or warp_to_x > W or warp_to_y <= 0 or warp_to_y > H:
                    continue
                if warp_min[warp_to_y][warp_to_x] != INF and warp_min[pos[0]][pos[1]] + 1 >= warp_min[warp_to_y][warp_to_x]:
                    dic[(warp_to_y, warp_to_x)] = 1
                    continue
                if S[warp_to_y][warp_to_x] == ".":
                    warp_min[warp_to_y][warp_to_x] = min(warp_min[pos[0]][pos[1]] + 1, warp_min[warp_to_y][warp_to_x])
                    d.append((warp_to_y, warp_to_x))

#for i in warp_min:
#    print(i)
if warp_min[Dh][Dw] == INF:
    warp_min[Dh][Dw] = -1
print(warp_min[Dh][Dw])

Submission status: Wrong Answer

Solution code:
from collections import deque

H, W = [int(a) for a in input().split()]
Ch, Cw = [int(a)-1 for a in input().split()]
Dh, Dw = [int(a)-1 for a in input().split()]

S = []
dist = []

for _ in range(H):
    S.append([a for a in input()])
    dist.append([-1 for _ in range(len(S[-1]))])

dist[Ch][Cw] = 0
q = deque()
q.append((Ch, Cw))
cost = 0

def p_dist():
    print('---')
    for row in dist:
        print(row)

while q:
    jump_list = []
    while q:
        v = q.popleft()
        jump_list.append(v)
        # print('n', v)
        for i in [v[0]-1, v[0]+1]:
            j = v[1]
            if i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                dist[i][j] = cost
                q.append((i, j))
        for j in [v[1]-1, v[1]+1]:
            i = v[0]
            if i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                dist[i][j] = cost
                q.append((i, j))

    cost += 1
    q = deque()
    for v in jump_list:
        # print('j', v)
        for i in range(v[0]-2, v[0]+3):
            for j in range(v[1]-2, v[1]+3):
                if ((i == v[0] + 1) or (i == v[0] -1)) and j == 0:
                    pass
                elif ((j == v[1] + 1) or (j == v[1] -1)) and i == 0:
                    pass
                elif i == v[0] and j == v[1]:
                    pass
                elif i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                    # print(i, j, dist[i][j])
                    dist[i][j] = cost
                    q.append((i, j))

# p_dist()
print(dist[Dh][Dw])

Submission status: Wrong Answer

Solution code:
import heapq

def dijkstra(s):
    hq = [(0, s)]
    heapq.heapify(hq) # リストを優先度付きキューに変換
    cost = [float('inf')] * (h*w) # 行ったことのないところはinf
    cost[s] = 0 # 開始地点は0
    while hq:
        c, v = heapq.heappop(hq)
        if c > cost[v]: # コストが現在のコストよりも高ければスルー
            continue
        for d, u in e[v]:
            tmp = d + cost[v]
            if tmp < cost[u]:
                cost[u] = tmp
                heapq.heappush(hq, (tmp, u))
    return cost

h,w = map(int,input().split())
c = list(map(int,input().split()))
d = list(map(int,input().split()))
maze = [list(input()) for i in range(h)]
# eのインデックスはi*w+j
e = [[] for _ in range(h*w)]
for i in range(h):
    for j in range(w):
        if j<w-1 and maze[i][j] == "." and maze[i][j+1] == ".":
            e[i*w+j].append((0, i*w+j+1))
            e[i*w+j+1].append((0, i*w+j)) # 有向グラフでは削除
        if i<h-1 and maze[i][j] == "." and maze[i+1][j] == ".":
            e[i*w+j].append((0, (i+1)*w+j))
            e[(i+1)*w+j].append((0, i*w+j)) # 有向グラフでは削除
        for x in range(3):
            for y in range(3):
                if i+x<h and j+y<w and maze[i+x][j+y] == ".":
                    e[i*w+j].append((1, (i+x)*w+j+y))
                    e[(i+x)*w+j+y].append((1, i*w+j))

# print(dijkstra(1))
ans = dijkstra((c[0]-1)*w+(c[1]-1))[(d[0]-1)*w+(d[1]-1)]
if ans == float("inf"):
    print(-1)
    exit()
print(ans)

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())
Ch -=1
Cw -= 1
Dh -= 1
Dw -= 1
t = [[i for i in range(w)] for j in range(h)]
visited = [[0 for i in range(w)] for j in range(h)]  # いったところFF
ans = 0
for i in range(h):
  temp = input()
  for j in range(w):
    t[i][j] = temp[j]
d = deque([(Ch, Cw)])
ans = 0
a = [(1, 0), (-1, 0), (0, 1), (0, -1)]
while True:
  # 移動A
  stemp = set()
  while len(d) != 0:
    Th, Tw = d.popleft()
    # s.add((Th, Tw))
    stemp.add((Th, Tw))
    if (Th, Tw) == (Dh, Dw):
      print(ans)
      exit()
    for i, j in a:
      _h, _w = Th + i, Tw + j
      _p = (_h, _w)
    if 0 <= _h < h and 0 <= _w < w and t[_h][_w] == '.' and visited[_h][_w] == 0:
      d.append(_p)
      visited[_h][_w] = 1
    # 移動B
    ans += 1
    for Th, Tw in stemp:
      for i in range(-2, 3):
        for j in range(-2, 3):
          _h, _w = Th + i, Tw + j
          _p = (_h, _w)
          if 0 <= _h < h and 0 <= _w < w and t[_h][_w] == '.' and visited[_h][_w] == 0:
            d.append(_p)
            visited[Th][Tw] = 1
    if len(d) == 0:
      print('-1')
      exit()

Submission status: Wrong Answer

Solution code:
from collections import deque
H, W = [int(a) for a in input().split()]
Ch, Cw = [int(a)-1 for a in input().split()]
Dh, Dw = [int(a)-1 for a in input().split()]

S = []
dist = []

for _ in range(H):
    S.append([a for a in input()])
    dist.append([-1 for _ in range(len(S[-1]))])

dist[Ch][Cw] = 0
q = deque()
q.append((Ch, Cw))
cost = 0

def p_dist():
    print('---')
    for row in dist:
        print(row)

while q:
    jump_list = []
    while q:
        v = q.popleft()
        jump_list.append(v)
        # print('n', v)
        for i in [v[0]-1, v[0]+1]:
            j = v[1]
            if i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                dist[i][j] = cost
                q.append((i, j))
        for j in [v[1]-1, v[1]+1]:
            i = v[0]
            if i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                dist[i][j] = cost
                q.append((i, j))

    cost += 1
    q = deque(jump_list)
    jump_list = []
    while q:
        v = q.popleft()
        # print('j', v)
        for i in range(v[0]-2, v[0]+3):
            for j in range(v[1]-2, v[1]+3):
                if ((i == v[0] + 1) or (i == v[0] -1)) and j == 0:
                    pass
                elif ((j == v[1] + 1) or (j == v[1] -1)) and i == 0:
                    pass
                elif i == v[0] and j == v[1]:
                    pass
                elif i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                    # print(i, j, dist[i][j])
                    dist[i][j] = cost
                    jump_list.append((i, j))

    q = deque(jump_list)

# p_dist()
print(dist[Dh][Dw])

Submission status: Wrong Answer

Solution code:
from collections import deque
H, W = [int(a) for a in input().split()]
Ch, Cw = [int(a)-1 for a in input().split()]
Dh, Dw = [int(a)-1 for a in input().split()]

S = []
dist = []

for _ in range(H):
    S.append([a for a in input()])
    dist.append([-1 for _ in range(len(S[-1]))])

dist[Ch][Cw] = 0
q = deque()
q.append((Ch, Cw))
cost = 0

def p_dist():
    print('---')
    for row in dist:
        print(row)

while q:
    jump_list = []
    while q:
        v = q.popleft()
        jump_list.append(v)
        # print('n', v)
        for i in [v[0]-1, v[0]+1]:
            j = v[1]
            if i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                dist[i][j] = cost
                q.append((i, j))
        for j in [v[1]-1, v[1]+1]:
            i = v[0]
            if i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                dist[i][j] = cost
                q.append((i, j))

    cost += 1
    q = deque(jump_list)
    jump_list = []
    while q:
        v = q.popleft()
        # print('j', v)
        for i in range(v[0]-2, v[0]+3):
            for j in range(v[1]-2, v[1]+3):
                if ((i == v[0] + 1) or (i == v[0] -1)) and j == 0:
                    pass
                elif ((j == v[1] + 1) or (j == v[1] -1)) and i == 0:
                    pass
                elif i >= 0 and j >= 0 and i < H and j < W and (i != v[0] or j != v[1]) and S[i][j]=='.' and dist[i][j] == -1:
                    # print(i, j, dist[i][j])
                    dist[i][j] = cost
                    jump_list.append((i, j))

    q = deque(jump_list)

# p_dist()
print(dist[Dh][Dw])

Submission status: Wrong Answer

Solution code:
from collections import deque
H, W = [int(a) for a in input().split()]
Ch, Cw = [int(a)-1 for a in input().split()]
Dh, Dw = [int(a)-1 for a in input().split()]

S = []
dist = []

for _ in range(H):
    S.append([a for a in input()])
    dist.append([-1 for _ in range(len(S[-1]))])

dist[Ch][Cw] = 0
q = deque()
q.append((Ch, Cw))
cost = 0

def p_dist():
    print('---')
    for row in dist:
        print(row)

while q:
    jump_list = []
    while q:
        v = q.popleft()
        jump_list.append(v)
        # print('n', v)
        for i in [v[0]-1, v[0]+1]:
            j = v[1]
            if i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                dist[i][j] = cost
                q.append((i, j))
        for j in [v[1]-1, v[1]+1]:
            i = v[0]
            if i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                dist[i][j] = cost
                q.append((i, j))

    cost += 1
    q = deque(jump_list)
    jump_list = []
    while q:
        v = q.popleft()
        # print('j', v)
        for i in range(v[0]-2, v[0]+3):
            for j in range(v[1]-2, v[1]+3):
                if ((i == v[0] + 1) or (i == v[0] -1)) and j == 0:
                    continue
                elif ((j == v[1] + 1) or (j == v[1] -1)) and i == 0:
                    continue
                elif i >= 0 and j >= 0 and i < H and j < W and (i != v[0] or j != v[1]) and S[i][j]=='.' and dist[i][j] == -1:
                    # print(i, j, dist[i][j])
                    dist[i][j] = cost
                    jump_list.append((i, j))

    q = deque(jump_list)

# p_dist()
print(dist[Dh][Dw])

Submission status: Wrong Answer

Solution code:
import sys, math
import io, os
#data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
#from bisect import bisect_left as bl, bisect_right as br, insort
#from heapq import heapify, heappush, heappop
#from collections import defaultdict as dd, deque, Counter
#from itertools import permutations,combinations
def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var) : sys.stdout.write(' '.join(map(str, var))+'\n')
def out(var) : sys.stdout.write(str(var)+'\n')
from decimal import Decimal
#from fractions import Fraction
#sys.setrecursionlimit(100000)
INF = float('inf')
mod = int(1e9)+7


h,w=mdata()
ch,cw=mdata()
dh,dw=mdata()
ch-=1
dh-=1
cw-=1
dw-=1
if ch==dh and cw==dw:
    out(0)
    exit()
S=[data() for i in range(h)]
steps=[(0,1),(1,0),(-1,0),(0,-1)]
mat=[[INF]*w for i in range(h)]
mat[ch][cw]=0
vis=[[0]*w for i in range(h)]
vis[ch][cw]=1
stack=[(ch,cw)]
while stack:
    stack1=[]
    while stack:
        a, b = stack.pop()
        for i in range(-2,3):
            for j in range(-2,3):
                if 0 <= a + i < h and 0 <= b + j < w:
                    if S[a + i][b + j] == '.':
                        mat[a+i][b+j]=min(mat[a+i][b+j],mat[a][b]+1)
                        if (i,j) in steps :
                            mat[a + i][b + j] = min(mat[a + i][b + j], mat[a][b])
                    if (i,j) in steps and vis[a+i][b+j]==0:
                        stack1.append((a+i,b+j))
                        vis[a+i][b+j]=1
    stack=stack1
if mat[dh][dw]!=INF:
    out(mat[dh][dw])
else:
    out(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque
H, W = [int(a) for a in input().split()]
Ch, Cw = [int(a)-1 for a in input().split()]
Dh, Dw = [int(a)-1 for a in input().split()]

S = []
dist = []

for _ in range(H):
    S.append([a for a in input()])
    dist.append([-1 for _ in range(len(S[-1]))])

dist[Ch][Cw] = 0
q = deque()
q.append((Ch, Cw))
cost = 0

def p_dist():
    print('---')
    for row in dist:
        print(row)

while q:
    jump_list = []
    while q:
        v = q.popleft()
        if cost == 0:
            jump_list.append(v)
        # print('n', v)
        for i in [v[0]-1, v[0]+1]:
            j = v[1]
            if i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                dist[i][j] = cost
                q.append((i, j))
                jump_list.append((i,j))
        for j in [v[1]-1, v[1]+1]:
            i = v[0]
            if i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                dist[i][j] = cost
                q.append((i, j))
                jump_list.append((i,j))

    cost += 1
    q = deque(list(jump_list))
    jump_list = []
    while q:
        v = q.popleft()
        # print('j', v)
        for i in range(v[0]-2, v[0]+3):
            for j in range(v[1]-2, v[1]+3):
                if ((i == v[0] + 1) or (i == v[0] -1)) and j == 0:
                    continue
                elif ((j == v[1] + 1) or (j == v[1] -1)) and i == 0:
                    continue
                # print(i, j, S[i][j])
                if i >= 0 and j >= 0 and i < H and j < W and (i != v[0] or j != v[1]) and S[i][j]=='.' and dist[i][j] == -1:
                    # print(i, j, dist[i][j])
                    dist[i][j] = cost
                    jump_list.append((i,j))

    q = deque(jump_list)

# p_dist()
print(dist[Dh][Dw])

Submission status: Wrong Answer

Solution code:
from collections import deque
H, W = [int(a) for a in input().split()]
Ch, Cw = [int(a)-1 for a in input().split()]
Dh, Dw = [int(a)-1 for a in input().split()]

S = []
dist = []

for _ in range(H):
    S.append([a for a in input()])
    dist.append([-1 for _ in range(len(S[-1]))])

dist[Ch][Cw] = 0
q = deque()
q.append((Ch, Cw))
cost = 0

def p_dist():
    print('---')
    for row in dist:
        print(row)

while q:
    jump_list = []
    while q:
        v = q.popleft()
        jump_list.append(v)
        # print('n', v)
        for i in [v[0]-1, v[0]+1]:
            j = v[1]
            if i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                dist[i][j] = cost
                q.append((i, j))
        for j in [v[1]-1, v[1]+1]:
            i = v[0]
            if i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                dist[i][j] = cost
                q.append((i, j))

    cost += 1
    q = deque(list(jump_list))
    jump_list = []
    while q:
        v = q.popleft()
        # print('j', v)
        for i in range(v[0]-2, v[0]+3):
            for j in range(v[1]-2, v[1]+3):
                if ((i == v[0] + 1) or (i == v[0] -1)) and j == 0:
                    continue
                elif ((j == v[1] + 1) or (j == v[1] -1)) and i == 0:
                    continue
                # print(i, j, S[i][j])
                if i >= 0 and j >= 0 and i < H and j < W and (i != v[0] or j != v[1]) and S[i][j]=='.' and dist[i][j] == -1:
                    # print(i, j, dist[i][j])
                    dist[i][j] = cost
                    jump_list.append((i,j))

    q = deque(jump_list)

# p_dist()
print(dist[Dh][Dw])

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())
S = [" "]
for _ in range(H):
    S.append(" " + input())
INF = 100000
warp_min = [[INF] * (W + 1) for _ in range(H + 1)]
warp_min[Ch][Cw] = 0
d = deque()
d.append((Ch, Cw))
dx = (0, 1, 0, -1)
dy = (1, 0, -1, 0)
ndx_0 = (0, -1, -2, -1, -2)
ndy_0 = (1, 0, 0, 1, 1)
ndx_1 = (1, 0, 0, 1, 1)
ndy_1 = (1, 2, 1, 2, 0)
ndx_2 = (1, 0, 2, 1, 2)
ndy_2 = (0, -1, 0, -1, -1)
ndx_3 = (0, 0, -1, -1, -1)
ndy_3 = (-2, -1, 0, -1, -2)

cnt = 0

#while warp_min[Dh][Dw] == INF:
while d:
    wall_pos = []
    pos = d.popleft()
    #next_pos = []
    for i in range(4):
        next_y = pos[0] + dy[i]
        next_x = pos[1] + dx[i]
        if next_x <= 0 or next_x > W or next_y <= 0 or next_y > H:
            continue
        if warp_min[next_y][next_x] != INF or warp_min[pos[0]][pos[1]] >= warp_min[next_y][next_x]:
            continue
        if S[next_y][next_x] == ".":
            warp_min[next_y][next_x] = min(warp_min[pos[0]][pos[1]], warp_min[next_y][next_x])
            d.append((next_y, next_x))
        else:
            for j in range(5):
                if i == 0:
                    warp_to_y = next_y + ndy_0[j]
                    warp_to_x = next_x + ndx_0[j]
                elif i == 1:
                    warp_to_y = next_y + ndy_1[j]
                    warp_to_x = next_x + ndx_1[j]
                elif i == 2:
                    warp_to_y = next_y + ndy_2[j]
                    warp_to_x = next_x + ndx_2[j]
                elif i == 3:
                    warp_to_y = next_y + ndy_3[j]
                    warp_to_x = next_x + ndx_3[j]
                if warp_to_x <= 0 or warp_to_x > W or warp_to_y <= 0 or warp_to_y > H:
                    continue
                if warp_min[warp_to_y][warp_to_x] != INF:
                    continue
                if S[warp_to_y][warp_to_x] == ".":
                    warp_min[warp_to_y][warp_to_x] = min(warp_min[pos[0]][pos[1]] + 1, warp_min[warp_to_y][warp_to_x])
                    d.append((warp_to_y, warp_to_x))





#for i in warp_min:
#    print(i)
if warp_min[Dh][Dw] == INF:
    warp_min[Dh][Dw] = -1
print(warp_min[Dh][Dw])

Submission status: Wrong Answer

Solution code:
from collections import deque
#import sys
#sys.setrecursionlimit(6000)

def bfs01(start_h, start_w, S, H, W):
    path = deque()
    path.append((start_h, start_w, 0))  #x, y, cost
    cost0 = set()
    cost1 = set()
    scores = [[10**18]*(W+1)]*(H+1)
    scores[start_h][start_w] = 0
    #BFS
    while path:
        x, y, cost = path.pop()
        if (x,y) in cost0:
            continue
        elif (x,y) in cost1 and cost==1:
            continue
        #print((x, y, cost))
        if cost==0:
            cost0.add((x,y))
        elif cost==1:
            cost1.add((x,y))
        else:
            assert False
        for xx, yy in [(x,y+1), (x, y-1), (x+1, y), (x-1, y)]:
            if xx<0 or yy<0 or xx>H or yy>W:
                continue
            if scores[xx][yy] <= scores[x][y]:
                continue
            #print(f"xx, yy={xx},{yy}")
            if S[xx][yy]:
                path.append((xx, yy,0))
                scores[xx][yy] = scores[x][y]
        for xx, yy in [(x-2,y+2),(x-1,y+2),(x,y+2),(x+1,y+2),(x+2,y+2),(x-2,y+1),(x-1,y+1),(x+1,y+1),(x+2,y+1),(x-2,y),(x+2,y),
            (x-2,y-1),(x-1,y-1),(x+1,y-1),(x+2,y-1),(x-2,y-2),(x-1,y-2),(x,y-2),(x+1,y-2),(x+2,y-2),]:
            if xx<0 or yy<0 or xx>H or yy>W:
                continue
            if scores[xx][yy] <= scores[x][y] + 1:
                continue
            #print(f"xx, yy={xx},{yy}")
            if S[xx][yy]:
                path.appendleft((xx, yy,1))
                scores[xx][yy] = scores[x][y] + 1
    return scores

def main():
    H, W = map(int, input().split())
    Ch, Cw = map(int, input().split())
    Dh, Dw = map(int, input().split())
    S = [[False]*(W+1)]  #S[h][w]でアクセス
    for _ in range(H):
        S.append([False] + [True if x=='.' else False for x in input()])
    scores = bfs01(Ch, Cw, S, H, W)
    if scores[Dh][Dw] == 10**18:
        print(-1)
    else:
        print(scores[Dh][Dw])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
H, W = [int(a) for a in input().split()]
Ch, Cw = [int(a)-1 for a in input().split()]
Dh, Dw = [int(a)-1 for a in input().split()]

S = []
dist = []

for _ in range(H):
    S.append([a for a in input()])
    dist.append([-1 for _ in range(len(S[-1]))])

dist[Ch][Cw] = 0
q = deque()
q.append((Ch, Cw))
cost = 0

def p_dist():
    print('---')
    for row in dist:
        print(row)

while q:
    while q:
        v = q.popleft()
        for i in [v[0]-1, v[0]+1]:
            j = v[1]
            if i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                dist[i][j] = cost
                q.append((i, j))
        for j in [v[1]-1, v[1]+1]:
            i = v[0]
            if i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                dist[i][j] = cost
                q.append((i, j))

    jump_list = []
    for i in range(H):
        for j in range(W):
            if dist[i][j] == cost:
                jump_list.append((i, j))

    cost += 1
    q = deque(list(jump_list))
    while q:
        v = q.popleft()
        for i in range(v[0]-2, v[0]+3):
            for j in range(v[1]-2, v[1]+3):
                if ((i == v[0] + 1) or (i == v[0] -1)) and j == 0:
                    continue
                elif ((j == v[1] + 1) or (j == v[1] -1)) and i == 0:
                    continue
                if i >= 0 and j >= 0 and i < H and j < W and i != v[0] and j != v[1] and S[i][j]=='.' and dist[i][j] == -1:
                    dist[i][j] = cost
    
    jump_list = []
    for i in range(H):
        for j in range(W):
            if dist[i][j] == cost:
                jump_list.append((i, j))
    q = deque(jump_list)

# p_dist()
print(dist[Dh][Dw])

Submission status: Wrong Answer

Solution code:
from collections import deque
#import sys
#sys.setrecursionlimit(6000)

def bfs01(start_h, start_w, S, H, W):
    path = deque()
    path.append((start_h, start_w, 0))  #x, y, cost
    cost0 = set()
    cost1 = set()
    scores = [[10**18]*(H+1)]*(W+1)
    scores[start_h][start_w] = 0
    #BFS
    while path:
        x, y, cost = path.pop()
        if (x,y) in cost0:
            continue
        elif (x,y) in cost1 and cost==1:
            continue
        #print((x, y, cost))
        if cost==0:
            cost0.add((x,y))
        elif cost==1:
            cost1.add((x,y))
        else:
            assert False
        for xx, yy in [(x,y+1), (x, y-1), (x+1, y), (x-1, y)]:
            if xx<0 or yy<0 or xx>H or yy>W:
                continue
            if scores[xx][yy] <= scores[x][y]:
                continue
            #print(f"xx, yy={xx},{yy}")
            if S[xx][yy]:
                path.append((xx, yy,0))
                scores[xx][yy] = scores[x][y]
        for xx, yy in [(x-2,y+2),(x-1,y+2),(x,y+2),(x+1,y+2),(x+2,y+2),(x-2,y+1),(x-1,y+1),(x+1,y+1),(x+2,y+1),(x-2,y),(x+2,y),
            (x-2,y-1),(x-1,y-1),(x+1,y-1),(x+2,y-1),(x-2,y-2),(x-1,y-2),(x,y-2),(x+1,y-2),(x+2,y-2),]:
            if xx<0 or yy<0 or xx>H or yy>W:
                continue
            if scores[xx][yy] <= scores[x][y] + 1:
                continue
            #print(f"xx, yy={xx},{yy}")
            if S[xx][yy]:
                path.appendleft((xx, yy,1))
                scores[xx][yy] = scores[x][y] + 1
    return scores

def main():
    H, W = map(int, input().split())
    Ch, Cw = map(int, input().split())
    Dh, Dw = map(int, input().split())
    S = [[False]*(W+1)]  #S[h][w]でアクセス
    for _ in range(H):
        S.append([False] + [True if x=='.' else False for x in input()])
    exit()
    scores = bfs01(Ch, Cw, S, H, W)
    if scores[Dw][Dh] == 10**18:
        print(-1)
    else:
        print(scores[Dw][Dh])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
#import sys
#sys.setrecursionlimit(6000)

def bfs01(start_h, start_w, S, H, W):
    path = deque()
    path.append((start_h, start_w, 0))  #x, y, cost
    cost0 = set()
    cost1 = set()
    scores = [[10**18]*(H+1)]*(W+1)
    scores[start_h][start_w] = 0
    #BFS
    while path:
        x, y, cost = path.pop()
        if (x,y) in cost0:
            continue
        elif (x,y) in cost1 and cost==1:
            continue
        #print((x, y, cost))
        if cost==0:
            cost0.add((x,y))
        elif cost==1:
            cost1.add((x,y))
        else:
            assert False
        for xx, yy in [(x,y+1), (x, y-1), (x+1, y), (x-1, y)]:
            if xx<0 or yy<0 or xx>H or yy>W:
                continue
            if scores[xx][yy] <= scores[x][y]:
                continue
            #print(f"xx, yy={xx},{yy}")
            if S[xx][yy]:
                path.append((xx, yy,0))
                scores[xx][yy] = scores[x][y]
            for xx, yy in [(x-2,y+2),(x-1,y+2),(x,y+2),(x+1,y+2),(x+2,y+2),(x-2,y+1),(x-1,y+1),(x+1,y+1),(x+2,y+1),(x-2,y),(x+2,y),
                (x-2,y-1),(x-1,y-1),(x+1,y-1),(x+2,y-1),(x-2,y-2),(x-1,y-2),(x,y-2),(x+1,y-2),(x+2,y-2),]:
                continue
            if scores[xx][yy] <= scores[x][y] + 1:
                continue
            #print(f"xx, yy={xx},{yy}")
            if S[xx][yy]:
                path.appendleft((xx, yy,1))
                scores[xx][yy] = scores[x][y] + 1
    return scores

def main():
    H, W = map(int, input().split())
    Ch, Cw = map(int, input().split())
    Dh, Dw = map(int, input().split())
    S = [[False]*(W+1)]  #S[h][w]でアクセス
    for _ in range(H):
        S.append([False] + [True if x=='.' else False for x in input()])
    exit()
    scores = bfs01(Ch, Cw, S, H, W)
    if scores[Dw][Dh] == 10**18:
        print(-1)
    else:
        print(scores[Dw][Dh])




if __name__ == "__main__":
    main()