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:
import sys, os, math, bisect, itertools, collections, heapq, queue, copy, array

# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
# from decimal import Decimal
# from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
INF = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    H, W = il()
    Ch, Cw = il()
    Dh, Dw = il()
    grid = [iss() for _ in range(H)]
    visited = [[INF] * W for _ in range(H)]
    move = [[-1, 0], [1, 0], [0, -1], [0, 1]]
    que = collections.deque()

    que.appendleft((Ch - 1, Cw - 1, 0))
    visited[Ch - 1][Cw - 1] = 0
    while que:
        while que:
            h, w, c = que.popleft()
            if visited[h][w] == INF:
                visited[h][w] = c
            for mh, mw in move: # 移動A: 歩いて移動
                ch, cw = h + mh, w + mw
                if 0 <= ch < H and 0 <= cw < W and grid[ch][cw] != '#' and visited[ch][cw] == INF:
                    visited[ch][cw] = c
                    que.appendleft((ch, cw, c))
        for wh in range(-2, 3): # 移動B: ワープ魔法
            for ww in range(-2, 3):
                ch, cw = h + wh, w + ww
                if 0 <= ch < H and 0 <= cw < W and grid[ch][cw] != '#' and visited[ch][cw] == INF:
                    que.append((ch, cw, c+1))

    ret = visited[Dh - 1][Dw - 1]
    print(-1 if ret == INF else ret)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque
from itertools import product
import heapq


def read(INF=10**7):
    H, W = map(int, input().strip().split())
    CH, CW = map(int, input().strip().split())
    DH, DW = map(int, input().strip().split())
    S = [[0 for j in range(W)] for i in range(H)]
    for i in range(H):
        s = list(input().strip())
        for j in range(W):
            if s[j] == "#":
                S[i][j] = INF
            else:
                S[i][j] = -1
    return H, W, CH, CW, DH, DW, S


def solve(H, W, CH, CW, DH, DW, S, INF=10**7):
    DIR0 = [(0, 1), (1, 0), (0, -1), (-1, 0)]
    DIR1 = [(y, x) for y, x in product(range(-2, 3), range(-2, 3))]
    S[CH-1][CW-1] = 0
    q = deque()
    q.append((CH-1, CW-1))
    while q:
        y0, x0 = q.popleft()
        d0 = S[y0][x0]
        # 距離0で到達可能な場所をBFS
        T = []
        r = deque()
        T.append((y0, x0))
        r.append((y0, x0))
        while r:
            y, x = r.popleft()
            for dy, dx in DIR0:
                ny, nx = y + dy, x + dx
                if 0 <= ny < H and 0 <= nx < W and S[ny][nx] == -1:
                    r.append((ny, nx))
                    T.append((ny, nx))
                    S[ny][nx] = d0
        # 距離1で到達可能な場所をBFS
        for y, x in T:
            for dy, dx in DIR1:
                ny, nx = y + dy, x + dx
                if 0 <= ny < H and 0 <= nx < W and S[ny][nx] == -1:
                    q.append((ny, nx))
                    S[ny][nx] = d0+1
    return S[DH-1][DW-1]


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Submission status: Wrong Answer

Solution code:
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


S = ['#'*(W+4)]
S.append('#'*(W+4))
for k in range(H):
  S.append('##' + input() + '##')
S.append('#'*(W+4))
S.append('#'*(W+4))

foo = [[-1 for _ in range(W+4)] for _ in range(H+4)]
for k in range(2, H+2):
  for j in range(2, W+2):
    if S[k][j] == '.':
      foo[k][j] = 10**6

foo[Ch][Cw] = 0

now = []
new = [(Ch, Cw)]
while len(new) > 0:
  item = new.pop()
  now.append(item)
  x = item[0]
  y = item[1]
  if foo[x+1][y] == 10**6:
    foo[x+1][y] = 0
    new.append((x+1, y))
  if foo[x][y+1] == 10**6:
    foo[x][y+1] = 0
    new.append((x, y+1))
  if foo[x-1][y] == 10**6:
    foo[x-1][y] = 0
    new.append((x-1, y))
  if foo[x][y-1] == 10**6:
    foo[x][y-1] = 0
    new.append((x, y-1))

ans = 0
while foo[Dh][Dw] == 10**6:
  if len(now) == 0:
    print(-1)
    exit() 
  ans += 1
  while len(now) > 0:
    item = now.pop()
    x = item[0]
    y = item[1]
    if foo[x+2][y+2] > ans:
      foo[x+2][y+2] = ans
      new.append((x+2, y+2))
    if foo[x+2][y+1] > ans:
      foo[x+2][y+1] = ans
      new.append((x+2, y+1))
    if foo[x+2][y] > ans:
      foo[x+2][y] = ans
      new.append((x+2, y))
    if foo[x+2][y-1] > ans:
      foo[x+2][y-1] = ans
      new.append((x+2, y-1))
    if foo[x+2][y-2] > ans:
      foo[x+2][y-2] = ans
      new.append((x+2, y-2))
    if foo[x+1][y+2] > ans:
      foo[x+1][y+2] = ans
      new.append((x+1, y+2))
    if foo[x+1][y-2] > ans:
      foo[x+1][y-2] = ans
      new.append((x+1, y-2))
    if foo[x][y+2] > ans:
      foo[x][y+2] = ans
      new.append((x, y+2))
    if foo[x][y-2] > ans:
      foo[x][y-2] = ans
      new.append((x, y-2))
    if foo[x-2][y+2] > ans:
      foo[x-2][y+2] = ans
      new.append((x-2, y+2))
    if foo[x-2][y+1] > ans:
      foo[x-2][y+1] = ans
      new.append((x-2, y+1))
    if foo[x-2][y] > ans:
      foo[x-2][y] = ans
      new.append((x-2, y))
    if foo[x-2][y-1] > ans:
      foo[x-2][y-1] = ans
      new.append((x-2, y-1))
    if foo[x-2][y-2] > ans:
      foo[x-2][y-2] = ans
      new.append((x-2, y-2))
    if foo[x-1][y+2] > ans:
      foo[x-1][y+2] = ans
      new.append((x-1, y+2))
    if foo[x-1][y-2] > ans:
      foo[x-1][y-2] = ans
      new.append((x-1, y-2))
    if foo[x-1][y-1] > ans:
      foo[x-1][y-1] = ans
      new.append((x-1, y-1))
    if foo[x-1][y+1] > ans:
      foo[x-1][y+1] = ans
      new.append((x-1, y+1))
    if foo[x+1][y+1] > ans:
      foo[x+1][y+1] = ans
      new.append((x+1, y+1))
    if foo[x+1][y-1] > ans:
      foo[x+1][y-1] = ans
      new.append((x+1, y-1))
      
  while len(new) > 0:
    item = new.pop()
    now.append(item)
    x = item[0]
    y = item[1]
    if foo[x+1][y] == 10**7:
      foo[x+1][y] = 0
      new.append((x+1, y))
    if foo[x][y+1] == 10**7:
      foo[x][y+1] = 0
      new.append((x, y+1))
    if foo[x-1][y] == 10**7:
      foo[x-1][y] = 0
      new.append((x-1, y))
    if foo[x][y-1] == 10**7:
      foo[x][y-1] = 0
      new.append((x, y-1))
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq

h, w = list(map(int, input().split()))
ch, cw = list(map(int, input().split()))
dh, dw = list(map(int, input().split()))
s = [input() for _ in range(h)]

def check_range(x, y):
    return (0 <= x < h) and (0 <= y < w)

heap = [(0, ch - 1, cw - 1)]
inf = 1000000000
dist = [[inf for i in range(w)] for _ in range(h)]

while len(heap):
    if dist[dh - 1][dw - 1] != inf:
        break

    d, x, y = heapq.heappop(heap)

    if dist[x][y] != inf:
        continue
    if dist[x][y] < d:
        continue

    dist[x][y] = d

    for dx, dy in [(i, j) for i in range(-2, 3) for j in range(-2, 3) if i != 0 and j != 0]:
        nx, ny = x + dx, y + dy

        # 近傍なら移動A, そうでなければ移動B
        typa = abs(dx) + abs(dy) == 1
        nd = d + (0 if typa else 1)

        if check_range(nx, ny) and dist[nx][ny] == inf and s[nx][ny] == '.':
            item = (nd, nx, ny)
            heapq.heappush(heap, item)

a = dist[dh - 1][dw - 1]
if a == inf:
    print(-1)
else:
    print(a)

Submission status: Wrong Answer

Solution code:
import heapq

h, w = list(map(int, input().split()))
ch, cw = list(map(int, input().split()))
dh, dw = list(map(int, input().split()))
s = [input() for _ in range(h)]

def check_range(x, y):
    return (0 <= x < h) and (0 <= y < w)

heap = [(0, ch - 1, cw - 1)]
inf = 1000000000
dist = [[inf for i in range(w)] for _ in range(h)]

while len(heap):
    if dist[dh - 1][dw - 1] != inf:
        break

    d, x, y = heapq.heappop(heap)

    if dist[x][y] != inf:
        continue
    if dist[x][y] < d:
        continue

    dist[x][y] = d

    for dx, dy in [(i, j) for i in range(-2, 3) for j in range(-2, 3) if i != j]:
        nx, ny = x + dx, y + dy

        # 近傍なら移動A, そうでなければ移動B
        typa = abs(dx) + abs(dy) == 1
        nd = d + (0 if typa else 1)

        if check_range(nx, ny) and dist[nx][ny] == inf and s[nx][ny] == '.':
            item = (nd, nx, ny)
            heapq.heappush(heap, item)

a = dist[dh - 1][dw - 1]
if a == inf:
    print(-1)
else:
    print(a)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque


def input():
    return sys.stdin.readline().rstrip()


def main():
    H, W = map(int, input().split())
    ch, cw = map(int, input().split())
    dh, dw = map(int, input().split())
    S = [list(map(str, input())) for _ in range(H)]

    q = deque()
    q.append((ch - 1, cw - 1, 0))

    pathA = set([(1, 0), (0, 1), (-1, 0), (0, -1)])
    pathB = set()
    for i in range(5):
        for j in range(5):
            pathB.add((i - 2, j - 2))

    pathB = pathB - pathA - set((0, 0))

    visited = [[-1] * W for _ in range(H)]

    while q:
        h, w, d = q.popleft()
        if visited[h][w] != -1:
            continue
        visited[h][w] = d

        for h1, w1 in pathA:
            newh = h + h1
            neww = w + w1
            if newh >= 0 and newh < H and neww >= 0 and neww < W and S[newh][neww] == ".":
                if visited[newh][neww] == -1:
                    q.appendleft((newh, neww, d))

                if newh ==dh-1 and newh ==dw-1:
                    print(d)
                    exit()

        for h1, w1 in pathB:
            newh = h + h1
            neww = w + w1
            if newh >= 0 and newh < H and neww >= 0 and neww < W and S[newh][neww] == ".":
                q.append((newh, neww, d + 1))

    print(visited[dh - 1][dw - 1])


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque


def input():
    return sys.stdin.readline().rstrip()


def main():
    H, W = map(int, input().split())
    ch, cw = map(int, input().split())
    dh, dw = map(int, input().split())
    S = [list(map(str, input())) for _ in range(H)]

    q = deque()
    q.append((ch - 1, cw - 1, 0))

    pathA = set([(1, 0), (0, 1), (-1, 0), (0, -1)])
    pathB = set()
    for i in range(5):
        for j in range(5):
            pathB.add((i - 2, j - 2))

    pathB = pathB - pathA - set((0, 0))

    visited = [[-1] * W for _ in range(H)]

    while q:
        h, w, d = q.popleft()
        if visited[h][w] != -1:
            continue
        visited[h][w] = d

        for h1, w1 in pathA:
            newh = h + h1
            neww = w + w1
            if newh >= 0 and newh < H and neww >= 0 and neww < W and S[newh][neww] == ".":
                if visited[newh][neww] == -1:
                    q.appendleft((newh, neww, d))

                if newh ==dh-1 and newh ==dw-1:
                    print(d)
                    exit()

        for h1, w1 in pathB:
            newh = h + h1
            neww = w + w1
            if newh >= 0 and newh < H and neww >= 0 and neww < W and S[newh][neww] == ".":
                q.append((newh, neww, d + 1))

                if newh ==dh-1 and newh ==dw-1:
                    print(d+1)
                    exit()

    print(visited[dh - 1][dw - 1])


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import heapq

h, w = list(map(int, input().split()))
ch, cw = list(map(int, input().split()))
dh, dw = list(map(int, input().split()))
s = [input() for _ in range(h)]

def check_range(x, y):
    return (0 <= x < h) and (0 <= y < w)

heap = [(0, ch - 1, cw - 1)]
inf = 1000000000
dist = [[inf for i in range(w)] for _ in range(h)]

def get_contain_wall(x, y):
    return any('#' in row[max(0, y-2):min(w, y+2)] for row in s[max(0, x-2):min(h, x+2)])
#cwmap = [[get_contain_wall(i, j) for j in range(w)] for i in range(h)]
cwmap = [[True for j in range(w)] for i in range(h)]

while len(heap):
    if dist[dh - 1][dw - 1] != inf:
        break

    d, x, y = heapq.heappop(heap)

    if dist[x][y] != inf:
        continue
    if dist[x][y] < d:
        continue

    dist[x][y] = d

    for dx, dy in [(i, j) for i in range(-2, 3) for j in range(-2, 3) if i != j]:
        nx, ny = x + dx, y + dy

        # 近傍なら移動A, そうでなければ移動B
        typa = abs(dx) + abs(dy) == 1
        nd = d + (0 if typa else 1)

        # 壁が無ければ移動Bをする必要がない
        if not typa and not cwmap[x][y]:
            continue
        if check_range(nx, ny) and dist[nx][ny] == inf and s[nx][ny] == '.':
            item = (nd, nx, ny)
            heapq.heappush(heap, item)

a = dist[dh - 1][dw - 1]
if a == inf:
    print(-1)
else:
    print(a)

Submission status: Wrong Answer

Solution code:
import heapq

h, w = list(map(int, input().split()))
ch, cw = list(map(int, input().split()))
dh, dw = list(map(int, input().split()))
s = [input() for _ in range(h)]

def check_range(x, y):
    return (0 <= x < h) and (0 <= y < w)

heap = [(0, ch - 1, cw - 1)]
inf = 1000000000
dist = [[inf for i in range(w)] for _ in range(h)]

def get_contain_wall(x, y):
    return any('#' in row[max(0, y-2):min(w, y+2)] for row in s[max(0, x-2):min(h, x+2)])
cwmap = [[get_contain_wall(i, j) for j in range(w)] for i in range(h)]

while len(heap):
    if dist[dh - 1][dw - 1] != inf:
        break

    d, x, y = heapq.heappop(heap)

    if dist[x][y] != inf:
        continue
    if dist[x][y] < d:
        continue

    dist[x][y] = d

    for dx, dy in [(i, j) for i in range(-2, 3) for j in range(-2, 3) if i != j]:
        nx, ny = x + dx, y + dy

        # 近傍なら移動A, そうでなければ移動B
        typa = abs(dx) + abs(dy) == 1
        nd = d + (0 if typa else 1)

        # 壁が無ければ移動Bをする必要がない
        if not typa and not cwmap[x][y]:
            continue
        if check_range(nx, ny) and dist[nx][ny] == inf and s[nx][ny] == '.':
            item = (nd, nx, ny)
            heapq.heappush(heap, item)

a = dist[dh - 1][dw - 1]
if a == inf:
    print(-1)
else:
    print(a)

Submission status: Wrong Answer

Solution code:
import heapq

h, w = list(map(int, input().split()))
ch, cw = list(map(int, input().split()))
dh, dw = list(map(int, input().split()))
s = [input() for _ in range(h)]

def check_range(x, y):
    return (0 <= x < h) and (0 <= y < w)

heap = [(0, ch - 1, cw - 1)]
inf = 1000000000
dist = [[inf for i in range(w)] for _ in range(h)]

def get_contain_wall(x, y):
    return any('#' in row[max(0, y-2):min(h, y+2)] for row in s[max(0, x-2):min(h, x+2)])
cwmap = [[get_contain_wall(i, j) for j in range(w)] for i in range(h)]

while len(heap):
    if dist[dh - 1][dw - 1] != inf:
        break

    d, x, y = heapq.heappop(heap)

    if dist[x][y] != inf:
        continue
    if dist[x][y] < d:
        continue

    dist[x][y] = d

    for dx, dy in [(i, j) for i in range(-2, 3) for j in range(-2, 3) if i != j]:
        nx, ny = x + dx, y + dy

        # 近傍なら移動A, そうでなければ移動B
        typa = abs(dx) + abs(dy) == 1
        nd = d + (0 if typa else 1)

        # 壁が無ければ移動Bをする必要がない
        if not typa and not cwmap[x][y]:
            continue
        if check_range(nx, ny) and dist[nx][ny] == inf and s[nx][ny] == '.':
            item = (nd, nx, ny)
            heapq.heappush(heap, item)

a = dist[dh - 1][dw - 1]
if a == inf:
    print(-1)
else:
    print(a)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
sx, sy = map(int, input().split())
sx -= 1; sy -= 1
tx, ty = map(int, input().split())
tx -= 1; ty -= 1

S = [input() for _ in range(H)]


dxy = [(-1, 0), (0, -1), (1, 0), (0, 1)]
dist = {(sx, sy) : 0}
next_points = [(sx, sy)]
def bfs(d):
    global dist, next_points
    stack = next_points.copy()
    next_points = []
    while stack:
        x, y = stack.pop()
        count = 0
        for dx, dy in dxy:
            xx = x + dx; yy = y + dy
            if 0 <= xx < H and 0 <= yy < W and S[xx][yy] == '.':
                count += 1
                if (xx, yy) not in dist:
                    dist[(xx, yy)] = d
                    stack.append((xx, yy))
        
        if count < 4:
            for dx in range(-2, 3):
                xx = x + dx
                for dy in range(-2, 3):
                    yy = y + dy
                    if (xx, yy) not in dist and 0 <= xx < H and 0 <= yy < W and S[xx][yy] == '.':
                        dist[(xx, yy)] = d + 1
                        next_points.append((xx, yy))

d = 0
while (tx, ty) not in dist:
    bfs(d)
    d += 1
    if len(next_points) == 0:
        if (tx, ty) in dist:
            print(dist[(tx, ty)])
        else:
            print(-1)
        quit()

print(dist[(tx, ty)])

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())
m=[list(input()) for _ in range(H)]
s=(Ch-1,Cw-1)
g=(Dh-1,Dw-1)
q=deque()
q.append(s)
q2=deque()
q2.append(s)
c=0
d={s:0}
while q:
    while q:
        y,x=q.popleft()
        for dy,dx in [(0,1),(1,0),(0,-1),(-1,0)]:
            ny=y+dy
            nx=x+dx
            if ny<0 or ny>=H or nx<0 or nx>=W:
                continue
            if m[ny][nx]=='.' and (ny,nx) not in d:
                q.append((ny,nx))
                q2.append((ny,nx))
                d[(ny,nx)]=c
    c+=1
    while q2:
        y,x=q2.popleft()
        for dy in range(-2,3):
            for dx in range(-2,3):
                if abs(dy)+abs(dx)<=1:
                    continue
                ny=y+dy
                nx=x+dx
                if ny<0 or ny>=H or nx<0 or nx>=W:
                    continue
                if m[ny][nx]=='.' and (ny,nx) not in d:
                    q.append((ny,nx))
                    d[(ny,nx)]=c
if g in d:
    print(d[g])
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())
ddh,ddw=map(int,input().split())
s=[[c for c in input()] for _ in range(h)]

q=deque([(ch-1,cw-1,0)])
visit=[[0]*w for _ in range(h)]

while len(q)>0:
    uh,uw,cnt=deque.popleft(q)
    if visit[uh][uw]==1:
        continue

    stack=[(uh,uw)]
    stack2=[(uh,uw)]
    while len(stack)>0:
        vh,vw=stack.pop()

        visit[vh][vw]=1

        if vh==ddh-1 and vw==ddw-1:
            print(cnt)
            exit(0)

        for dh,dw in ((0,1),(0,-1),(1,0),(-1,0)):
            nh=vh+dh
            nw=vw+dw
            if 0<=nh<h and 0<=nw<w and visit[nh][nw]==0 and s[nh][nw]=='.':
                stack.append((nh,nw))
                stack2.append((nh,nw))

    
    while len(stack2)>0:
        vh,vw=stack2.pop()

        for dx in range(-2,3):
            for dy in (-2,2):
                nh=vh+dx
                nw=vw+dy

                if 0<=nh<h and 0<=nw<w and visit[nh][nw]==0 and s[nh][nw]=='.':
                    deque.append(q,(nh,nw,cnt+1))
                
                nh=vh+dy
                nw=vw+dx

                if 0<=nh<h and 0<=nw<w and visit[nh][nw]==0 and s[nh][nw]=='.':
                    deque.append(q,(nh,nw,cnt+1))
                
print(-1)

Submission status: Wrong Answer

Solution code:
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)]
v = [[False] * W for _ in range(H)]
dq = deque([(0, CH, CW)])
while dq:
    d, h, w = dq.popleft()
    if (h, w) == (DH, DW):
        print(d)
        exit()
    if v[h][w]:
        continue
    v[h][w] = True
    for mh, mw in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
        nh, nw = mh + h, mw + w
        if 0 <= nh < H and 0 <= nw < W and S[nh][nw] == '.':
            dq.appendleft([d, nh, nw])
    for mh in range(-2, 3):
        for mw in range(-2, 3):
            if h == w == 0:
                continue
            nh, nw = mh + h, mw + w
            if 0 <= nh < H and 0 <= nw < W and S[nh][nw] == '.' and not v[nh][nw]:
                dq.append([d + 1, nh, nw])
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())
mp = [None]+[[None]+[c for c in input()] for _ in range(h)]

d = deque([(ch,cw)])
mp[ch][cw]=0

cost = 0

def mass11(_h, _w):
    l = []
    for i,j in [(-1,0),(1,0),(0,1),(0,-1)]:
        side_h, side_w = _h+i, _w+j
        if side_h>0 and side_h<=h and side_w>0 and side_w<=w:
            l.append((side_h, side_w))
    return l 

def mass55(_h, _w):
    l = []
    for i in range(5):
        for j in range(5):
            side_h, side_w = _h+i-2, _w+j-2
            if side_h>0 and side_h<=h and side_w>0 and side_w<=w:
                l.append((side_h, side_w))
    return l 


while not len(d) == 0:
    _h, _w = d.popleft()
    center_point = mp[_h][_w]
    # print(_h, _w)
    for side_h, side_w in mass11(_h, _w):
        side = mp[side_h][side_w]
        print('side',side_h, side_w,side)
        if not side == '#' and not side == mp[_h][_w]:
            mp[side_h][side_w]=center_point
            d.appendleft((side_h, side_w))
    for side_h, side_w in mass55(_h, _w):
        if mp[side_h][side_w] == '.':
            mp[side_h][side_w]=center_point+1
            d.append((side_h, side_w))

if mp[dh][dw] =='.':
    print(-1)
else:
    print(mp[dh][dw])

# print(mp)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
sx, sy = map(int, input().split())
sx -= 1; sy -= 1
tx, ty = map(int, input().split())
tx -= 1; ty -= 1

S = [input() for _ in range(H)]


dxy = [(-1, 0), (0, -1), (1, 0), (0, 1)]
dxy2 = [(-2, 0), (0, -2), (2, 0), (0, 2)]
warp = [
    [(-2, -2), (-2, -1), (-1, -2), (-1, -1)],
    [(2, -2), (2, -1), (1, -2), (1, -1)],
    [(2, 2), (2, 1), (1, 2), (1, 2)],
    [(-2, 2), (-2, 1), (-1, 2), (-1, 1)],
]

pattern = {}
for i in range(2**4):
    res = []
    tf = []
    for j in range(4):
        tf.append(i>>j & 1)
        if tf[-1]:
            res.append(dxy2[j])
    
    if tf[0] and tf[1]:
        res += warp[0]
    if tf[1] and tf[2]:
        res += warp[1]
    if tf[2] and tf[3]:
        res += warp[2]
    if tf[3] and tf[0]:
        res += warp[3]
    pattern[i] = res


dist = {(sx, sy) : 0}
next_points = set([(sx, sy)])
def bfs(d):
    global dist, next_points
    stack = list(next_points)
    next_points = set()
    while stack:
        x, y = stack.pop()
        idx = 0
        for i, (dx, dy) in enumerate(dxy):
            xx = x + dx; yy = y + dy
            if 0 <= xx < H and 0 <= yy < W:
                if S[xx][yy] == '.':
                    if (xx, yy) not in dist:
                        stack.append((xx, yy))
                        dist[(xx, yy)] = d
                else:
                    idx += 1 << i
        
        for dx, dy in pattern[idx]:
            xx = x + dx; yy = y + dy
            if (xx, yy) not in dist and 0 <= xx < H and 0 <= yy < W and S[xx][yy] == '.':
                next_points.add((xx, yy))
                dist[(xx, yy)] = d + 1


d = 0
while (tx, ty) not in dist:
    bfs(d)
    d += 1
    if len(next_points) == 0:
        if (tx, ty) in dist:
            print(dist[(tx, ty)])
        else:
            print(-1)
        quit()

print(dist[(tx, ty)])

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import deque
input = stdin.readline

H, W = map(int, input().rstrip().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)]
v = [[False] * W for _ in range(H)]
dq = deque([(0, CH, CW)])
while dq:
    d, h, w = dq.popleft()
    if (h, w) == (DH, DW):
        print(d)
        exit()
    if v[h][w]:
        continue
    v[h][w] = True
    for mh, mw in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
        nh, nw = mh + h, mw + w
        if 0 <= nh < H and 0 <= nw < W and S[nh][nw] == '.':
            dq.appendleft([d, nh, nw])
    for mh in range(-2, 3):
        for mw in range(-2, 3):
            if h == w == 0:
                continue
            nh, nw = mh + h, mw + w
            if 0 <= nh < H and 0 <= nw < W and S[nh][nw] == '.' and not v[nh][nw]:
                dq.append([d + 1, nh, nw])
print(-1)