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 heapq import *
from pprint import pprint

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=[[*input()] for _ in range(H)]

def nbs4(i,j):
    for ni,nj in [
                (i-1,j),
        (i,j-1),        (i,j+1),
                (i+1,j),
    ]:
        if 0<=ni<H and 0<=nj<W:
            yield ni,nj

def nbs8(i,j):
    for ni,nj in [
        (i-1,j-1),(i-1,j),(i-1,j+1),
        (  i,j-1),        (  i,j+1),
        (i+1,j-1),(i+1,j),(i+1,j+1),
    ]:
        if 0<=ni<H and 0<=nj<W:
            yield ni,nj

ans=-1
hp=[(0,0,si,sj)]
dist=[[1000]*W for _ in range(H)]
dist[si][sj]=0
while hp:
    k,w,i,j=heappop(hp)
    if i==ti and j==tj:
        ans=k
        break
    if w==1:
        x,y=k,0
        for ni,nj in nbs8(i,j):
            if A[ni][nj]=='#':
                continue
            if x>=dist[ni][nj]:
                continue
            dist[ni][nj]=x
            heappush(hp,(x,y,ni,nj))
    else:
        for ni,nj in nbs4(i,j):
            x,y=k,0
            if A[ni][nj]=='#':
                x=k+1
                y=1
            if x>=dist[ni][nj]:
                continue
            dist[ni][nj]=x
            heappush(hp,(x,y,ni,nj))

#pprint(dist)

print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import *
from pprint import pprint

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=[[*input()] for _ in range(H)]

def nbs4(i,j):
    for ni,nj in [
                (i-1,j),
        (i,j-1),        (i,j+1),
                (i+1,j),
    ]:
        if 0<=ni<H and 0<=nj<W:
            yield ni,nj

def nbs8(i,j):
    for ni,nj in [
        (i-1,j-1),(i-1,j),(i-1,j+1),
        (  i,j-1),        (  i,j+1),
        (i+1,j-1),(i+1,j),(i+1,j+1),
    ]:
        if 0<=ni<H and 0<=nj<W:
            yield ni,nj

ans=-1
hp=[(0,0,si,sj)]
dist=[[1000]*W for _ in range(H)]
dist[si][sj]=0
while hp:
    k,w,i,j=heappop(hp)
    if i==ti and j==tj:
        ans=k
        break
    x,y=k,0
    if w==1:
        for ni,nj in nbs8(i,j):
            if A[ni][nj]=='#':
                continue
            if x>=dist[ni][nj]:
                continue
            dist[ni][nj]=x
            heappush(hp,(x,y,ni,nj))
    else:
        for ni,nj in nbs4(i,j):
            if A[ni][nj]=='#':
                if w==1:
                    continue
                x=k+1
                y=1
            if x>=dist[ni][nj]:
                continue
            dist[ni][nj]=x
            heappush(hp,(x,y,ni,nj))

#pprint(dist)

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
for h in range(H):
    S[h] = [1 if c=='.' else 0 for c in input()]
#print( H,W,s,e,S,sep='\n')
walk=[[] for _ in range(H*W)]
warp=[[] for _ in range(H*W)]
#print(walk,warp,sep='\n')
for i in range(H):
    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(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")
        while len(waiting_warp):
            to=waiting_warp.popleft()
            #print(to)
            if done[to]==0:
                waiting_walk.append(to)
        warping=0
        cnt+=1
    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)

Submission status: Wrong Answer

Solution code:
from collections import deque

def idx(h,w):
    return h*W + w

def main():
    graph=[set() for _ in range(N)]
    for h in range(H):
        for w in range(W):
            idx_from=idx(h,w)
            if Ss[h][w]==0:
                continue
            adj=[(-1,0), (0,1), (1,0), (0,-1)]
            for dh in [-2,-1, 0, 1, 2]:
                for dw in [-2, -1, 0, 1, 2]:
                    h_dst=h+dh
                    w_dst=w+dw
                    if (0<=h_dst<H) and (0<=w_dst<W) and (dh!=0 or dw!=0) and (Ss[h_dst][w_dst]==1):
                        idx_to=idx(h_dst,w_dst)
                        if (dh,dw) in adj:
                            graph[idx_from].add((idx_to,0))
                            graph[idx_to].add((idx_from,0))
                        else:
                            graph[idx_from].add((idx_to,1))
                            graph[idx_to].add((idx_from,1))
    print(-1)
    exit()
    s=idx(Ch,Cw)
    dist=[10**9]*N
    dist[s]=0
    que=deque()
    que.append(s)

    while len(que)>0:
        i = que.popleft() # 先頭取り出し
        for j,c in graph[i]:
            d = dist[i] + c
            if d < dist[j]:
                dist[j] = d
                if c == 1:
                    que.append(j) # 末尾へ
                else:
                    que.appendleft(j) #先頭へ
    #print(graph,dist)
    ans = dist[idx(Dh,Dw)]
    print(-1 if ans == 10**9 else ans)

H,W=map(int,input().split())
N=H*W
Ch,Cw=[int(e)-1 for e in input().split()]
Dh,Dw=[int(e)-1 for e in input().split()]
Ss=[]
for _ in range(H):
    Ss.append([(1 if c=='.'else 0) for c in input()])
main()

Submission status: Wrong Answer

Solution code:
import math
import collections
import fractions
import itertools
import functools
import operator
import bisect

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())
maze = [list(input()) for _ in range(h)]
visited = [[-1]*w for _ in range(h)]

# コスト0で移動できるマスを列挙
def bfs_walk(sy, sx):
    global maze, visited
    d = collections.deque([[sy, sx]])
    rect = [[sy, sx]]
    visited = [[-1]*w for _ in range(h)]
    visited[sy][sx] = 0
    while d:
        y, x = d.popleft()
        for i, j in ([(0,1),(0,-1),(1,0),(-1,0)]):
            newy, newx = y+i, x+j
            if newy < 0 or newx < 0:
                continue
            elif newy >= h or newx >= w:
                continue
            elif visited[newy][newx] == -1:
                if maze[newy][newx] == ".":
                    visited[newy][newx] = visited[y][x]+1
                    d.append([newy, newx])
                    rect.append([newy, newx])
    return rect

# 魔法で移動できるマスを列挙
def bfs_magic(sy, sx):
    global maze, visited
    d = collections.deque([[sy, sx]])
    rect = [[sy, sx]]
    visited[sy][sx] = 0
    while d:
        y, x = d.popleft()
        for i in range(-2, 3):
            for j in range(-2, 3):
                newy, newx = y+i, x+j
                if newy < 0 or newx < 0:
                    continue
                elif newy >= h or newx >= w:
                    continue
                elif visited[newy][newx] == -1:
                    if maze[newy][newx] == ".":
                        visited[newy][newx] = visited[y][x]+1
                        d.append([newy, newx])
                        rect.append([newy, newx])
    return rect

def solve():
    cost = 0
    sy, sx = ch, cw
    use_walk = bfs_walk(sy, sx)
    flag = False
    while True:
        if [dh, dw] in use_walk:
            print(cost)
            break
        cost += 1
        for i in use_walk:
            use_magic = bfs_magic(i[0], i[1])
            if [dh, dw] in use_magic:
                print(cost)
                flag = True
                break
        use_walk = bfs_walk(sy, sx)
        if flag:
            break
    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import queue
h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())
graphlist = []
grapht = [[-1 for i in range(w+2)] for i in range(h+2)]
meiro = [["#" for i in range(w+2)]]+[["#"]+list(input())+["#"] for i in range(h)]+[["#" for i in range(w+2)]]
lastnum=0
for i in range(1, h+1):
    for j in range(1, w+1):
        if(meiro[i][j] == "."):
            if(grapht[i-1][j] != -1):
                grapht[i][j] = grapht[i-1][j]
            elif(grapht[i][j-1] != -1):
                grapht[i][j] = grapht[i][j-1]
            elif(grapht[i+1][j] != -1):
                grapht[i][j] = grapht[i+1][j]
            elif(grapht[i][j+1] != -1):
                grapht[i][j] = grapht[i][j+1]
            else:
                grapht[i][j] = lastnum
                graphlist.append(set())
                lastnum +=1
            if(meiro[i-1][j] == "."):
                grapht[i-1][j] = grapht[i][j]
            if(meiro[i][j-1] == "."):
                grapht[i][j-1] = grapht[i][j]
            if(meiro[i+1][j] == "."):
                grapht[i+1][j] = grapht[i][j]
            if(meiro[i][j+1] == "."):
                grapht[i][j+1] = grapht[i][j]                
            for k in range(i-2, i+3):
                if(k >= h+1):
                    continue
                for l in range(j-2, j+3):
                    if(l >= w+1):
                        continue
                    if(grapht[k][l] != -1 and grapht[k][l] != grapht[i][j]):
                        graphlist[grapht[i][j]].add(grapht[k][l])
                        graphlist[grapht[k][l]].add(grapht[i][j])
start = grapht[ch][cw]
end = grapht[dh][dw]
donelist = set()
nextlist = queue.Queue()
depthlist = [-1]*len(graphlist)
donelist.add(start)
depthlist[start] = 0
#print(grapht)
while True:
    for i in graphlist[start]:
        if(i not in donelist):
            nextlist.put(i)
            depthlist[i] = depthlist[start]+1
            donelist.add(i)
    if(nextlist.empty()):
        break
    start = nextlist.get()
        
#print(graphlist)
print(depthlist[end])

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

S=[]
for i in range(H):
  s=list(input())
  S.append([10**10 if i =="." else -1 for i in s])

dq = deque()
dq.append((Ch,Cw))
S[Ch][Cw]=0

while dq:
  a=dq.pop()
  for i in range(5):
    for j in range(5):
      if a[0]-2+i>=0 and a[0]-2+i<H and a[1]-2+j>=0 and a[1]-2+j<W:
        if abs(i-2)+abs(j-2)==1 and S[a[0]][a[1]]<S[a[0]-2+i][a[1]-2+j]:
          S[a[0]-2+i][a[1]-2+j]=S[a[0]][a[1]]
          dq.appendleft((a[0]-2+i,a[1]-2+j))
        elif S[a[0]][a[1]]+1<S[a[0]-2+i][a[1]-2+j]:
          S[a[0]-2+i][a[1]-2+j]=S[a[0]][a[1]]+1
          dq.append((a[0]-2+i,a[1]-2+j))
      

if S[Dh][Dw]==10^10:
  print(-1)
else:
  print(S[Dh][Dw])

Submission status: Wrong Answer

Solution code:
from collections import deque
def main():
    movey = (0, -1, 0, 1)
    movex = (1, 0, -1, 0)
    warpy = (-2, -2, -2, -2, -2, -1, -1, -1, -1,  0, 0,  1,  1, 1, 1, 2, 2, 2, 2, 2)
    warpx = (-2, -1,  0,  1,  2, -2, -1,  1,  2, -2, 2, -2, -1, 1, 2, -2, -1, 0, 1, 2)

    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
    dummy = tuple(['#'] * (w + 4))
    maze = {e: [] for e in range(h + 4)}
    maze[0] = dummy
    maze[1] = dummy
    for i in range(h):
        row = ['#'] + ['#'] + list(input()) + ['#'] + ['#']
        maze[i+2] = row
    maze[h+2] = dummy
    maze[h+3] = dummy
    maze[ch][cw] = 0
    maze[dh][dw] = 'g'

    nodes = deque()
    nodes.append((ch, cw))
    res = []
    while nodes:
        (n_y, n_x) = nodes.popleft()
        cur_masu = maze[n_y][n_x]
        flag = False
        for j1 in range(4):
            ny = n_y + movey[j1]
            nx = n_x + movex[j1]
            next_masu = maze[ny][nx]
            if next_masu == 'g':
                res.append(cur_masu)
            elif next_masu == '.':
                maze[ny][nx] = cur_masu
                nodes.appendleft((ny, nx))
            elif next_masu == '#':
                flag = True
            elif next_masu > cur_masu:
                maze[ny][nx] = cur_masu
        if flag:
            for j1 in range(20):
                ny = n_y + warpy[j1]
                nx = n_x + warpx[j1]
                next_masu = maze[ny][nx]
                if next_masu == 'g':
                    res.append(cur_masu+1)
                elif next_masu == '.':
                    maze[ny][nx] = cur_masu + 1
                    nodes.append((ny, nx))
                elif next_masu == '#':
                    pass
                elif next_masu > cur_masu + 1:
                    maze[ny][nx] =cur_masu + 1
                    nodes.append((ny, nx))

    if res:
        print(min(res))
    else:
        print(-1)

if __name__ == '__main__':
    main()

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
for h in range(H):
    S[h] = [1 if c=='.' else 0 for c in input()]
#print( H,W,s,e,S,sep='\n')
walk=[[] for _ in range(H*W)]
warp=[[] for _ in range(H*W)]
#print(walk,warp,sep='\n')
for i in range(H):
    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(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")
        while len(waiting_warp):
            to=waiting_warp.popleft()
            #print(to)
            if done[to]==0:
                waiting_walk.append(to)
        warping=0
        cnt+=1
    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]:
                waiting_warp.append(n)
        if len(waiting_walk)==0:warping=1
if found:print(cnt)
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

S=[]
for i in range(H):
  s=list(input())
  S.append([10^10 if i =="." else -1 for i in s])

dq = deque()
dq.append((Ch,Cw))
S[Ch][Cw]=0

while dq:
  a=dq.pop()
  for i in range(5):
    for j in range(5):
      if a[0]-2+i>=0 and a[0]-2+i<H and a[1]-2+j>=0 and a[1]-2+j<W:
        if abs(i-2)+abs(j-2)==1 and S[a[0]][a[1]]<S[a[0]-2+i][a[1]-2+j]:
          S[a[0]-2+i][a[1]-2+j]=S[a[0]][a[1]]
          dq.appendleft((a[0]-2+i,a[1]-2+j))
        elif S[a[0]][a[1]]+1<S[a[0]-2+i][a[1]-2+j]:
          S[a[0]-2+i][a[1]-2+j]=S[a[0]][a[1]]+1
          dq.append((a[0]-2+i,a[1]-2+j))

if S[Dh][Dw]==10^10:
  print(-1)
else:
  print(S[Dh][Dw])

Submission status: Wrong Answer

Solution code:
from collections import deque
def main():
    movey = (0, -1, 0, 1)
    movex = (1, 0, -1, 0)
    warpy = (-2, -2, -2, -2, -2, -1, -1, -1, -1,  0, 0,  1,  1, 1, 1, 2, 2, 2, 2, 2)
    warpx = (-2, -1,  0,  1,  2, -2, -1,  1,  2, -2, 2, -2, -1, 1, 2, -2, -1, 0, 1, 2)

    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
    dummy = tuple(['#'] * (w + 4))
    maze = {e: [] for e in range(h + 4)}
    maze[0] = dummy
    maze[1] = dummy
    for i in range(h):
        row = ['#'] + ['#'] + list(input()) + ['#'] + ['#']
        maze[i+2] = row
    maze[h+2] = dummy
    maze[h+3] = dummy
    #maze[ch][cw] = 0
    maze[dh][dw] = 'g'

    next_nodes = deque()
    next_nodes.append((0, ch, cw))
    res = []
    while next_nodes:
        (d, nextny, nextnx) = next_nodes.popleft()
        flag = False
        for j1 in range(4):
            ny = nextny + movey[j1]
            nx = nextnx + movex[j1]
            if maze[ny][nx] == 'g':
                res.append(d)
            elif maze[ny][nx] == '.':
                maze[ny][nx] = '+'
                if (d+1, ny, nx) in next_nodes:
                    next_nodes.remove((d+1, ny, nx))
                if (d, ny, nx) not in next_nodes:
                    next_nodes.appendleft((d, ny, nx))
            elif maze[ny][nx] == '#':
                flag = True
        if flag:
            for j1 in range(20):
                ny = nextny + warpy[j1]
                nx = nextnx + warpx[j1]
                nextg = maze[ny][nx]
                if maze[ny][nx] == 'g':
                    res.append(d+1)
                elif nextg == '.':
                    maze[ny][nx] = '+'
                    if (d+1, ny, nx) not in next_nodes and (d, ny, nx) not in next_nodes :
                        next_nodes.append((d+1, ny, nx))
    if res:
        print(min(res))
    else:
        print(-1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import queue
h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())
graphlist = []
grapht = [[-1 for i in range(w+2)] for i in range(h+2)]
meiro = [["#" for i in range(w+2)]]+[["#"]+list(input())+["#"] for i in range(h)]+[["#" for i in range(w+2)]]
lastnum=0
for i in range(1, h+1):
    for j in range(1, w+1):
        if(meiro[i][j] == "."):
            if(grapht[i-1][j] != -1):
                grapht[i][j] = grapht[i-1][j]
            elif(grapht[i][j-1] != -1):
                grapht[i][j] = grapht[i][j-1]
            elif(grapht[i+1][j] != -1):
                grapht[i][j] = grapht[i+1][j]
            elif(grapht[i][j+1] != -1):
                grapht[i][j] = grapht[i][j+1]
            else:
                grapht[i][j] = lastnum
                graphlist.append(set())
                lastnum +=1
            if(meiro[i+1][j] == "."):
                grapht[i+1][j] = grapht[i][j]
            if(meiro[i][j+1] == "."):
                grapht[i][j+1] = grapht[i][j]                
            for k in range(i-2, i+3):
                if(k >= h+1):
                    continue
                for l in range(j-2, j+3):
                    if(l >= w+1):
                        continue
                    if(grapht[k][l] != -1 and grapht[k][l] != grapht[i][j]):
                        graphlist[grapht[i][j]].add(grapht[k][l])
                        graphlist[grapht[k][l]].add(grapht[i][j])
start = grapht[ch][cw]
end = grapht[dh][dw]
donelist = set()
nextlist = queue.Queue()
depthlist = [-1]*len(graphlist)
donelist.add(start)
depthlist[start] = 0
#print(grapht)
while True:
    for i in graphlist[start]:
        if(i not in donelist):
            nextlist.put(i)
            depthlist[i] = depthlist[start]+1
            donelist.add(i)
    if(nextlist.empty()):
        break
    start = nextlist.get()
        
#print(graphlist)
print(depthlist[end])