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:
h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())

s = [list(input()) for _i in range(h)]

bord = [[float('inf') for _i in range(w)] for _j in range(h)]
bord[ch-1][cw-1] = 0

dh, dw = dh-1, dw-1
from collections import deque
q = deque([[ch-1, cw-1]])
while q:
    x, y = q.pop()
    for i, j in [[1,0], [0, 1], [-1, 0], [0, -1]]:
        if 0 <= x+i and 0 <= y+j and x+i < h and y+j < w:
            if s[x+i][y+j]=='.':
                if bord[x][y] < bord[x+i][y+j]:
                    bord[x+i][y+j] = bord[x][y]
                    q.append([x+i, y+j])
            else:
                for n in [-2, -1, 1, 2]:
                    for m in [-2, -1, 1, 2]:
                        if 0 <= x+n and 0 <= y+m and x+n < h and y+m < w:
                            if (s[x+n][y+m] == '.') and (bord[x][y]+1 < bord[x+n][y+m]):
                                bord[x+n][y+m] = bord[x][y] + 1
                                q.append([x+n, y+m])

if bord[dh][dw]==float('inf'):
    print(-1)
else:
    print(bord[dh][dw])

Submission status: Wrong Answer

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

s = [input() for _i in range(h)]

bord = [[float('inf') for _i in range(w)] for _j in range(h)]
bord[ch-1][cw-1] = 0

dh, dw = dh-1, dw-1
from collections import deque
q = deque([[ch-1, cw-1]])
while q:
    x, y = q.pop()
    for i, j in [[1,0], [0, 1], [-1, 0], [0, -1]]:
        if 0 <= x+i and 0 <= y+j and x+i < h and y+j < w:
            if s[x+i][y+j]=='.':
                if bord[x][y] < bord[x+i][y+j]:
                    bord[x+i][y+j] = bord[x][y]
                    q.append([x+i, y+j])
            else:
                for n in [-2, -1, 1, 2]:
                    for m in [-2, -1, 1, 2]:
                        if 0 <= x+n and 0 <= y+m and x+n < h and y+m < w:
                            if (s[x+n][y+m] == '.') and (bord[x][y]+1 < bord[x+n][y+m]):
                                bord[x+n][y+m] = bord[x][y] + 1
                                q.append([x+n, y+m])

if bord[dh][dw]==float('inf'):
    print(-1)
else:
    print(bord[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

s = []
for i in range(h):
  t = input()
  s.append(t)

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

queue = deque([[ch,cw]])
visited = [[False for i in range(w)] for j in range(h)]
magic = [[-1 for i in range(w)] for j in range(h)]
visited[ch][cw] = True
magic[ch][cw] = 0
while queue:
  l,m = queue.popleft()
  for k in range(4):
    x = l + dx[k]
    y = m + dy[k]
    if x < 0 or y < 0 or x >= h or y >= w or visited[x][y] or s[x][y] == "#":
      continue
    #visited[x][y] = visited[l][m] + 1
    visited[x][y] = True
    magic[x][y] = magic[l][m]
    queue.append([x,y])
  if len(queue) == 0:
    for i in range(-2,3):
      for j in range(-2,3):
        if (i== 1 and j == 0)or(i==-1 and j == 0)or(i==0 and j == 1)or(i==0 and j==-1)or(i==0 and j==0):
          continue
        x = l + i
        y = m + j
        if x < 0 or y < 0 or x >= h or y >= w or visited[x][y] or s[x][y] == "#":
          continue
        visited[x][y] = True
        magic[x][y] = magic[l][m] + 1
        queue.append([x,y])
print (magic[dh][dw])

Submission status: Wrong Answer

Solution code:
H,W = map(int, input().split())
Ch,Cw = map(int, input().split())
Dh,Dw = map(int, input().split())
maze = []
vis=[]
for h in range(H):
  maze.append(list(input()))
  vis.append([-1]*W)
vis[Ch-1][Cw-1] = 0

def check(start):
  stack = [start]
  while stack:
    p = stack.pop()
    r = [-2,3,-2,3]
    if p==[Dh-1,Dw-1]:
      print(vis[p[0]][p[1]])
      return True
    elif maze[p[0]][p[1]]=="#":
      vis[p[0]][p[1]] = -1
      continue
    
    if p[0]<2:
      r[0] = -p[0]
    if p[0]>H-3:
      r[1] = H-1 -p[0]
    if p[1]<2:
      r[2] = -p[1]
    if p[1]>W-3:
      r[3] = W -1-p[1] 
    
    for i in range(r[0],r[1]):
      for j in range(r[2],r[3]):
        if maze[p[0]+i][p[1]+j]=="#":
          continue
        elif i*i+j*j==0 or [p[0]+i,p[1]+j] in stack:
          continue
          
        if i*i+j*j == 1: #上下左右が
          if vis[p[0]+i][p[1]+j] == -1:
            vis[p[0]+i][p[1]+j] = 0
            stack.append([p[0]+i,p[1]+j])
          if vis[p[0]+i][p[1]+j] > vis[p[0]][p[1]]: #自分より大きい時
            vis[p[0]+i][p[1]+j] = vis[p[0]][p[1]]
            stack.append([p[0]+i,p[1]+j])
        elif i*i+j*j>1:
          if vis[p[0]+i][p[1]+j] == -1:
            vis[p[0]+i][p[1]+j] = vis[p[0]][p[1]] + 1
            stack.append([p[0]+i,p[1]+j])
  return False

check([Ch-1,Cw-1])

Submission status: Wrong Answer

Solution code:
from collections import deque
H,W = map(int,input().split())
ch,cw = map(int,input().split())
ch -= 1
cw -= 1
dh,dw = map(int,input().split())
dh -= 1
dw -= 1
S = []
for _ in range(H):
    S.append(list(input()))
q0 = deque([(ch,cw,0)])
q1 = deque()
while True:
    if not q0 and not q1:
        print(-1)
        exit(0)
    elif not q0:
        q0 = q1
        q1 = deque()
        continue
    x,y,c = q0.popleft()
    fl = False
    for nx,ny in [(x-1,y),(x,y-1),(x+1,y),(x,y+1)]:
        if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == ".":
            if nx == dh and ny == dw:
                print(c)
                exit(0)
            S[nx][ny] = "#"
            q0.append((nx,ny,c))
            fl = True
    if fl:
        continue
    for nx, ny in [(x-2+i, y-2+j) for j in range(5) for i in range(5)]:
        if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == ".":
            if nx == dh and ny == dw:
                print(c+1)
                exit(0)
            S[nx][ny] = "#"
            q1.append((nx,ny,c+1))

Submission status: Wrong Answer

Solution code:
# input here
_INPUT = """\
4 5
1 2
2 5
#.###
####.
#..##
#..##
"""

def main():
    H, W = map(int, input().split())
    c = tuple(map(int, input().split()))
    d = tuple(map(int, input().split()))
    d_y = d[0]-1
    d_x = d[1]-1
    s = [list(input()) for i in range(H)]

    visited = [[-1]*W for j in range(H)]
    visited[c[0]-1][c[1]-1] = 0

    main_q = deque()
    main_q.append((c[0]-1,c[1]-1))
    magic_q = deque()
    direction = [(0,1),(1,0),(-1,0),(0,-1)]

    #魔法使わないbfs
    while main_q:
        y, x = main_q.popleft()
        magic_q.append((y,x))

        for i in range(4):
            moved_y = y + direction[i][0]
            moved_x = x + direction[i][1]

            if moved_y < 0 or H -1 < moved_y or moved_x < 0 or W -1 < moved_x:
                continue
            if s[moved_y][moved_x] == '#':
                continue
            if s[moved_y][moved_x] == '.' and visited[moved_y][moved_x] == -1:
                main_q.append((moved_y, moved_x))
                visited[moved_y][moved_x] = visited[y][x]

        if not main_q:
            while magic_q:
                y, x = magic_q.popleft()

                for k in range(-2,3):
                    for l in range(-2,3):
                        moved_y = y + k
                        moved_x = x + l

                        if moved_y < 0 or H -1 < moved_y or moved_x < 0 or W -1 < moved_x:
                            continue
                        if s[moved_y][moved_x] == '#':
                            continue
                        if s[moved_y][moved_x] == '.' and visited[moved_y][moved_x] == -1:
                            main_q.append((moved_y, moved_x))
                            visited[moved_y][moved_x] = visited[y][x] + 1
    print(visited)
    print(visited[d_y][d_x])


if __name__ == '__main__':
    import io
    import sys
    import math
    import itertools
    from collections import deque

    # sys.stdin = io.StringIO(_INPUT)
    main()

Submission status: Wrong Answer

Solution code:
# input here
_INPUT = """\
4 4
2 2
3 3
....
....
....
....
"""

def main():
    H, W = map(int, input().split())
    c = tuple(map(int, input().split()))
    d = tuple(map(int, input().split()))
    d_y = d[0]-1
    d_x = d[1]-1
    s = [list(input()) for i in range(H)]

    visited = [[-1]*W for j in range(H)]
    visited[c[0]-1][c[1]-1] = 0

    main_q = deque()
    main_q.append((c[0]-1,c[1]-1))
    magic_q = deque()
    direction = [(0,1),(1,0),(-1,0),(0,-1)]

    #魔法使わないbfs
    while main_q:
        y, x = main_q.popleft()
        magic_q.append((y,x))

        for i in range(4):
            moved_y = y + direction[i][0]
            moved_x = x + direction[i][1]

            if moved_y < 0 or H -1 < moved_y or moved_x < 0 or W -1 < moved_x:
                continue
            if s[moved_y][moved_x] == '#':
                continue
            if s[moved_y][moved_x] == '.' and visited[moved_y][moved_x] == -1:
                main_q.append((moved_y, moved_x))
                visited[moved_y][moved_x] = visited[y][x]

        if not main_q:
            while magic_q:
                y, x = magic_q.popleft()

                for k in range(-2,3):
                    for l in range(-2,3):
                        moved_y = y + k
                        moved_x = x + l

                if moved_y < 0 or H -1 < moved_y or moved_x < 0 or W -1 < moved_x:
                    continue
                if s[moved_y][moved_x] == '#':
                    continue
                if s[moved_y][moved_x] == '.' and visited[moved_y][moved_x] == -1:
                    main_q.append((moved_y, moved_x))
                    visited[moved_y][moved_x] = visited[y][x] + 1

    print(visited[d_y][d_x])


if __name__ == '__main__':
    import io
    import sys
    import math
    import itertools
    from collections import deque

    # sys.stdin = io.StringIO(_INPUT)
    main()

Submission status: Wrong Answer

Solution code:
from heapq import heapify,heappush,heappop

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 i in range(H)]
cnt = [[10**10 for i in range(W)] for j in range(H)]

task_list = []
heapify(task_list)
heappush(task_list,[0,sx,sy])
S[sy][sx]="."

while len(task_list)>0:
    t,x,y = heappop(task_list)
    if x==gx and y==gy:
        print(t)
        exit()
    
    if x<0 or W<=x or y<0 or H<=y:
        continue

    if S[y][x] != ".":
        continue

    S[y][x]=str(t)#確定最低値
    cnt[y][x]=t#暫定最低値
    heappush(task_list,[t,x+1,y])
    heappush(task_list,[t,x-1,y])
    heappush(task_list,[t,x,y+1])
    heappush(task_list,[t,x,y-1])
    for i in range(-2,3):
        for j in range(-2,3):
            if abs(i)+abs(j) < 2:
                continue
            if x+i<0 or W<=x+i or y+j<0 or H<=y+j:
                continue
            if S[y+j][x+i]=="." and cnt[y+j][x+i] <= t+1:
                heappush(task_list,[t+1,x+i,y+j])
                cnt[y+j][x+i]=t+1

print(-1)

Submission status: Wrong Answer

Solution code:
from heapq import heapify,heappush,heappop

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 i in range(H)]
cnt = [[10**10 for i in range(W)] for j in range(H)]

task_list = []
heapify(task_list)
heappush(task_list,[0,sx,sy])
S[sy][sx]="."

while len(task_list)>0:
    t,x,y = heappop(task_list)
    if x==gx and y==gy:
        print(t)
        exit()
    
    if x<0 or W<=x or y<0 or H<=y:
        continue

    if S[y][x] != ".":
        continue

    S[y][x]=str(t)#確定最低値
    cnt[y][x]=t#暫定最低値
    heappush(task_list,[t,x+1,y])
    heappush(task_list,[t,x-1,y])
    heappush(task_list,[t,x,y+1])
    heappush(task_list,[t,x,y-1])
    for i in range(-2,3):
        for j in range(-2,3):
            if abs(i)+abs(j) < 2:
                continue
            if x+i<0 or W<=x+i or y+j<0 or H<=y+j:
                continue
            if S[y+j][x+i]=="." and cnt[y+j][x+i] <= t:
                heappush(task_list,[t+1,x+i,y+j])
                cnt[y+j][x+i]=t+1

print(-1)

Submission status: Wrong Answer

Solution code:
import sys
import pprint
h,w = map(int,input().split())
sh,sw = map(int,input().split())
gh,gw = map(int,input().split())
C = [list(input()) for _ in range(h)]
dist = [[-1] * w for _ in range(h)] 
sh-=1
sw-=1
gh-=1
gw-=1

move = [[-1, 0], [1, 0], [0, -1], [0, 1]]
magic = [[-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]]

def solve(s_pos):
    queue = []
    queue.append(s_pos)
    dist[s_pos[0]][s_pos[1]] = 0

    candidates = []
    while len(queue) > 0:
        y, x = queue.pop(0)
        candidates.append([y,x])
        for p, q in move:
            vh = y + p
            vw = x + q
            if 0 <= vh < h and 0 <= vw < w:
                if C[vh][vw] != "#" and dist[vh][vw] == -1:
                    dist[vh][vw] = dist[y][x]
                    queue.append([vh,vw])
        if len(queue) == 0:
            while len(candidates) > 0:
                y2, x2 = candidates.pop(0)
                for r in range(-2,3):
                    if r == 0:
                        continue
                    for s in range(-2,3):
                        if s == 0:
                            continue
                        vh2 = y2 + r
                        vw2 = x2 + s
                        if 0 <= vh2 < h and 0 <= vw2 < w:
                            nd = dist[y2][x2] + 1
                            if C[vh2][vw2] != "#" and dist[vh2][vw2] == -1:
                                dist[vh2][vw2] = nd
                                queue.append([vh2, vw2])

solve([sh,sw])
print(dist[gh][gw])

Submission status: Wrong Answer

Solution code:
from heapq import heapify,heappush,heappop

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 i in range(H)]

task_list = []
heapify(task_list)
heappush(task_list,[0,sx,sy])
S[sy][sx]="."

while len(task_list)>0:
    t,x,y = heappop(task_list)
    if x==gx and y==gy:
        print(t)
        exit()
    
    if x<0 or W<=x or y<0 or H<=y:
        continue

    if S[y][x] != ".":
        continue

    S[y][x]=str(t)
    heappush(task_list,[t,x+1,y])
    heappush(task_list,[t,x-1,y])
    heappush(task_list,[t,x,y+1])
    heappush(task_list,[t,x,y-1])
    for i in range(-2,3):
        for j in range(-2,3):
            if i+j < 2:
                continue
            heappush(task_list,[t+1,x+i,y+j])
    
print(-1)

Submission status: Wrong Answer

Solution code:
import sys
import pprint
h,w = map(int,input().split())
sh,sw = map(int,input().split())
gh,gw = map(int,input().split())
C = [list(input()) for _ in range(h)]
dist = [[-1] * w for _ in range(h)] 
sh-=1
sw-=1
gh-=1
gw-=1

move = [[-1, 0], [1, 0], [0, -1], [0, 1]]

def solve(s_pos):
    queue = []
    queue.append(s_pos)
    dist[s_pos[0]][s_pos[1]] = 0
    
    candidates = []
    while len(queue) > 0:
        y, x = queue.pop(0)
        candidates.append([y,x])
        for p, q in move:
            vh = y + p
            vw = x + q
            if 0 <= vh < h and 0 <= vw < w:
                if C[vh][vw] != "#" and dist[vh][vw] == -1:
                    dist[vh][vw] = dist[y][x]
                    queue.append([vh,vw])
        if len(queue) == 0:
            while len(candidates) > 0:
                y2, x2 = candidates.pop(0)
                for ii in range(-2,3):
                    if ii == 0:
                        continue
                    for jj in range(-2,3):
                        if jj == 0:
                            continue
                        vh = y2 + ii
                        vw = x2 + jj
                        if 0 <= vh < h and 0 <= vw < w:
                            nd = dist[y2][x2] + 1
                            if C[vh][vw] != "#" and dist[vh][vw] == -1:
                                dist[vh][vw] = nd
                                queue.append([vh, vw])

solve([sh,sw])
print(dist[gh][gw])

Submission status: Wrong Answer

Solution code:
import sys
import pprint
h,w = map(int,input().split())
sh,sw = map(int,input().split())
gh,gw = map(int,input().split())
C = [list(input()) for _ in range(h)]
dist = [[-1] * w for _ in range(h)] 
sh-=1
sw-=1
gh-=1
gw-=1

dy_dx = [[1,0],[0,1],[-1,0],[0,-1]]

def solve(s_pos):
    queue = []
    queue.append(s_pos)
    dist[s_pos[0]][s_pos[1]] = 0
    
    candidates = []
    while len(queue) > 0:
        y, x = queue.pop(0)
        candidates.append([y,x])
        for i in range(4):
            vh = y + dy_dx[i][0]
            vw = x + dy_dx[i][1]
            if 0 <= vh < h and 0 <= vw < w:
                if C[vh][vw] != "#" and dist[vh][vw] == -1:
                    dist[vh][vw] = dist[y][x]
                    queue.append([vh,vw])
        if len(queue) == 0:
            while len(candidates) > 0:
                y2, x2 = candidates.pop(0)
                for ii in range(-2,3):
                    if ii == 0:
                        continue
                    for jj in range(-2,3):
                        if jj == 0:
                            continue
                        vh = y2 + ii
                        vw = x2 + jj
                        if 0 <= vh < h and 0 <= vw < w:
                            nd = dist[y2][x2] + 1
                            if C[vh][vw] != "#" and dist[vh][vw] == -1:
                                dist[vh][vw] = nd
                                queue.append([vh, vw])

solve([sh,sw])
print(dist[gh][gw])

Submission status: Wrong Answer

Solution code:
import sys
import pprint
h,w = map(int,input().split())
sh,sw = map(int,input().split())
gh,gw = map(int,input().split())
C = [list(input()) for _ in range(h)]
dist = [[-1] * w for _ in range(h)] 
sh-=1
sw-=1
gh-=1
gw-=1

dy_dx = [[1,0],[0,1],[-1,0],[0,-1]]

def solve(s_pos):
    queue = []
    queue.append(s_pos)
    dist[s_pos[0]][s_pos[1]] = 0
    
    candidates = []
    while len(queue) > 0:
        y, x = queue.pop(0)
        candidates.append([y,x])
        for i in range(4):
            vh = y + dy_dx[i][0]
            vw = x + dy_dx[i][1]
            if 0 <= vh < h and 0 <= vw < w:
                if C[vh][vw] != "#" and dist[vh][vw] == -1:
                    dist[vh][vw] = dist[y][x]
                    queue.append([vh,vw])
        if len(queue) == 0:
            while len(candidates) > 0:
                y2, x2 = candidates.pop()
                for ii in range(-2,3):
                    if ii == 0:
                        continue
                    for jj in range(-2,3):
                        if jj == 0:
                            continue
                        vh = y2 + ii
                        vw = x2 + jj
                        if 0 <= vh < h and 0 <= vw < w:
                            nd = dist[y2][x2] + 1
                            if C[vh][vw] != "#" and dist[vh][vw] == -1:
                                dist[vh][vw] = nd
                                queue.append([vh, vw])

solve([sh,sw])
print(dist[gh][gw])

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Ch -= 1
Cw -= 1
Dh, Dw = map(int, input().split())
Dh -= 1
Dw -= 1
S = [list(input()) for i in range(H)]
S[Ch][Cw] = "#"

ans = 0
warp_after = [[Ch, Cw]]
warp_before = []

def nowRange(now):
    queue = [[1,0],[-1,0],[0,1],[0,-1]]
    r = []
    for i in queue:
        j = now[0]+i[0]
        k = now[1]+i[1]
        if j >= 0 and j < H and k >= 0 and k < W:
            if j == Dh and k == Dw:
                print(ans)
                exit()
            elif S[j][k] == ".":
                S[j][k] = "#"
                r = r + [[j,k]]
    return r


def stepRange(trout):
    r = []
    for i in trout:
        r = r + nowRange(i)
    if len(r) == 0:
        return []
    else:
        return r + stepRange(r)


def nowWarp(now):
    queue = [
        [-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]
    ]
    r = []
    for i in queue:
        j = now[0]+i[0]
        k = now[1]+i[1]
        if j >= 0 and j < H and k >= 0 and k < W:
            if j == Dh and k == Dw:
                print(ans)
                exit()
            elif S[j][k] == ".":
                S[j][k] = "#"
                r = r + [[j,k]]
    return r


def warpRange(trout):
    r = []
    for i in trout:
        r = r + nowWarp(i)
    if len(r) == 0:
        print(-1)
        exit()
    else:
        return r


for i in range(10**5):
    warp_before = stepRange(warp_after) + warp_after
    ans += 1
    warp_after = warpRange(warp_before)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Ch -= 1
Cw -= 1
Dh, Dw = map(int, input().split())
Dh -= 1
Dw -= 1
S = [list(input()) for i in range(H)]
S[Ch][Cw] = "#"

ans = 0
warp_after = [[Ch, Cw]]
warp_before = []

def nowRange(now):
    queue = [[1,0],[-1,0],[0,1],[0,-1]]
    r = []
    for i in queue:
        j = now[0]+i[0]
        k = now[1]+i[1]
        if j >= 0 and j < H and k >= 0 and k < W:
            if j == Dh and k == Dw:
                print(ans)
                exit()
            elif S[j][k] == ".":
                S[j][k] = "#"
                r.append([j,k])
    return r


def stepRange(trout):
    r = []
    for i in trout:
        r = r + nowRange(i)
    if len(r) == 0:
        return []
    else:
        return r + stepRange(r)


def nowWarp(now):
    queue = [
        [-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]
    ]
    r = []
    for i in queue:
        j = now[0]+i[0]
        k = now[1]+i[1]
        if j >= 0 and j < H and k >= 0 and k < W:
            if j == Dh and k == Dw:
                print(ans)
                exit()
            elif S[j][k] == ".":
                S[j][k] = "#"
                r.append([j,k])
    return r


def warpRange(trout):
    r = []
    for i in trout:
        r = r + nowWarp(i)
    if len(r) == 0:
        print(-1)
        exit()
    else:
        return r


for i in range(10**5):
    warp_before = stepRange(warp_after) + warp_after
    
    ans += 1
    warp_after = warpRange(warp_before)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque


def is_inside(x, y, W, H, G):
    return (x <= H-1 and y <= W-1 and
            x >= 0 and y >= 0 and
            G[x][y] != "#")


def main():
    H, W = map(int, input().split())
    Cx, Cy = map(int, input().split())
    Dx, Dy = map(int, input().split())

    G = [list(input()) for _ in range(H)]

    q = deque()
    q.append((Cx-1, Cy-1, 0))
    score = [[0] * W for _ in range(H)]
    curr_score = 1
    while q:
        g = q.popleft()
        if score[g[0]][g[1]]:
            continue

        if g[2]:
            curr_score += 1

        score[g[0]][g[1]] = curr_score

        for step_x in range(-2, 3):
            for step_y in range(-2, 3):
                x_, y_ = g[0]+step_x, g[1]+step_y
                if is_inside(x_, y_, W, H, G) and not score[x_][y_]:
                    if (max(abs(step_x), abs(step_y)) == 1 and
                            min(abs(step_x), abs(step_y)) == 0):

                        q.appendleft((x_, y_, 0))
                    else:
                        q.append((x_, y_, 1))

    print(score[Dx-1][Dy-1]-1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Ch -= 1
Cw -= 1
Dh, Dw = map(int, input().split())
Dh -= 1
Dw -= 1
S = [list(input()) for i in range(H)]
S[Ch][Cw] = "#"

ans = 0
warp_after = [[Ch, Cw]]
warp_before = []

def nowRange(now):
    queue = [[1,0],[-1,0],[0,1],[0,-1]]
    r = []
    for i in queue:
        j = now[0]+i[0]
        k = now[1]+i[1]
        if j >= 0 and j < H and k >= 0 and k < W:
            if j == Dh and k == Dw:
                print(ans)
                exit()
            elif S[j][k] == ".":
                S[j][k] = "#"
                r.append([j,k])
    return r


def stepRange(trout):
    r = []
    for i in trout:
        r = r + nowRange(i)
    if len(r) == 0:
        return []
    else:
        return r + stepRange(r)


def nowWarp(now):
    queue = [
        [-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]
    ]
    r = []
    for i in queue:
        j = now[0]+i[0]
        k = now[1]+i[1]
        if j >= 0 and j < H and k >= 0 and k < W:
            if j == Dh and k == Dw:
                print(ans)
                exit()
            elif S[j][k] == ".":
                S[j][k] = "#"
                r.append([j,k])
    return r


def warpRange(trout):
    r = []
    for i in trout:
        r = r + nowWarp(i)
    if len(r) == 0:
        return []
    else:
        return r


for i in range(10**4):
    warp_before = stepRange(warp_after) + warp_after
    ans += 1
    warp_after = warpRange(warp_before)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Ch -= 1
Cw -= 1
Dh, Dw = map(int, input().split())
Dh -= 1
Dw -= 1
S = [list(input()) for i in range(H)]
S[Ch][Cw] = "#"

ans = 0
warp_after = [[Ch, Cw]]
warp_before = []

def nowRange(now):
    queue = [[1,0],[-1,0],[0,1],[0,-1]]
    r = []
    for i in queue:
        j = now[0]+i[0]
        k = now[1]+i[1]
        if j >= 0 and j < H and k >= 0 and k < W:
            if j == Dh and k == Dw:
                print(ans)
                exit()
            elif S[j][k] == ".":
                S[j][k] = "#"
                r.append([j,k])
    return r


def stepRange(trout):
    r = []
    for i in trout:
        r = r + nowRange(i)
    if len(r) == 0:
        return []
    else:
        return r + stepRange(r)


def nowWarp(now):
    queue = [
        [-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]
    ]
    r = []
    for i in queue:
        j = now[0]+i[0]
        k = now[1]+i[1]
        if j >= 0 and j < H and k >= 0 and k < W:
            if j == Dh and k == Dw:
                print(ans)
                exit()
            elif S[j][k] == ".":
                S[j][k] = "#"
                r.append([j,k])
    return r


def warpRange(trout):
    r = []
    for i in trout:
        r = r + nowWarp(i)
    if len(r) == 0:
        return []
    else:
        return r


for i in range(10**5):
    warp_before = stepRange(warp_after) + warp_after
    ans += 1
    warp_after = warpRange(warp_before)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys

INF = 10 ** 18
sys.setrecursionlimit(10 ** 6)
from collections import defaultdict


def li():
    return [int(x) for x in input().split()]


from collections import deque

H, W = li()
Ch, Cw = li()
Dh, Dw = li()
S = [list(input()) for x in range(H)]

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
print(S)


def bfs():
    costmap = [[-1] * W for i in range(H)]
    costmap[Ch - 1][Cw - 1] = 0
    que = deque([])
    que.append((Cw - 1, Ch - 1))

    while que:
        # print(que)
        # 左から取り出す
        x, y = que.popleft()
        # 通常移動パターン
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            # バリデーション
            if nx < 0 or nx >= W or ny < 0 or ny >= H:
                continue
            if S[ny][nx] != '.':
                continue
            # 初探索 or コストが下がる場合
            if costmap[ny][nx] == -1 or costmap[y][x] < costmap[ny][nx]:
                # コストゼロは左から
                que.appendleft((nx, ny))
                costmap[ny][nx] = costmap[y][x]
        # 特殊移動パターン
        for i in range(x - 2, x + 3):
            for j in range(y - 2, y + 3):
                nx, ny = i, j
                # バリデーション
                if nx < 0 or nx >= W or ny < 0 or ny >= H:
                    continue
                if S[ny][nx] != '.':
                    continue
                # 初探索 or コストが下がる場合
                if costmap[ny][nx] == -1 or costmap[y][x] + 1 < costmap[ny][nx]:
                    # コスト1は右から
                    que.append((nx, ny))
                    costmap[ny][nx] = costmap[y][x] + 1
    return costmap


costmap = bfs()
# print(costmap)
print(costmap[Dh - 1][Dw - 1])