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: Time Limit Exceeded

Solution code:
dhw = [(1,0),(0,-1),(0,1),(-1,0),
( 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)]

H, W = map(int, input().split())
sh, sw = map(lambda x:int(x)-1, input().split())
eh, ew = map(lambda x:int(x)-1, input().split())
S = [input() for _ in range(H)]

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

from collections import deque
dq = deque([(sh, sw, 0)])
while dq:
    h, w, d = dq.popleft()
    if dst[h][w] >= 0 or S[h][w] == '#': continue
    if dst[h][w] == -1 or d < dst[h][w]: dst[h][w] = d

    for i in range(4):
        nh = h + dhw[i][0]; nw = w + dhw[i][1]
        if nh < 0 or H <= nh : continue
        if nw < 0 or W <= nw : continue
        if S[nh][nw] == '#': continue
        if dst[nh][nw] >= 0: continue
        dq.appendleft((nh, nw, d))

    for i in range(4, 24):
        nh = h + dhw[i][0]; nw = w + dhw[i][1]
        if nh < 0 or H <= nh : continue
        if nw < 0 or W <= nw : continue
        if S[nh][nw] == '#': continue
        if dst[nh][nw] >= 0: continue
        dq.append((nh, nw, d+1))

print(dst[eh][ew])

Submission status: Time Limit Exceeded

Solution code:
dhw = [(1,0),(0,-1),(0,1),(-1,0),
( 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)]

H, W = map(int, input().split())
sh, sw = map(lambda x:int(x)-1, input().split())
eh, ew = map(lambda x:int(x)-1, input().split())
S = [input() for _ in range(H)]

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

from collections import deque
dq = deque([(sh, sw, 0)])
while dq:
    h, w, d = dq.popleft()
    if dst[h][w] == -1 or d < dst[h][w]: dst[h][w] = d
    for i in range(4):
        nh = h + dhw[i][0]; nw = w + dhw[i][1]
        if nh < 0 or H <= nh or nw < 0 or W <= nw: continue
        if S[nh][nw] == '#' or 0 <= dst[nh][nw]: continue
        dq.appendleft((nh, nw, d))
    for i in range(4, 24):
        nh = h + dhw[i][0]; nw = w + dhw[i][1]
        if nh < 0 or H <= nh or nw < 0 or W <= nw: continue
        if S[nh][nw] == '#' or 0 <= dst[nh][nw]: continue
        dq.append((nh, nw, d+1))

print(dst[eh][ew])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())
maze = [[9]*(w+4)]
maze.append([9]*(w+4))
for i in range(h):
  s = input()
  tmp = [9,9]
  for i in s:
    if i == "#":
      tmp.append(9)
    else:
      tmp.append(0)
  tmp.append(9)
  tmp.append(9)
  maze.append(tmp)
maze.append([9]*(w+4))
maze.append([9]*(w+4))
#print(maze)

pos = deque()
pos.append([ch+1,cw+1,0])
while len(pos)>0:
  x, y, hop = pos.popleft()
  #print(pos)
  #print[x,y,hop)
  if (x,y) == (dh+1,dw+1):
    print(hop)
    break
  maze[x][y] = 2
  
  if maze[x-1][y] <2:
    pos.appendleft([x-1,y,hop])
    
  if maze[x+1][y] <2:
    pos.appendleft([x+1,y,hop])
    
  if maze[x][y-1] <2:
    pos.appendleft([x,y-1,hop])
    
  if maze[x][y+1] <2:
    pos.appendleft([x,y+1,hop])


  move =[(-2,0),(-2,1),(-2,2),(-2,-1),(-2,-2),(2,0),(2,1),(2,2),(2,-1),(2,-2),(0,-2),(-1,-2),(1,-2),(0,2),(-1,2),(1,2),(-1,1),(-1,-1),(1,1),(1,-1)]
  for dx,dy in move:
    if maze[x+dx][y+dy] <2:
      if ([x+dx,y+dy,hop+1] not in pos) or ([x-dx,y,hop] not in pos):
        pos.append([x+dx,y+dy,hop+1])
  
else:
  print(-1)

Submission status: Time Limit Exceeded

Solution code:
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード
    #input
    h, w = map(int, input().split())
    c_h, c_w = map(int, input().split())
    d_h, d_w = map(int, input().split())
    s = [input() for _ in range(h)]

    #output
    from collections import deque

    INF = pow(10, 10)
    dp = [[INF] * w for _ in range(h)]

    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    dxb = [-2, -2, -2, -2, -2, -1, -1, -1, -1, 0, 0, 1, 1,  1, 1, 2, 2, 2, 2, 2]
    dyb = [-2, -1, 0,   1,  2, -2, -1,  1,  2,-2, 2, -2,-1, 1, 2, -2,-1,0, 1, 2]

    q = deque([])
    q.append((c_h-1, c_w-1))
    dp[c_h-1][c_w-1] = 0

    while q:
        x, y = q.popleft()

        if x == d_h-1 and y == d_w-1:
            break

        for i in range(0, 4):
            nx, ny = x + dx[i], y + dy[i]
            if 0 <= nx < h and 0 <= ny < w and s[nx][ny] != "#" and dp[nx][ny] > dp[x][y]:
                q.appendleft((nx, ny))#ここを変更
                dp[nx][ny] = dp[x][y]#ここを変更

        for j in range(0, 20):
            nx2, ny2 = x + dxb[j], y + dyb[j]
            if 0 <= nx2 < h and 0 <= ny2 < w and s[nx2][ny2] != "#" and dp[nx2][ny2] > dp[x][y]:
                q.append((nx2, ny2))
                dp[nx2][ny2] = dp[x][y] + 1

    """def clear_maze(sx, sy, gx, gy, maze):
        def bfs():
            q = deque([])
            q.append((sx, sy))
            dp[sx][sy] = 0

            while len(q):
                x, y = q.popleft()

                if x == gx and y == gy:
                    break

                for i in range(0, 4):
                    nx, ny = x + dx[i], y + dy[i]
                    if 0 <= nx < h and 0 <= ny < w and maze[nx][ny] != "#" and dp[nx][ny] > dp[x][y]:
                        q.appendleft((nx, ny))#ここを変更
                        dp[nx][ny] = dp[x][y]#ここを変更

                for j in range(0, 20):
                    nx2, ny2 = x + dxb[j], y + dyb[j]
                    if 0 <= nx2 < h and 0 <= ny2 < w and maze[nx2][ny2] != "#" and dp[nx2][ny2] > dp[x][y]:
                        q.append((nx2, ny2))
                        dp[nx2][ny2] = dp[x][y] + 1 
            return dp[gx][gy]
        return bfs()"""

    answer = dp[d_h-1][d_w-1] if dp[d_h-1][d_w-1] < INF else "-1"

    # %%
    print(answer)

    #N = 1のときなどcorner caseを確認！
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード
    #input
    h, w = map(int, input().split())
    c_h, c_w = map(int, input().split())
    d_h, d_w = map(int, input().split())
    s = [input() for _ in range(h)]

    #output
    from collections import deque

    INF = pow(10, 10)
    dp = [[INF] * w for _ in range(h)]

    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    dxb = [-2, -2, -2, -2, -2, -1, -1, -1, -1, 0, 0, 1, 1,  1, 1, 2, 2, 2, 2, 2]
    dyb = [-2, -1, 0,   1,  2, -2, -1,  1,  2,-2, 2, -2,-1, 1, 2, -2,-1,0, 1, 2]

    q = deque([])
    q.append((c_h-1, c_w-1))
    dp[c_h-1][c_w-1] = 0

    while q:
        x, y = q.popleft()

        if x == d_h-1 and y == d_w-1:
            break

        for i in range(0, 4):
            nx, ny = x + dx[i], y + dy[i]
            if 0 <= nx < h and 0 <= ny < w and s[nx][ny] != "#" and dp[nx][ny] > dp[x][y]:
                q.appendleft((nx, ny))#ここを変更
                dp[nx][ny] = dp[x][y]#ここを変更

        for j in range(0, 20):
            nx2, ny2 = x + dxb[j], y + dyb[j]
            if 0 <= nx2 < h and 0 <= ny2 < w and s[nx2][ny2] != "#" and dp[nx2][ny2] > dp[x][y]:
                q.append((nx2, ny2))
                dp[nx2][ny2] = dp[x][y] + 1

    """def clear_maze(sx, sy, gx, gy, maze):
        def bfs():
            q = deque([])
            q.append((sx, sy))
            dp[sx][sy] = 0

            while len(q):
                x, y = q.popleft()

                if x == gx and y == gy:
                    break

                for i in range(0, 4):
                    nx, ny = x + dx[i], y + dy[i]
                    if 0 <= nx < h and 0 <= ny < w and maze[nx][ny] != "#" and dp[nx][ny] > dp[x][y]:
                        q.appendleft((nx, ny))#ここを変更
                        dp[nx][ny] = dp[x][y]#ここを変更

                for j in range(0, 20):
                    nx2, ny2 = x + dxb[j], y + dyb[j]
                    if 0 <= nx2 < h and 0 <= ny2 < w and maze[nx2][ny2] != "#" and dp[nx2][ny2] > dp[x][y]:
                        q.append((nx2, ny2))
                        dp[nx2][ny2] = dp[x][y] + 1 
            return dp[gx][gy]
        return bfs()"""

    answer = dp[d_h-1][d_w-1]

    # %%
    if answer == INF:
        print(-1)
    else:
        print(answer)

    #N = 1のときなどcorner caseを確認！
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード
    #input
    h, w = map(int, input().split())
    c_h, c_w = map(int, input().split())
    d_h, d_w = map(int, input().split())
    s = ["" for _ in range(h)]
    for i in range(h):
        s[i] = str(input())

    #output
    from collections import deque

    INF = pow(10, 10)
    dp = [[INF] * w for _ in range(h)]

    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    dxb = [-2, -2, -2, -2, -2, -1, -1, -1, -1, 0, 0, 1, 1,  1, 1, 2, 2, 2, 2, 2]
    dyb = [-2, -1, 0,   1,  2, -2, -1,  1,  2,-2, 2, -2,-1, 1, 2, -2,-1,0, 1, 2]

    q = deque([])
    q.append((c_h-1, c_w-1))
    dp[c_h-1][c_w-1] = 0

    while q:
        x, y = q.popleft()

        if x == d_h-1 and y == d_w-1:
            break

        for i in range(0, 4):
            nx, ny = x + dx[i], y + dy[i]
            if 0 <= nx < h and 0 <= ny < w and s[nx][ny] != "#" and dp[nx][ny] > dp[x][y]:
                q.appendleft((nx, ny))#ここを変更
                dp[nx][ny] = dp[x][y]#ここを変更

        for j in range(0, 20):
            nx2, ny2 = x + dxb[j], y + dyb[j]
            if 0 <= nx2 < h and 0 <= ny2 < w and s[nx2][ny2] != "#" and dp[nx2][ny2] > dp[x][y]:
                q.append((nx2, ny2))
                dp[nx2][ny2] = dp[x][y] + 1

    """def clear_maze(sx, sy, gx, gy, maze):
        def bfs():
            q = deque([])
            q.append((sx, sy))
            dp[sx][sy] = 0

            while len(q):
                x, y = q.popleft()

                if x == gx and y == gy:
                    break

                for i in range(0, 4):
                    nx, ny = x + dx[i], y + dy[i]
                    if 0 <= nx < h and 0 <= ny < w and maze[nx][ny] != "#" and dp[nx][ny] > dp[x][y]:
                        q.appendleft((nx, ny))#ここを変更
                        dp[nx][ny] = dp[x][y]#ここを変更

                for j in range(0, 20):
                    nx2, ny2 = x + dxb[j], y + dyb[j]
                    if 0 <= nx2 < h and 0 <= ny2 < w and maze[nx2][ny2] != "#" and dp[nx2][ny2] > dp[x][y]:
                        q.append((nx2, ny2))
                        dp[nx2][ny2] = dp[x][y] + 1 
            return dp[gx][gy]
        return bfs()"""

    answer = dp[d_h-1][d_w-1]

    # %%
    if answer == INF:
        print(-1)
    else:
        print(answer)

    #N = 1のときなどcorner caseを確認！
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード
    #input
    h, w = map(int, input().split())
    c_h, c_w = map(int, input().split())
    d_h, d_w = map(int, input().split())
    s = ["" for _ in range(h)]
    for i in range(h):
        s[i] = str(input())

    #output
    from collections import deque

    INF = pow(10, 10)
    dp = [[INF] * w for _ in range(h)]

    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    dxb = [-2, -2, -2, -2, -2, -1, -1, -1, -1, 0, 0, 1, 1,  1, 1, 2, 2, 2, 2, 2]
    dyb = [-2, -1, 0,   1,  2, -2, -1,  1,  2,-2, 2, -2,-1, 1, 2, -2,-1,0, 1, 2]

    q = deque([])
    q.append((c_h-1, c_w-1))
    dp[c_h-1][c_w-1] = 0

    while len(q):
        x, y = q.popleft()

        if x == d_h-1 and y == d_w-1:
            break

        for i in range(0, 4):
            nx, ny = x + dx[i], y + dy[i]
            if 0 <= nx < h and 0 <= ny < w and s[nx][ny] != "#" and dp[nx][ny] > dp[x][y]:
                q.appendleft((nx, ny))#ここを変更
                dp[nx][ny] = dp[x][y]#ここを変更

        for j in range(0, 20):
            nx2, ny2 = x + dxb[j], y + dyb[j]
            if 0 <= nx2 < h and 0 <= ny2 < w and s[nx2][ny2] != "#" and dp[nx2][ny2] > dp[x][y]:
                q.append((nx2, ny2))
                dp[nx2][ny2] = dp[x][y] + 1

    """def clear_maze(sx, sy, gx, gy, maze):
        def bfs():
            q = deque([])
            q.append((sx, sy))
            dp[sx][sy] = 0

            while len(q):
                x, y = q.popleft()

                if x == gx and y == gy:
                    break

                for i in range(0, 4):
                    nx, ny = x + dx[i], y + dy[i]
                    if 0 <= nx < h and 0 <= ny < w and maze[nx][ny] != "#" and dp[nx][ny] > dp[x][y]:
                        q.appendleft((nx, ny))#ここを変更
                        dp[nx][ny] = dp[x][y]#ここを変更

                for j in range(0, 20):
                    nx2, ny2 = x + dxb[j], y + dyb[j]
                    if 0 <= nx2 < h and 0 <= ny2 < w and maze[nx2][ny2] != "#" and dp[nx2][ny2] > dp[x][y]:
                        q.append((nx2, ny2))
                        dp[nx2][ny2] = dp[x][y] + 1 
            return dp[gx][gy]
        return bfs()"""

    answer = dp[d_h-1][d_w-1]

    # %%
    if answer == INF:
        print(-1)
    else:
        print(answer)

    #N = 1のときなどcorner caseを確認！
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())
maze = [[9]*(w+4)]
maze.append([9]*(w+4))
for i in range(h):
  s = input()
  tmp = [9,9]
  for i in s:
    if i == "#":
      tmp.append(9)
    else:
      tmp.append(0)
  tmp.append(9)
  tmp.append(9)
  maze.append(tmp)
maze.append([9]*(w+4))
maze.append([9]*(w+4))
#print(maze)

pos = deque()
pos.append([ch+1,cw+1,0])
while len(pos)>0:
  x, y, hop = pos.popleft()
  #print(pos)
  #print[x,y,hop)
  if (x,y) == (dh+1,dw+1):
    print(hop)
    break
  maze[x][y] = 2
  
  if maze[x-1][y] <2:
    pos.appendleft([x-1,y,hop])
    
  if maze[x+1][y] <2:
    pos.appendleft([x+1,y,hop])
    
  if maze[x][y-1] <2:
    pos.appendleft([x,y-1,hop])
    
  if maze[x][y+1] <2:
    pos.appendleft([x,y+1,hop])
      
  if maze[x-2][y] <2:
    if [x-2, y, hop + 1] not in pos:
    	pos.append([x-2,y,hop+1])
    
  if maze[x-2][y+1] <2:
    if [x-2, y+1, hop + 1] not in pos:
    	pos.append([x-2,y+1,hop+1])
    
  if maze[x-2][y+2] <2:
    if [x-2, y+2, hop + 1] not in pos:
    	pos.append([x-2,y+2,hop+1])
    
  if maze[x-2][y-1] <2:
    if [x-2, y-1, hop + 1] not in pos:
    	pos.append([x-2,y-1,hop+1])
    
  if maze[x-2][y-2] <2:
    if [x-2, y-2, hop + 1] not in pos:
    	pos.append([x-2,y-2,hop+1])
    
  if maze[x+2][y] <2:
    if [x+2, y, hop + 1] not in pos:
    	pos.append([x+2,y,hop+1])
    
  if maze[x+2][y-1] <2:
    if [x+2, y-1, hop + 1] not in pos:
    	pos.append([x+2,y-1,hop+1])
    
  if maze[x+2][y-2] <2:
    if [x+2, y-2, hop + 1] not in pos:
    	pos.append([x+2,y-2,hop+1])
    
  if maze[x+2][y+1] <2:
    if [x+2, y+1, hop + 1] not in pos:
    	pos.append([x+2,y+1,hop+1])
    
  if maze[x+2][y+2] <2:
    if [x+2, y+2, hop + 1] not in pos:
    	pos.append([x+2,y+2,hop+1])
    
  if maze[x][y-2] <2:
    if [x, y-2, hop + 1] not in pos:
    	pos.append([x,y-2,hop+1])
    
  if maze[x+1][y-2] <2:
    if [x+1, y-2, hop + 1] not in pos:
    	pos.append([x+1,y-2,hop+1])
    
  if maze[x-1][y-2] <2:
    if [x-1, y-2, hop + 1] not in pos:
    	pos.append([x-1,y-2,hop+1])
    
  if maze[x][y+2] <2:
    if [x, y+2, hop + 1] not in pos:
    	pos.append([x,y+2,hop+1])
    
  if maze[x+1][y+2] <2:
    if [x+1, y+2, hop + 1] not in pos:
    	pos.append([x+1,y+2,hop+1])
    
  if maze[x-1][y+2] <2:
    if [x-1, y+2, hop + 1] not in pos:
    	pos.append([x-1,y+2,hop+1])
    
  if maze[x-1][y-1] <2:
    if [x-1, y-1, hop + 1] not in pos:
    	pos.append([x-1,y-1,hop+1])
    
  if maze[x-1][y+1] <2:
    if [x-1, y+1, hop + 1] not in pos:
    	pos.append([x-1,y+1,hop+1])
    
  if maze[x+1][y-1] <2:
    if [x+1, y-1, hop + 1] not in pos:
      pos.append([x+1,y-1,hop+1])
    
  if maze[x+1][y+1] <2:
    if [x+1, y+1, hop + 1] not in pos:
      pos.append([x+1,y+1,hop+1])  
    
else:
  print(-1)

Submission status: Time Limit Exceeded

Solution code:
dhw = [(1,0),(0,-1),(0,1),(-1,0),
( 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)]

H, W = map(int, input().split())
sh, sw = map(lambda x:int(x)-1, input().split())
eh, ew = map(lambda x:int(x)-1, input().split())
S = [input() for _ in range(H)]

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

from collections import deque
dq = deque([(sh, sw, 0)])
while dq:
    h, w, d = dq.popleft()
    if dst[h][w] >= 0 or S[h][w] == '#': continue
    dst[h][w] = d

    for i in range(4):
        nh = h + dhw[i][0]; nw = w + dhw[i][1]
        if nh < 0 or H <= nh : continue
        if nw < 0 or W <= nw : continue
        if S[nh][nw] == '#': continue
        if dst[nh][nw] >= 0: continue
        dq.appendleft((nh, nw, d))

    for i in range(4, 24):
        nh = h + dhw[i][0]; nw = w + dhw[i][1]
        if nh < 0 or H <= nh : continue
        if nw < 0 or W <= nw : continue
        if S[nh][nw] == '#': continue
        if dst[nh][nw] >= 0: continue
        dq.append((nh, nw, d+1))

print(dst[eh][ew])

Submission status: Time Limit Exceeded

Solution code:
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード
    #input
    h, w = map(int, input().split())
    c_h, c_w = map(int, input().split())
    d_h, d_w = map(int, input().split())
    s = ["" for _ in range(h)]
    for i in range(h):
        s[i] = str(input())

    #output
    from collections import deque

    INF = pow(10, 10)
    dp = [[INF] * w for _ in range(h)]

    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    dxb = [-2, -2, -2, -2, -2, -1, -1, -1, -1, 0, 0, 1, 1,  1, 1, 2, 2, 2, 2, 2]
    dyb = [-2, -1, 0,   1,  2, -2, -1,  1,  2,-2, 2, -2,-1, 1, 2, -2,-1,0, 1, 2]

    def clear_maze(sx, sy, gx, gy, maze):
        def bfs():
            q = deque([])
            q.append((sx, sy))
            dp[sx][sy] = 0

            while len(q):
                x, y = q.popleft()

                if x == gx and y == gy:
                    break

                for i in range(0, 4):
                    nx, ny = x + dx[i], y + dy[i]
                    if 0 <= nx < h and 0 <= ny < w and maze[nx][ny] != "#" and dp[nx][ny] > dp[x][y]:
                        q.appendleft((nx, ny))#ここを変更
                        dp[nx][ny] = dp[x][y]#ここを変更

                for j in range(0, 20):
                    nx2, ny2 = x + dxb[j], y + dyb[j]
                    if 0 <= nx2 < h and 0 <= ny2 < w and maze[nx2][ny2] != "#" and dp[nx2][ny2] > dp[x][y]:
                        q.append((nx2, ny2))
                        dp[nx2][ny2] = dp[x][y] + 1 
            return dp[gx][gy]
        return bfs()

    answer = clear_maze(c_h-1, c_w-1, d_h-1, d_w-1, s)

    # %%
    if answer == INF:
        print(-1)
    else:
        print(answer)

    #N = 1のときなどcorner caseを確認！
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード
    #input
    h, w = map(int, input().split())
    c_h, c_w = map(int, input().split())
    d_h, d_w = map(int, input().split())
    s = ["" for _ in range(h)]
    for i in range(h):
        s[i] = str(input())

    #output
    from collections import deque

    INF = pow(10, 10)
    dp = [[INF] * w for _ in range(h)]

    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    dxb = [-2, -2, -2, -2, -2, -1, -1, -1, -1, 0, 0, 1, 1,  1, 1, 2, 2, 2, 2, 2]
    dyb = [-2, -1, 0,   1,  2, -2, -1,  1,  2,-2, 2, -2,-1, 1, 2, -2,-1,0, 1, 2]

    def clear_maze(sx, sy, gx, gy, maze):
        def bfs():
            q = deque([])
            q.append((sx, sy))
            dp[sx][sy] = 0

            while len(q):
                x, y = q.popleft()

                if x == gx and y == gy:
                    break

                for i in range(0, 4):
                    nx, ny = x + dx[i], y + dy[i]
                    if 0 <= nx < h and 0 <= ny < w and maze[nx][ny] != "#" and dp[nx][ny] > dp[x][y]:
                        q.appendleft((nx, ny))#ここを変更
                        dp[nx][ny] = dp[x][y]#ここを変更

                for j in range(0, 20):
                    nx2, ny2 = x + dxb[j], y + dyb[j]
                    if 0 <= nx2 < h and 0 <= ny2 < w and maze[nx2][ny2] != "#" and dp[nx2][ny2] > dp[x][y]:
                        q.append((nx2, ny2))
                        dp[nx2][ny2] = dp[x][y] + 1 
            return dp[gx][gy]
        return bfs()

    answer = clear_maze(c_h-1, c_w-1, d_h-1, d_w-1, s) if clear_maze(c_h-1, c_w-1, d_h-1, d_w-1, s) < INF else "-1" 
    # %%
    print(answer)

    #N = 1のときなどcorner caseを確認！
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
dhw = [(1,0),(0,-1),(0,1),(-1,0),
( 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)]

H, W = map(int, input().split())
sh, sw = map(lambda x:int(x)-1, input().split())
eh, ew = map(lambda x:int(x)-1, input().split())
S = [input() for _ in range(H)]

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

from collections import deque
dq = deque([(sh, sw, 0)])
while dq:
    h, w, d = dq.popleft()
    # if 0 <= h < H and 0 <= w < W and dst[h][w] < 0 and S[h][w] != '#':
    if dst[h][w] < 0 and S[h][w] != '#':
        dst[h][w] = d
    else:
        continue
    for i in range(4):
        nh = h + dhw[i][0]; nw = w + dhw[i][1]
        if 0 <= nh < H and 0 <= nw < W and dst[nh][nw] < 0 and S[nh][nw] != '#':
            dq.appendleft((nh, nw, d))
    for i in range(4, 24):
        nh = h + dhw[i][0]; nw = w + dhw[i][1]
        if 0 <= nh < H and 0 <= nw < W and dst[nh][nw] < 0 and S[nh][nw] != '#':
            dq.append((nh, nw, d+1))

print(dst[eh][ew])

Submission status: Time Limit Exceeded

Solution code:
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード
    #input
    h, w = map(int, input().split())
    c_h, c_w = map(int, input().split())
    d_h, d_w = map(int, input().split())
    s = ["" for _ in range(h)]
    for i in range(h):
        s[i] = str(input())

    #output
    from collections import deque

    INF = pow(10, 10)
    dp = [[INF] * w for _ in range(h)]

    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    dxb = [-2, -2, -2, -2, -2, -1, -1, -1, -1, 0, 0, 1, 1,  1, 1, 2, 2, 2, 2, 2]
    dyb = [-2, -1, 0,   1,  2, -2, -1,  1,  2,-2, 2, -2,-1, 1, 2, -2,-1,0, 1, 2]

    def clear_maze(sx, sy, gx, gy, maze):
        def bfs():
            q = deque([])
            q.append((sx, sy))
            dp[sx][sy] = 0

            while len(q):
                x, y = q.popleft()

                if x == gx and y == gy:
                    break

                for i in range(0, 4):
                    nx, ny = x + dx[i], y + dy[i]
                    if 0 <= nx < h and 0 <= ny < w and maze[nx][ny] != "#" and dp[nx][ny] > dp[x][y]:
                        q.appendleft((nx, ny))#ここを変更
                        dp[nx][ny] = dp[x][y]#ここを変更

                for j in range(0, 20):
                    nx2, ny2 = x + dxb[j], y + dyb[j]
                    if 0 <= nx2 < h and 0 <= ny2 < w and maze[nx2][ny2] != "#" and dp[nx2][ny2] > dp[x][y]:
                        q.append((nx2, ny2))
                        dp[nx2][ny2] = dp[x][y] + 1 
            return dp[gx][gy]
        return bfs()

    answer = clear_maze(c_h-1, c_w-1, d_h-1, d_w-1, s)
    # %%
    if answer == INF:
        print(-1)
    else:
        print(answer)

    #N = 1のときなどcorner caseを確認！
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import math
from itertools import product
from collections import deque

h, w = map(int, input().split())
c = tuple(map(lambda x: int(x) - 1, input().split()))
d = tuple(map(lambda x: int(x) - 1, input().split()))
S = {}
roads = set()
for i in range(h):
    s = input()
    for j in range(w):
        S[i, j] = s[j]
        if s[j] == '.':
            roads.add((i, j))

reached = {k: False for k in roads}
distance = {v: math.inf for v in roads}
distance[c] = 0
reached = {k: False for k in roads}
queue = deque([c])

while queue:
    u = queue.popleft()
    reached[u] = True
    i, j = u
    adj = []
    if i > 0 and S[i - 1, j] == '.':
        adj.append((i-1, j))
    if j > 0 and S[i, j - 1] == '.':
        adj.append((i, j - 1))
    if i < h-1 and S[i + 1, j] == '.':
        adj.append((i+1, j))
    if j < w-1 and S[i, j+1] == '.':
        adj.append((i, j+1))
    for v in adj:
        if not reached[v]:
            distance[v] = distance[u]
            queue.appendleft(v)

    warp = product(range(max(i-2, 0), min(i+3, h)),
                   range(max(j-2, 0), min(j+3, w)))
    for wa in warp:
        if wa != u and wa not in adj and S[wa] == '.' and not reached[wa]:
            distance[wa] = distance[u] + 1
            queue.append(wa)

print(distance[d] if distance[d] != math.inf else -1)

Submission status: Time Limit Exceeded

Solution code:
from collections import *
H,W = map(int,input().split())
ch,cw = map(int,input().split())
dh,dw = map(int,input().split())
S = 2*[(W+4)*"#"]+["##"+input()+"##" for h in range(H)]+2*[(W+4)*"#"]
ans = [(W+4)*[-1] for h in range(H+4)]
ans[ch+1][cw+1] = 0
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]]

for h in range(H+4):
  for w in range(W+4):
    if S[h][w]=="#":
      ans[h][w]=-2

P = deque([[ch+1,cw+1]])
Q = deque([])

while 0<len(P):
  x1,y1 = P.popleft()
  Q.append([x1,y1])
  for dx,dy in move:
    if ans[x1+dx][y1+dy]==-1:
      ans[x1+dx][y1+dy] = ans[x1][y1]
      P.append([x1+dx,y1+dy])
  
  if len(P)==0:
    while Q:
      x2,y2 = Q.popleft()
      for dx,dy in magic:
        if ans[x2+dx][y2+dy]==-1:
          ans[x2+dx][y2+dy] = ans[x2][y2]+1
          P.append([x2+dx,y2+dy])

print(ans[dh+1][dw+1])

Submission status: Time Limit Exceeded

Solution code:
from collections import *
H,W = map(int,input().split())
ch,cw = map(int,input().split())
dh,dw = map(int,input().split())
S = 2*[(W+4)*"#"]+["##"+input()+"##" for h in range(H)]+2*[(W+4)*"#"]
ans = [(W+4)*[-1] for h in range(H+4)]
ans[ch+1][cw+1] = 0
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]]

for h in range(H+4):
  for w in range(W+4):
    if S[h][w]=="#":
      ans[h][w]=-2

P = deque([[ch+1,cw+1]])
Q = deque([])

while 0<len(P):
  x,y = P.popleft()
  Q.append([x,y])
  for dx,dy in move:
    if ans[x+dx][y+dy]==-1:
      ans[x+dx][y+dy] = ans[x][y]
      P.append([x+dx,y+dy])
  
  if len(P)==0:
    while Q:
      x,y = Q.popleft()
      for dx,dy in magic:
        if ans[x+dx][y+dy]==-1:
          ans[x+dx][y+dy] = ans[x][y]+1
          P.append([x+dx,y+dy])

print(ans[dh+1][dw+1])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())
maze = [[9]*(w+4)]
maze.append([9]*(w+4))
for i in range(h):
  s = input()
  tmp = [9,9]
  for i in s:
    if i == "#":
      tmp.append(9)
    else:
      tmp.append(0)
  tmp.append(9)
  tmp.append(9)
  maze.append(tmp)
maze.append([9]*(w+4))
maze.append([9]*(w+4))
#print(maze)

pos = deque()
pos.append([ch+1,cw+1,0])
while len(pos)>0:
  x, y, hop = pos.popleft()
  #print(pos)
  #print(x,y,hop)
  if (x,y) == (dh+1,dw+1):
    print(hop)
    break
  maze[x][y] = 2
  
  if maze[x-1][y] <2:
    pos.appendleft([x-1,y,hop])
    #maze[x-1][y] = 2
    flag = 1
  if maze[x+1][y] <2:
    pos.appendleft([x+1,y,hop])
    #maze[x+1][y] = 2
    flag = 1
  if maze[x][y-1] <2:
    pos.appendleft([x,y-1,hop])
    flag = 1
    #maze[x][y+1] = 2
  if maze[x][y+1] <2:
    pos.appendleft([x,y+1,hop])
    #maze[x][y+1] = 2
    flag = 1
  #print(pos)  
  if maze[x-2][y] <2:
    pos.append([x-2,y,hop+1])
  if maze[x-2][y+1] <2:
    pos.append([x-2,y+1,hop+1])
  if maze[x-2][y+2] <2:
    pos.append([x-2,y+2,hop+1])
  if maze[x-2][y-1] <2:
    pos.append([x-2,y-1,hop+1])
  if maze[x-2][y-2] <2:
    pos.append([x-2,y-2,hop+1])
  if maze[x+2][y] <2:
    pos.append([x+2,y,hop+1])
  if maze[x+2][y-1] <2:
    pos.append([x+2,y-1,hop+1])
  if maze[x+2][y-2] <2:
    pos.append([x+2,y-2,hop+1])
  if maze[x+2][y+1] <2:
    pos.append([x+2,y+1,hop+1])
  if maze[x+2][y+2] <2:
    pos.append([x+2,y+2,hop+1])
  if maze[x][y-2] <2:
    pos.append([x,y-2,hop+1])
  if maze[x+1][y-2] <2:
    pos.append([x+1,y-2,hop+1])
  if maze[x-1][y-2] <2:
    pos.append([x-1,y-2,hop+1])
  if maze[x][y+2] <2:
    pos.append([x,y+2,hop+1])
  if maze[x+1][y+2] <2:
    pos.append([x+1,y+2,hop+1])
  if maze[x-1][y+2] <2:
    pos.append([x-1,y+2,hop+1])
  if maze[x-1][y-1] <2:
    pos.append([x-1,y-1,hop+1])
  if maze[x-1][y+1] <2:
    pos.append([x-1,y+1,hop+1])
  if maze[x+1][y-1] <2:
    pos.append([x+1,y-1,hop+1])
  if maze[x+1][y+1] <2:
    pos.append([x+1,y+1,hop+1])  
else:
  print(-1)