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
l=lambda:[int(x) for x in input().split()]
H,W=l()
CH,CW=l()
DH,DW=l()
S=[[x=='#' for x in list(input())] for _ in range(H)]
q=deque([(CH-1,CW-1,0)])
checked=set()
def check(h,w,c,o):
    if h < 0 or h >= H or w < 0 or w >= W:
        return
    if S[h][w]:
        return
    if o:
        q.append((h,w,c))
    else:
        q.appendleft((h,w,c+1))

def main():
    dh=DH-1
    dw=DW-1
    while q:
        h,w,c=q.pop()
        if h==dh and w==dw:
            print(c)
            return
        key=(h,w)
        if key in checked:
            continue
        checked.add(key)
        # walk
        check(h-1,w-0,c,True)
        check(h-0,w-1,c,True)
        check(h+1,w+0,c,True)
        check(h+0,w+1,c,True)
        # warp
        check(h-1,w-1,c,False)
        check(h+1,w+1,c,False)
        for i in [-2,+2]:
            for j in range(-2,3):
                check(h+i,w+j,c,False)
                check(h+j,w+i,c,False)
    print(-1)
main()

Submission status: Wrong Answer

Solution code:
from collections import deque
l=lambda:[int(x) for x in input().split()]
H,W=l()
CH,CW=l()
DH,DW=l()
S=[[x=='#' for x in list(input())] for _ in range(H)]
q=deque([(CH-1,CW-1,0)])
checked=set()
def check(h,w,c,o):
    if h < 0 or h >= H or w < 0 or w >= W:
        return
    if S[h][w]:
        return
    if o:
        q.append((h,w,c))
    else:
        q.appendleft((h,w,c+1))
dh=DH-1
dw=DW-1
while q:
    h,w,c=q.pop()
    if h==dh and w==dw:
        print(c)
        exit(0)
    key=(h,w)
    if key in checked:
        continue
    checked.add(key)
    # walk
    check(h-1,w-0,c,True)
    check(h-0,w-1,c,True)
    check(h+1,w+0,c,True)
    check(h+0,w+1,c,True)
    # warp
    check(h-1,w-1,c,False)
    check(h+1,w+1,c,False)
    for i in [-2,+2]:
        for j in range(-2,3):
            check(h+i,w+j,c,False)
            check(h+j,w+i,c,False)
print(-1)

Submission status: Wrong Answer

Solution code:
l=lambda:[int(x) for x in input().split()]
H,W=l()
CH,CW=l()
DH,DW=l()
S=[[x=='#' for x in list(input())] for _ in range(H)]
q=[(CH-1,CW-1,0)]
checked=set()
def check(h,w,c,o):
    if h < 0 or h >= H or w < 0 or w >= W:
        return
    if S[h][w]:
        return
    if o:
        q.append((h,w,c))
    else:
        q.insert(0,(h,w,c+1))
dh=DH-1
dw=DW-1
while q:
    h,w,c=q.pop()
    if h==dh and w==dw:
        print(c)
        exit(0)
    key=(h,w)
    if key in checked:
        continue
    checked.add(key)
    # walk
    check(h-1,w-0,c,True)
    check(h-0,w-1,c,True)
    check(h+1,w+0,c,True)
    check(h+0,w+1,c,True)
    # warp
    check(h-1,w-1,c,False)
    check(h+1,w+1,c,False)
    for i in [-2,+2]:
        for j in range(-2,3):
            check(h+i,w+j,c,False)
            check(h+j,w+i,c,False)
print(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys

INF = 10 ** 8

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(list(input()))

d = [[INF for i in range(W)] for j in range(H)]

Q = deque([[CH-1, CW-1, 0]])
d[CH - 1][CW - 1] = 0

while len(Q) != 0:
    x, y, cnt = Q.popleft()
    
    if x == DH - 1 and y == DW - 1:
        print(cnt)
        sys.exit()
    
    for tx in range(-2, 3):
        for ty in range(-2, 3):
            nx = x + tx
            ny = y + ty
            if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == "." and d[nx][ny] == INF:
                if abs(tx)+abs(ty) == 1:
                    d[nx][ny] = cnt
                    Q.appendleft([nx, ny, cnt])
                else:
                    d[nx][ny] = cnt + 1
                    Q.append([nx, ny, cnt + 1])

print(-1)

Submission status: Wrong Answer

Solution code:
import sys
import bisect
import itertools
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def solve():
    readline = sys.stdin.buffer.readline
    input = readline
    mod = 10 ** 9 + 7
    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
    Smatrix = []
    allpoints = set()
    for _ in range(H):
        s = list(input())
        Smatrix.append(s)

    for h in range(H):
        for w in range(W):
            if Smatrix[h][w] == '.':
                allpoints.add((h, w))

    def checkaround(h, w):
        now = neomatrix[h][w]
        if now == 0:
            now = point + 1
        point += 1
        side = [w-1, w+1]
        updown = [h-1, h+1]
        for s in side:
            for u in updown:
                if 0 <= s < W and 0 <= u < H:
                    if Smatrix[s][u] == '.':
                        neomatrix[s][u] = now

    d = collections.deque([(Ch, Cw)])
    checked = set()

    count = 0
    flag = False
    while len(d) > 0:
        beforechecked = set()
        while len(d) > 0:
            nowh, noww = d.popleft()
            checked.add((nowh, noww))
            beforechecked.add((nowh, noww))
            if (nowh, noww) == (Dh, Dw):
                print(count)
                flag = True
                sys.exit()
            hwlist = [(nowh+1, noww), (nowh, noww+1), (nowh-1, noww), (nowh, noww-1)]
            for h, w in hwlist:
                if (h, w) in checked:
                    continue
                if 0 <= h < H and 0 <= w < W and Smatrix[h][w] == '.':
                    d.append((h, w))
        count += 1
        for bh, bw in beforechecked:
            for hdef in range(5):
                for wdef in range(5):
                    h, w = bh - 2 + hdef, bw - 2 + wdef
                    if (h, w) in checked:
                        continue
                    if 0 <= h < H and 0 <= w < W and Smatrix[h][w] == '.':
                        d.append((h, w))
    print(-1)




if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

import heapq

INF = 10 ** 10

class Edge():
    def __init__(self, start, end, cost):
        self.start = start
        self.end = end
        self.cost = cost
    def __str__(self):
        return 'Edge: {} -> {}, cost {}'.format(self.start, self.end, self.cost)

def dijkstra(n, G, s):
    dist = [INF] * n
    dist[s] = 0
    h = []
    heapq.heappush(h, (0, s))

    while h:
        # 使っていない頂点のうち、現時点で最も距離の近いものを選びvとする
        d, v = heapq.heappop(h)
        if dist[v] < d:
            continue
        # vから到達可能な頂点について、距離が短くなれば更新
        for e in G[v]:
            if dist[e.end] > dist[v] + e.cost:
                dist[e.end] = dist[v] + e.cost
                heapq.heappush(h, (dist[e.end], e.end))
    return dist


H, W = map(int, input().split())
N = H * W
ch, cw = map(lambda x:int(x)-1, input().split())
gh, gw = map(lambda x:int(x)-1, input().split())

s = []
es = [[] for i in range(N)]

for h in range(H):
   s.append(input().rstrip('\n'))

for h in range(H):
    for w in range(W):
        if s[h][w] != '.':
            continue
        for dh in (-2, -1, 0, 1, 2):
            for dw in (-2, -1, 0, 1, 2):
                if dh == 0 and dw == 0:
                    continue
                nh = h + dh
                nw = h + dw
                if 0 <= nh and nh < H and 0 <= nw and nw < W and s[nh][nw] == '.':
                    a = h*W+w
                    b = nh*W+nw
                    if (dh, dw) in [(-1,0),(1,0),(0,-1),(0,1)]:
                        es[a].append(Edge(a, b, 0))
                    else:
                        es[a].append(Edge(a, b, 1))

dist = dijkstra(N, es, ch*W+cw)
if dist[gh*W+gw] == INF:
    print(-1)
else:
    print(dist[gh*W+gw])

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

maze=[list(input()) for i in range(h)]
distance=[[10**10 for j in range(w)] for i in range(h)]

distance[ch][cw]=0


Q=deque()
Q.append((ch,cw))



while Q:
  p=Q.popleft()
  flag=False

  if p[0]==dh and p[1]==dw:
    break
  
  dx=[1,1,-1,-1]
  dy=[-1,1,-1,1]
  dddx=[0,0,2,-2,2,2,2,2,1,1,1,1,-2,-2,-2,-2,-1,-1,-1,-1]
  dddy=[2,-2,0,0,2,1,-1,-2,2,1,-1,-2,2,1,-1,-2,2,1,-1,-2]

  for i in range(4):
    nx=p[0]+dx[i]
    ny=p[1]+dy[i]

    if 0<=nx<h and 0<=ny<w and maze[nx][ny]!='#' and distance[p[0]][p[1]]<distance[nx][ny]:
      Q.appendleft((nx,ny))
      flag=True
      distance[nx][ny]=distance[p[0]][p[1]]

  if flag:
    continue

  for j in range(20):
    nnnx=p[0]+dddx[j]
    nnny=p[1]+dddy[j]

    if 0<=nnnx<h and 0<=nnny<w and maze[nnnx][nnny]!='#' and distance[p[0]][p[1]]+1<distance[nnnx][nnny]:
      Q.append((nnnx,nnny))
      distance[nnnx][nnny]=distance[p[0]][p[1]]+1


if distance[dh][dw]==10**10:
  print(-1)
  exit()


print(distance[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())
ch-=1
cw-=1
dh-=1
dw-=1

maze=[list(input()) for i in range(h)]
distance=[[10**10 for j in range(w)] for i in range(h)]

distance[ch][cw]=0


Q=deque()
Q.append((ch,cw))



while Q:
  p=Q.popleft()
  flag=False

  if p[0]==dh and p[1]==dw:
    break
  
  dx=[1,1,-1,-1]
  dy=[-1,1,-1,1]
  dddx=[0,0,2,-2,2,2,2,2,1,1,1,1,-2,-2,-2,-2,-1,-1,-1,-1]
  dddy=[2,-2,0,0,2,1,-1,-2,2,1,-1,-2,2,1,-1,-2,2,1,-1,-2]

  for i in range(4):
    nx=p[0]+dx[i]
    ny=p[1]+dy[i]

    if 0<=nx<h and 0<=ny<w and maze[nx][ny]!='#' and distance[p[0]][p[1]]<distance[nx][ny]:
      Q.appendleft((nx,ny))
      flag=True
      distance[nx][ny]=distance[p[0]][p[1]]

  if flag:
    continue

  for j in range(20):
    nnnx=p[0]+dddx[j]
    nnny=p[1]+dddy[j]

    if 0<=nnnx<h and 0<=nnny<w and maze[nnnx][nnny]!='#' and distance[p[0]][p[1]]<distance[nnnx][nnny]+1:
      Q.append((nnnx,nnny))
      distance[nnnx][nnny]=distance[p[0]][p[1]]+1


if distance[dh][dw]==10**10:
  print(-1)
  exit()


print(distance[dh][dw])

Submission status: Wrong Answer

Solution code:
l=lambda:[int(x) for x in input().split()]
H,W=l()
CH,CW=l()
DH,DW=l()
S=[[x=='#' for x in list(input())] for _ in range(H)]
d={(CH-1,CW-1):0}
q=[(CH-1,CW-1,0)]
def check(h,w,c,o):
    if h < 0 or h >= H or w < 0 or w >= W:
        return False
    key=(h,w)
    if key in d:
        if d[key] <= c:
            return False
    if S[h][w]:
        return False
    if o:
        q.append((h,w,c))
    else:
        q.insert(0,(h,w,c+1))
    d[key]=c
    return True
while q:
    h,w,c=q.pop()
    # walk
    check(h-1,w-0,c,True)
    check(h-0,w-1,c,True)
    check(h+1,w+0,c,True)
    check(h+0,w+1,c,True)
    # warp
    check(h-1,w-1,c,False)
    check(h+1,w+1,c,False)
    for i in [-2,+2]:
        for j in range(-2,3):
            check(h+i,w+j,c,False)
            check(h+i,w+j,c,False)
key=(DH-1,DW-1)
if key in d:
    print(d[key])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
l=lambda:[int(x) for x in input().split()]
H,W=l()
CH,CW=l()
DH,DW=l()
S=[[x=='#' for x in list(input())] for _ in range(H)]
d={(CH-1,CW-1):0}
q=[(CH-1,CW-1,0)]
def check(h,w,c,o):
    if h < 0 or h >= H or w < 0 or w >= W:
        return False
    key=(h,w)
    if key in d:
        return False
        #if d[key] <= c:
        #    return False
    if S[h][w]:
        return False
    if o:
        q.append((h,w,c))
    else:
        q.insert(0,(h,w,c+1))
    d[key]=c
    return True
while q:
    h,w,c=q.pop()
    # walk
    check(h-1,w-0,c,True)
    check(h-0,w-1,c,True)
    check(h+1,w+0,c,True)
    check(h+0,w+1,c,True)
    # warp
    check(h-1,w-1,c,False)
    check(h+1,w+1,c,False)
    for i in [-2,+2]:
        for j in range(-2,3):
            check(h+i,w+j,c,False)
            check(h+i,w+j,c,False)
key=(DH-1,DW-1)
if key in d:
    print(d[key])
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())
ch-=1
cw-=1
dh-=1
dw-=1

maze=[list(input()) for i in range(h)]
distance=[[10**10 for j in range(w)] for i in range(h)]
visited=[[False]*w for i in range(h)]

distance[ch][cw]=0


Q=deque()
Q.append((ch,cw))



while Q:
  p=Q.popleft()

  if p[0]==dh and p[1]==dw:
    break
  
  dx=[1,1,-1,-1]
  dy=[-1,1,-1,1]
  dddx=[0,0,2,-2,2,2,2,2,1,1,1,1,-2,-2,-2,-2,-1,-1,-1,-1]
  dddy=[2,-2,0,0,2,1,-1,-2,2,1,-1,-2,2,1,-1,-2,2,1,-1,-2]

  for i in range(4):
    nx=p[0]+dx[i]
    ny=p[1]+dy[i]

    if 0<=nx<h and 0<=ny<w and visited[nx][ny]==False and maze[nx][ny]!='#' and distance[p[0]][p[1]]<distance[nx][ny]:
      Q.appendleft((nx,ny))
      distance[nx][ny]=distance[p[0]][p[1]]

  for j in range(20):
    nnnx=p[0]+dddx[j]
    nnny=p[1]+dddy[j]

    if 0<=nnnx<h and 0<=nnny<w and visited[nnnx][nnny]==False and maze[nnnx][nnny]!='#' and distance[p[0]][p[1]]<distance[nnnx][nnny]+1:
      Q.append((nnnx,nnny))
      distance[nnnx][nnny]=min(distance[nnnx][nnny],distance[p[0]][p[1]]+1)


if distance[dh][dw]==10**10:
  print(-1)
  exit()


print(distance[dh][dw])

Submission status: Wrong Answer

Solution code:
l=lambda:[int(x) for x in input().split()]
H,W=l()
CH,CW=l()
DH,DW=l()
S=[[x=='#' for x in list(input())] for _ in range(H)]
d={(CH-1,CW-1):0}
q=[(CH-1,CW-1,0)]
def check(h,w,c):
    if h < 0 or h >= H or w < 0 or w >= W:
        return False
    key=(h,w)
    if key in d:
        if d[key] <= c:
            return False
    if S[h][w]:
        return False
    q.append((h,w,c))
    d[key]=c
    return True
while q:
    h,w,c=q.pop()
    # walk
    check(h-1,w-0,c)
    check(h-0,w-1,c)
    check(h+1,w+0,c)
    check(h+0,w+1,c)
    # warp
    for i in range(-2,3):
        for j in range(-2,3):
            if i != 0 and j != 0:
                check(h+i,w+j,c+1)
key=(DH-1,DW-1)
if key in d:
    print(d[key])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w = map(int, input().split())
sx,sy = map(int, input().split())
ex, ey = map(int, input().split())

def legal(x,y):
    if x>=h or y>=w or x<0 or y<0 or board[x][y] == '#':
        return False
    return True

sx -=1 
sy -= 1
ex -= 1
ey -= 1

xs = deque()
ys = deque()

board = []
for i in range(h):
    board.append(list(input()))

xs.append(sx)
ys.append(sy)

dist = [[(10**5) for _ in range(w)] for _ in range(h)]

dist[sx][sy] = 0

dx = [-1,1,0,0]
dy = [0,0,-1,1]

while xs:
    curx = xs.popleft()
    cury = ys.popleft()
    
    for i in range(4):
        jx = curx+dx[i]
        jy = cury+dy[i]
        if not legal(jx,jy):
            continue
        if dist[jx][jy] > dist[curx][cury]:
            dist[jx][jy] = dist[curx][cury]
            xs.appendleft(jx)
            ys.appendleft(jy)
            
    for i in range(curx-2, curx+3):
        for j in range(cury-2, cury+3):
            if not legal(i,j):
                continue
            if dist[i][j]>dist[curx][cury]+1:
                dist[i][j]=dist[curx][cury]+1
                xs.append(i)
                ys.append(j)

if dist[ex][ey] >= (10**5):
    print(-1)
else:
    print(dist[ex][ey])

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w = map(int, input().split())
sx,sy = map(int, input().split())
ex, ey = map(int, input().split())

def legal(x,y):
    if x>=h or y>=w or x<0 or y<0 or board[x][y] == '#':
        return False
    return True

sx -=1 
sy -= 1
ex -= 1
ey -= 1

xs = deque()
ys = deque()

board = []
for i in range(h):
    board.append(list(input()))

xs.append(sx)
ys.append(sy)

dist = [[(10**5) for _ in range(w)] for _ in range(h)]

dist[sx][sy] = 0

dx = [-1,1,0,0]
dy = [0,0,-1,1]

while xs:
    curx = xs.popleft()
    cury = ys.popleft()
    
    for i in range(4):
        jx = curx+dx[i]
        jy = cury+dy[i]
        if not legal(jx,jy):
            continue
        if dist[jx][jy] > dist[curx][cury]:
            dist[jx][jy] = dist[curx][cury]
            xs.appendleft(jx)
            ys.appendleft(jy)
            
    for i in range(curx-2, curx+3):
        for j in range(cury-2, cury+3):
            if not legal(i,j):
                continue
            if dist[i][j]>dist[curx][cury]+1:
                dist[i][j]=dist[curx][cury]+1
                xs.append(i)
                ys.append(j)

if dist[ex][ey] > (10**5):
    print(-1)
else:
    print(dist[ex][ey])

Submission status: Wrong Answer

Solution code:
l=lambda:[int(x) for x in input().split()]
H,W=l()
CH,CW=l()
DH,DW=l()
S=[list(input()) for _ in range(H)]
d={(CH-1,CW-1):0}
q=[(CH-1,CW-1,0)]
def check(h,w,c):
    if h < 0 or h >= H or w < 0 or w >= W:
        return False
    key=(h,w)
    if key in d:
        return False
    if S[h][w]!='.':
        return False
    q.insert(0,(h,w,c))
    d[key]=c
    return True
while q:
    h,w,c=q.pop()
    # walk
    hit=False
    hit|=check(h-1,w-0,c)
    hit|=check(h-0,w-1,c)
    hit|=check(h+1,w+0,c)
    hit|=check(h+0,w+1,c)
    if hit:
        continue
    # warp
    for i in range(-2,3):
        for j in range(-2,3):
            check(h+i,w+j,c+1)
key=(DH-1,DW-1)
if key in d:
    print(d[key])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque

def bfs():
    h,w=map(int,input().split())
    sy,sx=map(int,input().split())
    gy,gx=map(int,input().split())
    sy-=1;sx-=1;gy-=1;gx-=1
    s=[list(input()) for _ in range(h)]
    dict={".":10000000,"#":-1}
    s=[[dict[s[y][x]] for x in range(w)]for y in range(h)]
    s[sy][sx]=0
    que=deque([[sy,sx]])
    temp=[(1,0),(0,1),(-1,0),(0,-1),(2,2),(2,1),(2,0),(2,-1),(2,-2),(1,2),(1,1),(1,-1),(1,-2),(0,2),(0,-2),(-1,2),(-1,1),(-1,-1),(-1,-2),(-2,2),(-2,1),(-2,0),(-2,-1),(-2,-2)]
    while que:
        y,x=que.popleft()
        for i,j in temp:
            if 0<=y+i<h and 0<=x+j<w and s[y+i][x+j]>=0:
                nexty,nextx=y+i,x+j
                dist=s[nexty][nextx]
                if abs(i)<=1 and abs(j)<=1 and dist>s[y][x]:
                    s[nexty][nextx]=s[y][x]
                    que.append([nexty,nextx])
                elif dist>s[y][x]+1:
                    s[nexty][nextx]=s[y][x]+1
                    que.append([nexty,nextx])

    if s[gy][gx]==10000000:
        print(-1)
    else:
        print(s[gy][gx])

bfs()

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w = map(int, input().split())
sx,sy = map(int, input().split())
ex, ey = map(int, input().split())

def legal(x,y):
    if x>=h or y>=w or x<0 or y<0 or board[x][y] == '#':
        return False
    return True

sx -=1 
sy -= 1
ex -= 1
ey -= 1

xs = deque()
ys = deque()

board = []
for i in range(h):
    board.append(list(input()))

xs.append(sx)
ys.append(sy)

dist = [[(10**5)+7 for _ in range(w)] for _ in range(h)]

dist[sx][sy] = 0

dx = [-1,1,0,0]
dy = [0,0,-1,1]

while xs:
    curx = xs.popleft()
    cury = ys.popleft()
    
    for i in range(4):
        jx = curx+dx[i]
        jy = cury+dy[i]
        if not legal(jx,jy):
            continue
        if dist[jx][jy] > dist[curx][cury]:
            dist[jx][jy] = dist[curx][cury]
            xs.appendleft(jx)
            ys.appendleft(jy)
            
    for i in range(curx-2, curx+3):
        for j in range(cury-2, cury+3):
            if not legal(i,j):
                continue
            if dist[i][j]>dist[curx][cury]+1:
                dist[i][j]=dist[curx][cury]+1
                xs.append(i)
                ys.append(j)

if dist[ex][ey] >= (10**5)+7:
    print(-1)
else:
    print(dist[ex][ey])

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

maze=[list(input()) for i in range(h)]
distance=[[10**10 for j in range(w)] for i in range(h)]
visited=[[False]*w for i in range(h)]

distance[ch][cw]=0


Q=deque()
Q.append((ch,cw))



while Q:
  flag=False
  p=Q.popleft()
  visited[p[0]][p[1]]=True

  if p[0]==dh and p[1]==dw:
    break
  
  dx=[1,1,-1,-1]
  dy=[-1,1,-1,1]
  dddx=[0,0,2,-2,2,2,2,2,1,1,1,1,-2,-2,-2,-2,-1,-1,-1,-1]
  dddy=[2,-2,0,0,2,1,-1,-2,2,1,-1,-2,2,1,-1,-2,2,1,-1,-2]

  for i in range(4):
    nx=p[0]+dx[i]
    ny=p[1]+dy[i]

    if 0<=nx<h and 0<=ny<w and visited[nx][ny]==False and maze[nx][ny]!='#':
      Q.appendleft((nx,ny))
      flag=True
      distance[nx][ny]=min(distance[p[0]][p[1]],distance[nx][ny])

  if flag:
    continue

  visited[p[0]][p[1]]=True
  for j in range(20):
    nnnx=p[0]+dddx[j]
    nnny=p[1]+dddy[j]

    if 0<=nnnx<h and 0<=nnny<w and visited[nnnx][nnny]==False and maze[nnnx][nnny]!='#':
      Q.append((nnnx,nnny))
      distance[nnnx][nnny]=min(distance[nnnx][nnny],distance[p[0]][p[1]]+1)


if distance[dh][dw]==10**10:
  print(-1)
  exit()


print(distance[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())
ch-=1
cw-=1
dh-=1
dw-=1

maze=[list(input()) for i in range(h)]
distance=[[10**10 for j in range(w)] for i in range(h)]
visited=[[False]*w for i in range(h)]

distance[ch][cw]=0


Q=deque()
Q.append((ch,cw))



while Q:
  global flag
  flag=False
  p=Q.popleft()
  visited[p[0]][p[1]]=True

  if p[0]==dh and p[1]==dw:
    break
  
  dx=[1,1,-1,-1]
  dy=[-1,1,-1,1]
  dddx=[0,0,2,-2,2,2,2,2,1,1,1,1,-2,-2,-2,-2,-1,-1,-1,-1]
  dddy=[2,-2,0,0,2,1,-1,-2,2,1,-1,-2,2,1,-1,-2,2,1,-1,-2]

  for i in range(4):
    nx=p[0]+dx[i]
    ny=p[1]+dy[i]

    if 0<=nx<h and 0<=ny<w and visited[nx][ny]==False and maze[nx][ny]!='#':
      Q.appendleft((nx,ny))
      flag=True
      distance[nx][ny]=min(distance[p[0]][p[1]],distance[nx][ny])

  if flag:
    continue

  visited[p[0]][p[1]]=True
  for j in range(20):
    nnnx=p[0]+dddx[j]
    nnny=p[1]+dddy[j]

    if 0<=nnnx<h and 0<=nnny<w and visited[nnnx][nnny]==False and maze[nnnx][nnny]!='#':
      Q.append((nnnx,nnny))
      distance[nnnx][nnny]=min(distance[nnnx][nnny],distance[p[0]][p[1]]+1)


if distance[dh][dw]==10**10:
  print(-1)
  exit()


print(distance[dh][dw])

Submission status: Wrong Answer

Solution code:
l=lambda:[int(x) for x in input().split()]
H,W=l()
CH,CW=l()
DH,DW=l()
S=[list(input()) for _ in range(H)]
d={(CH-1,CW-1):0}
q=[(CH-1,CW-1,0)]
def tryAppend(h,w,c,qs):
    p = (h,w)
    if p in d:
        return
    if 0<=h<H:
        if 0<=w<W:
            if S[h][w]=='.':
                d[p]=c
                for q in qs:
                    q.append((h,w,c))
while q:
    (h,w,c)=q.pop(0)
    # walk
    wq=[(h,w,c)]
    while wq:
        hh,ww,c=wq.pop()
        tryAppend(hh-1,w+0,c,[wq,q])
        tryAppend(hh+0,w-1,c,[wq,q])
        tryAppend(hh+1,w+0,c,[wq,q])
        tryAppend(hh+0,w+1,c,[wq,q])
    # warp
    for i in range(-2,3):
        for j in range(-2,3):
            if i!=0 and j!=0:
                tryAppend(h+i,w+j,c+1,[q])
p = (DH-1,DW-1)
if p in d:
    print(d[p])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
l=lambda:[int(x) for x in input().split()]
H,W=l()
CH,CW=l()
DH,DW=l()
S=[list(input()) for _ in range(H)]
d={(CH-1,CW-1):0}
q=[(CH-1,CW-1,0)]
def tryAppend(h,w,c,qs):
    if 0<=h<H:
        if 0<=w<W:
            if S[h][w]=='.':
                p=(h,w)
                if p in d:
                    d[p]=min(d[p],c)
                else:
                    d[p]=c
                    for q in qs:
                        q.append((h,w,c))
                    return True
    return False
while q:
    (h,w,c)=q.pop(0)
    # walk
    wq=[(h,w,c)]
    while wq:
        hh,ww,c=wq.pop()
        tryAppend(hh-1,w+0,c,[wq,q])
        tryAppend(hh+0,w-1,c,[wq,q])
        tryAppend(hh+1,w+0,c,[wq,q])
        tryAppend(hh+0,w+1,c,[wq,q])
    # warp
    for i in range(-2,3):
        for j in range(-2,3):
            if i!=0 and j!=0:
                tryAppend(h+i,w+j,c+1,[q])
p = (DH-1,DW-1)
if p in d:
    print(d[p])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import heapq

def in_the_map(x):
    if x[0] < 0 or H <= x[0] or x[1] < 0 or W <= x[1]:
        return False
    return True

H, W = [int(x) for x in input().split()]
c_h, c_w = [int(x) - 1 for x in input().split()]  # Convert to 0-origin
d_h, d_w = [int(x) - 1 for x in input().split()]  # Convert to 0-origin
s = [['.' for w in range(W)] for h in range(H)]
for h in range(H):
    s[h] = input()

visited = [[False for w in range(W)] for h in range(H)]
queue = []
heapq.heappush(queue, (0, (c_h, c_w)))  # Push start point with cost (number of warps) 0

while len(queue) > 0:
    cost, p = heapq.heappop(queue)

    if visited[p[0]][p[1]]:  # Skip if already visited (the priority queue assures we can visit to the point with the smallest cost)
        continue
    visited[p[0]][p[1]] = True

    if p[0] == d_h and p[1] == d_w:  # Finally reach to the goal
        print(cost)
        break
    # print(f'Visit {p}: {cost}')

    # Explore neighbours where can be reached by walk (with the same cost as p)
    neighbours = ((p[0], p[1] - 1), (p[0], p[1] + 1), (p[0] - 1, p[1]), (p[0] + 1, p[1]))
    for n in neighbours:
        if not in_the_map(n) or visited[n[0]][n[1]]:
            continue
        if s[n[0]][n[1]] == '.':  # not wall
            heapq.heappush(queue, (cost, n))  # For later exploration

    # Explore points where can be reached by warp from visited points(with +1 cost from p)
    for diff_h in range(-2, 2):
        for diff_w in range(-2, 2):
            h = p[0] + diff_h
            w = p[1] + diff_w
            if not in_the_map((h, w)) or visited[h][w]:
                continue
            if s[h][w] == '.': # not wall
                heapq.heappush(queue, (cost + 1, (h, w)))

if not visited[d_h][d_w]:  # Cannot reach to the goal
    print(-1)

Submission status: Wrong Answer

Solution code:
# import queue
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
table = []
for i in range(h):
    table.append(input())
# print(table)

def check(h_tmp, w_tmp, steps):
    if h_tmp < 0 or w_tmp < 0 or h_tmp >= h or w_tmp >= w:
        return False
    elif visit[h_tmp][w_tmp] <= steps:
        return False
    elif table[h_tmp][w_tmp] == '#':
        return False
    else:
        return True


# q = queue.Queue()
q = deque()
q.append((ch, cw, 0, 0))
ans = 1000000000
visit = [[1000000000] * w] * h
# print(visit)
while not len(q) == 0:
    now = q.popleft()
    visit[now[0]][now[1]] = min(visit[now[0]][now[1]], now[3])
    # print(q.qsize())
    # print(visit)
    if now[0] == dh and now[1] == dw:
        # print(now[0], now[1])
        ans = visit[now[0]][now[1]]
        # continue
        break
    # print(now[0], now[1])

    if check(now[0]+1, now[1], now[3]):
        q.appendleft((now[0]+1, now[1], now[2] + 1, now[3]))

    if check(now[0], now[1]+1, now[3]):
        q.appendleft((now[0], now[1]+1, now[2] + 1, now[3]))
    if check(now[0]-1, now[1], now[3]):
        q.appendleft((now[0]-1, now[1], now[2] + 1, now[3]))
    if check(now[0], now[1]-1, now[3]):
        q.appendleft((now[0], now[1]-1, now[2] + 1, now[3]))
    if check(now[0]+1, now[1]+1, now[3]+1):
        q.append((now[0]+1, now[1]+1, now[2] + 1, now[3]+1))
    if check(now[0]-1, now[1]+1, now[3]+1):
        q.append((now[0]-1, now[1]+1, now[2] + 1, now[3]+1))
    if check(now[0]+1, now[1]-1, now[3]+1):
        q.append((now[0]+1, now[1]-1, now[2] + 1, now[3]+1))
    if check(now[0]-1, now[1]-1, now[3]+1):
        q.append((now[0]-1, now[1]-1, now[2] + 1, now[3]+1))

    if check(now[0]+2, now[1], now[3]+1):
        q.append((now[0]+2, now[1], now[2] + 1, now[3]+1))
    if check(now[0]+2, now[1]+1, now[3]+1):
        q.append((now[0]+2, now[1]+1, now[2] + 1, now[3]+1))
    if check(now[0]+2, now[1]+2, now[3]+1):
        q.append((now[0]+2, now[1]+2, now[2] + 1, now[3]+1))
    if check(now[0]+2, now[1]-1, now[3]+1):
        q.append((now[0]+2, now[1]-1, now[2] + 1, now[3]+1))
    if check(now[0]+2, now[1]-2, now[3]+1):
        q.append((now[0]+2, now[1]-2, now[2] + 1, now[3]+1))
    if check(now[0]-2, now[1], now[3]+1):
        q.append((now[0]-2, now[1], now[2] + 1, now[3]+1))
    if check(now[0]-2, now[1]+1, now[3]+1):
        q.append((now[0]-2, now[1]+1, now[2] + 1, now[3]+1))
    if check(now[0]-2, now[1]+2, now[3]+1):
        q.append((now[0]-2, now[1]+2, now[2] + 1, now[3]+1))
    if check(now[0]-2, now[1]-1, now[3]+1):
        q.append((now[0]-2, now[1]-1, now[2] + 1, now[3]+1))
    if check(now[0]-2, now[1]-2, now[3]+1):
        q.append((now[0]-2, now[1]-2, now[2] + 1, now[3]+1))

    if check(now[0], now[1]+2, now[3]+1):
        q.append((now[0], now[1]+2, now[2] + 1, now[3]+1))
    if check(now[0]-1, now[1]+2, now[3]+1):
        q.append((now[0]-1, now[1]+2, now[2] + 1, now[3]+1))
    if check(now[0]+1, now[1]+2, now[3]+1):
        q.append((now[0]+1, now[1]+2, now[2] + 1, now[3]+1))
    if check(now[0], now[1]-2, now[3]+1):
        q.append((now[0], now[1]-2, now[2] + 1, now[3]+1))
    if check(now[0]+1, now[1]-2, now[3]+1):
        q.append((now[0]+1, now[1]-2, now[2] + 1, now[3]+1))
    if check(now[0]-1, now[1]-2, now[3]+1):
        q.append((now[0]-1, now[1]-2, now[2] + 1, now[3]+1))
if not ans == 1000000000:
    print(ans)
else:
    print(-1)