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

Solution (Python):
import sys
from collections import deque
from itertools import product
input = sys.stdin.readline


def validate_pos(y, x, h, w):
    if y < 0 or y >= h or x < 0 or x >= w:
        return False
    else:
        return True


def main():
    h, w = map(int, input().split())
    sy, sx = map(int, input().split())
    gy, gx = map(int, input().split())
    field = [input().rstrip() for _ in range(h)]
    seen = set()
    todo = deque([(sy-1, sx-1)])
    tmp_visited = []

    shifts = [[1, 0], [-1, 0], [0, 1], [0, -1]]
    warp_dist = [-2, -1, 0, 1, 2]
    warps = list(product(warp_dist, repeat=2))
    ans = 0
    while True:
        while todo:
            y, x = todo.popleft()
            tmp_visited.append((y, x))

            for dy, dx in shifts:
                ny, nx = y+dy, x+dx
                if not validate_pos(ny, nx, h, w):
                    continue
                if ny == gy-1 and nx == gx-1:
                    print(ans)
                    return
                if field[ny][nx] == "
                    continue
                if (ny, nx) in seen:
                    continue
                todo.append((ny, nx))
                seen.add((ny, nx))
                tmp_visited.append((ny, nx))

        ans += 1
        if not tmp_visited:
            print(-1)
            return

        for y, x in tmp_visited:
            for dy, dx in warps:
                ny, nx = y+dy, x+dx
                if not validate_pos(ny, nx, h, w):
                    continue
                if field[ny][nx] == "
                    continue
                if ny == gy-1 and nx == gx-1:
                    print(ans)
                    return
                if (ny, nx) in seen:
                    continue
                todo.append((ny, nx))
                seen.add((ny, nx))

        tmp_visited = []


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    from collections import deque
    H, W = (int(i) for i in input().split())
    Ch, Cw = (int(i)-1 for i in input().split())
    Dh, Dw = (int(i)-1 for i in input().split())
    A = [input() for i in range(H)]
    V = [[-1]*W for i in range(H)]  
    d = ((1, 0), (0, 1), (-1, 0), (0, -1))

    def bfs_2d(sy, sx, v):
        que = deque()
        V[sy][sx] = v
        que.append((sy, sx))
        while que:
            uh, uw = que.popleft()
            for dy, dx in d:
                next_h = uh + dy
                next_w = uw + dx
                if not(0 <= next_h < H and 0 <= next_w < W):
                    continue
                if V[next_h][next_w] != -1:
                    continue
                if A[next_h][next_w] == '
                    continue
                V[next_h][next_w] = v
                que.append((next_h, next_w))

    
    vertex = 0
    for h in range(H):
        for w in range(W):
            if A[h][w] == "
                continue
            bfs_2d(h, w, vertex)
            vertex += 1

    
    G = [[] for _ in range(vertex)]
    used = [set() for _ in range(vertex)]
    for h in range(H):
        for w in range(W):
            if A[h][w] == "
                continue
            for dh in range(-2, 3):
                for dw in range(-2, 3):
                    if h + dh < 0 or H <= h + dh or w + dw < 0 or W <= w + dw:
                        continue
                    if A[h+dh][w+dw] == "
                        continue
                    if V[h][w] == V[h+dh][w+dw]:
                        continue
                    if V[h+dh][w+dw] in used[V[h][w]]:
                        continue
                    G[V[h][w]].append((V[h+dh][w+dw]))
                    used[V[h][w]].add((V[h+dh][w+dw]))

    
    

    
    

    
    s = V[Ch][Cw]
    g = V[Dh][Dw]
    que = deque([0])

    dist = [-1 for _ in range(vertex)]
    par = [-1]*vertex
    que = deque()
    dist[s] = 0
    par[s] = -2  
    que.append(s)
    while que:
        u = que.popleft()
        for v in G[u]:
            if par[v] != -1:
                continue
            par[v] = u
            dist[v] = dist[u] + 1
            que.append(v)
    print(dist[g])


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque


def main():
    from collections import deque
    H, W = (int(i) for i in input().split())
    Ch, Cw = (int(i)-1 for i in input().split())
    Dh, Dw = (int(i)-1 for i in input().split())
    A = [input() for i in range(H)]
    V = [[-1]*W for i in range(H)]  
    d = ((1, 0), (0, 1), (-1, 0), (0, -1))

    def bfs_2d(sy, sx, v):
        que = deque()
        V[sy][sx] = v
        que.append((sy, sx))
        while que:
            uh, uw = que.popleft()
            for dy, dx in d:
                next_h = uh + dy
                next_w = uw + dx
                if not(0 <= next_h < H and 0 <= next_w < W):
                    continue
                if V[next_h][next_w] != -1:
                    continue
                if A[next_h][next_w] == '
                    continue
                V[next_h][next_w] = v
                que.append((next_h, next_w))

    
    vertex = 0
    for h in range(H):
        for w in range(W):
            if A[h][w] == "
                continue
            bfs_2d(h, w, vertex)
            vertex += 1

    
    G = [[] for _ in range(vertex)]
    used = [set() for _ in range(vertex)]
    for h in range(H):
        for w in range(W):
            if A[h][w] == "
                continue
            for dh in range(-2, 3):
                for dw in range(-2, 3):
                    if h + dh < 0 or H <= h + dh or w + dw < 0 or W <= w + dw:
                        continue
                    if A[h+dh][w+dw] == "
                        continue
                    if V[h][w] == V[h+dh][w+dw]:
                        continue
                    if V[h+dh][w+dw] in used[V[h][w]]:
                        continue
                    G[V[h][w]].append((V[h+dh][w+dw]))
                    used[V[h][w]].add((V[h+dh][w+dw]))

    
    

    
    

    
    s = V[Ch][Cw]
    g = V[Dh][Dw]
    que = deque([0])

    dist = [-1 for _ in range(vertex)]
    par = [-1]*vertex
    que = deque()
    dist[s] = 0
    que.append(s)
    while que:
        u = que.popleft()
        for v in G[u]:
            if par[v] != -1:
                continue
            par[v] = u
            dist[v] = dist[u] + 1
            que.append(v)

    print(dist[g])


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

H, W = map(int, input().split())

CH, CW = map(int, input().split())

DH, DW = map(int, input().split())

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

move_a = [(0, 1), (0, -1), (1, 0), (-1, 0)]

move_b = [(x, y) for x in range(-2, 3)
          for y in range(-2, 3) if (x, y) not in move_a + [(0, 0)]]

INF = 10 ** 10

path = [[INF] * W for _ in range(H)]

q = deque()
path[CH-1][CW-1] = 0
q.append((CH-1, CW-1, 0))

while q:

    x, y, s = q.popleft()

    
    for dx, dy in move_a:
        nx = x + dx
        ny = y + dy

        if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == "." and path[nx][ny] > s:
            path[nx][ny] = s
            q.appendleft((nx, ny, s))


    for dx, dy in move_b:
        nx = x + dx
        ny = y + dy

        if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == "." and path[nx][ny] > s+1:
            path[nx][ny] = s+1
            q.append((nx, ny, s+1))


print(path[DH-1][DW-1]) if path[DH-1][DW-1] < INF else print(-1)

Solution (Python):
import sys
input = sys.stdin.readline


def main():
    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 = ["
    s.append("
    for i in range(h):
        s.append("
    s.append("
    s.append("
    
    
    ans = [[-1]*(w+4) for _ in range(h+4)]
    for i in range(h+4):
        for j in range(w+4):
            if s[i][j] == "
                ans[i][j] = -2
    ans[ch][cw] = 0
    
    move = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    move2 = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2), \
             (-1, -2), (-1, -1), (-1, 0), (-1, 1), (-1, 2), \
             (0, -2), (0, -1), (0, 0), (0, 1), (0, 2), \
             (1, -2), (1, -1), (1, 0), (1, 1), (1, 2), \
             (2, -2), (2, -1), (2, 0), (2, 1), (2, 2)]
        
    not_yet = [(ch, cw)]
    one_time = []
    
    while len(not_yet) > 0:
        x, y = not_yet.pop()
        one_time.append((x, y))
        for (p, q) in move:
            v1, v2 = x+p, y+q
            if ans[v1][v2] == -1:
                not_yet.append((v1, v2))
                ans[v1][v2] = ans[x][y]
                
        if len(not_yet) == 0:
            while len(one_time) > 0:
                x2, y2 = one_time.pop()
                for (v1, v2) in move2:
                    i, j = x2+v1, y2+v2
                    if ans[i][j] == -1:
                        ans[i][j] = ans[x2][y2]+1
                        not_yet.append((i, j))
          
    print(ans[dh][dw])
    
    
    
if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline


def main():
    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 = ["
    s.append("
    for i in range(h):
        s.append("
    s.append("
    s.append("
    
    
    ans = [[-1]*(w+4) for _ in range(h+4)]
    for i in range(h+4):
        for j in range(w+4):
            if s[i][j] == "
                ans[i][j] = -2
    ans[ch][cw] = 0
    
    move = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    move2 = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2), \
             (-1, -2), (-1, -1), (-1, 0), (-1, 1), (-1, 2), \
             (0, -2), (0, -1), (0, 0), (0, 1), (0, 2), \
             (1, -2), (1, -1), (1, 0), (1, 1), (1, 2), \
             (2, -2), (2, -1), (2, 0), (2, 1), (2, 2)]
        
    not_yet = [(ch, cw)]
    one_time = []
    
    while len(not_yet) > 0:
        x, y = not_yet.pop()
        one_time.append((x, y))
        for (p, q) in move:
            v1, v2 = x+p, y+q
            if ans[v1][v2] == -1:
                not_yet.append((v1, v2))
                ans[v1][v2] = ans[x][y]
                
        if len(not_yet) == 0:
            while len(one_time) > 0:
                x2, y2 = one_time.pop()
                for (v1, v2) in move2:
                    i, j = x2+v1, y2+v2
                    if ans[i][j] == -1:
                        ans[i][j] = ans[x2][y2]+1
                        not_yet.append((i, j))
          
    print(ans[dh][dw])
    
    
    
if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline


def main():
    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 = ["
    s.append("
    for i in range(h):
        s.append("
    s.append("
    s.append("
    
    
    ans = [[-1]*(w+4) for _ in range(h+4)]
    for i in range(h+4):
        for j in range(w+4):
            if s[i][j] == "
                ans[i][j] = -2
    ans[ch][cw] = 0
    
    move = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    move2 = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2), \
             (-1, -2), (-1, -1), (-1, 0), (-1, 1), (-1, 2), \
             (0, -2), (0, -1), (0, 0), (0, 1), (0, 2), \
             (1, -2), (1, -1), (1, 0), (1, 1), (1, 2), \
             (2, -2), (2, -1), (2, 0), (2, 1), (2, 2)]
        
    not_yet = [(ch, cw)]
    one_time = []
    
    while len(not_yet) > 0:
        x, y = not_yet.pop()
        one_time.append((x, y))
        for (p, q) in move:
            v1, v2 = x+p, y+q
            if ans[v1][v2] == -1:
                not_yet.append((v1, v2))
                ans[v1][v2] = ans[x][y]
                
        if len(not_yet) == 0:
            while len(one_time) > 0:
                x2, y2 = one_time.pop()
                for (v1, v2) in move2:
                    i, j = x2+v1, y2+v2
                    if ans[i][j] == -1:
                        ans[i][j] = ans[x2][y2]+1
                        not_yet.append((i, j))
          
    print(ans[dh][dw])
    
    
    
if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import deque
input = sys.stdin.readline


def main():
    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 = ["
    s.append("
    for i in range(h):
        s.append("
    s.append("
    s.append("
    
    
    ans = [[-1]*(w+4) for _ in range(h+4)]
    for i in range(h+4):
        for j in range(w+4):
            if s[i][j] == "
                ans[i][j] = -2
    ans[ch][cw] = 0
    
    move = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    move2 = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2), \
             (-1, -2), (-1, -1), (-1, 0), (-1, 1), (-1, 2), \
             (0, -2), (0, -1), (0, 0), (0, 1), (0, 2), \
             (1, -2), (1, -1), (1, 0), (1, 1), (1, 2), \
             (2, -2), (2, -1), (2, 0), (2, 1), (2, 2)]
        
    not_yet = deque([(ch, cw)])
    one_time = deque([])
    
    while len(not_yet) > 0:
        x, y = not_yet.popleft()
        one_time.append((x, y))
        for (p, q) in move:
            v1, v2 = x+p, y+q
            if ans[v1][v2] == -1:
                not_yet.append((v1, v2))
                ans[v1][v2] = ans[x][y]
                
        if len(not_yet) == 0:
            while len(one_time) > 0:
                x2, y2 = one_time.popleft()
                for (v1, v2) in move2:
                    i, j = x2+v1, y2+v2
                    if ans[i][j] == -1:
                        ans[i][j] = ans[x2][y2]+1
                        not_yet.append((i, j))
          
    print(ans[dh][dw])
    
    
    
if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import deque
input = sys.stdin.readline


def main():
    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 = ["
    s.append("
    for i in range(h):
        s.append("
    s.append("
    s.append("
    
    
    ans = [[-1]*(w+4) for _ in range(h+4)]
    for i in range(h+4):
        for j in range(w+4):
            if s[i][j] == "
                ans[i][j] = -2
    ans[ch][cw] = 0
    
    move = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    move2 = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2), \
             (-1, -2), (-1, -1), (-1, 0), (-1, 1), (-1, 2), \
             (0, -2), (0, -1), (0, 0), (0, 1), (0, 2), \
             (1, -2), (1, -1), (1, 0), (1, 1), (1, 2), \
             (2, -2), (2, -1), (2, 0), (2, 1), (2, 2)]
        
    not_yet = deque([(ch, cw)])
    one_time = deque([])
    
    while len(not_yet) > 0:
        x, y = not_yet.popleft()
        one_time.append((x, y))
        for (p, q) in move:
            v1, v2 = x+p, y+q
            if ans[v1][v2] == -1:
                not_yet.append((v1, v2))
                ans[v1][v2] = ans[x][y]
                
        if len(not_yet) == 0:
            while len(one_time) > 0:
                x2, y2 = one_time.popleft()
                for (v1, v2) in move2:
                    i, j = x2+v1, y2+v2
                    if ans[i][j] == -1:
                        ans[i][j] = ans[x2][y2]+1
                        not_yet.append((i, j))
          
    print(ans[dh][dw])
    
    
    
if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque

def main():
    h, w = map(int, input().split())
    ch,cw = map(int, input().split())
    dh, dw = map(int, input().split())
    ls = [input() for _ in range(h)]

    widou = []
    for i in range(-2,3):
        for j in range(-2, 3):
            widou.append((i, j))
    widou.remove((0,0))
    widou.remove((1,0))
    widou.remove((0,1))
    widou.remove((-1,0))
    widou.remove((0,-1))

    start = (ch-1, cw-1)
    goal = (dh-1, dw-1)

    explored = set()
    tempexplored = set()

    ytemp= deque()
    ytemp.append(start)

    wap = 0

    while ytemp:
        now = ytemp.popleft()

        if now == goal:
            print(wap)
            exit(0)

        x = now[0]
        y = now[1]

        explored.add((x, y))
        tempexplored.add((x, y))

        idou = [(1,0), (0,1), (0,-1), (-1,0)]
        for i, j in idou:
            a = x + i
            b = y + j
            if 0 <= a < h and 0 <= b < w:
                if ls[a][b] == '.':
                    if (a, b) not in explored:
                        ytemp.append((a,b))
                        explored.add((a,b))
                        tempexplored.add((a,b))

        if len(ytemp) ==0:
            wap += 1
            tempx = set()
            for s,t in tempexplored:
                for i, j in widou:
                    a = s + i
                    b = t + j
                    if 0 <= a < h and 0 <= b < w:
                        if ls[a][b] == '.':
                            if (a, b) not in explored:
                                ytemp.append((a, b))
                                explored.add((a, b))
                                tempx = set()
            tempexplored = tempx
    print('-1')
if __name__ == '__main__':
    main()

Solution (Python):
import sys
from collections import deque
input = sys.stdin.readline


def main():
    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 = ["
    s.append("
    for i in range(h):
        s.append("
    s.append("
    s.append("
    
    
    ans = [[-1]*(w+4) for _ in range(h+4)]
    for i in range(h+4):
        for j in range(w+4):
            if s[i][j] == "
                ans[i][j] = -2
    ans[ch][cw] = 0
    
    move = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    move2 = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2), \
             (-1, -2), (-1, -1), (-1, 0), (-1, 1), (-1, 2), \
             (0, -2), (0, -1), (0, 0), (0, 1), (0, 2), \
             (1, -2), (1, -1), (1, 0), (1, 1), (1, 2), \
             (2, -2), (2, -1), (2, 0), (2, 1), (2, 2)]
        
    not_yet = deque([(ch, cw)])
    one_time = deque([])
    
    while len(not_yet) > 0:
        x, y = not_yet.popleft()
        one_time.append((x, y))
        for (p, q) in move:
            if ans[x+p][y+q] == -1:
                not_yet.append((x+p, y+q))
                ans[x+p][y+q] = ans[x][y]
                
        if len(not_yet) == 0:
            while len(one_time) > 0:
                x2, y2 = one_time.popleft()
                for (v1, v2) in move2:
                    i, j = x2+v1, y2+v2
                    if ans[i][j] == -1:
                        ans[i][j] = ans[x2][y2]+1
                        not_yet.append((i, j))
          
    print(ans[dh][dw])
    
    
    
if __name__ == "__main__":
    main()

Solution (Python):
def main():
    from collections import deque
    H, W = (int(i) for i in input().split())
    Ch, Cw = (int(i)-1 for i in input().split())
    Dh, Dw = (int(i)-1 for i in input().split())
    A = [input() for i in range(H)]
    V = [[-1]*W for i in range(H)]  
    d = ((1, 0), (0, 1), (-1, 0), (0, -1))

    def bfs_2d(sy, sx, v):
        que = deque()
        V[sy][sx] = v
        que.append((sy, sx))
        while que:
            uh, uw = que.popleft()
            for dy, dx in d:
                next_h = uh + dy
                next_w = uw + dx
                if not(0 <= next_h < H and 0 <= next_w < W):
                    continue
                if V[next_h][next_w] != -1:
                    continue
                if A[next_h][next_w] == '
                    continue
                V[next_h][next_w] = v
                que.append((next_h, next_w))

    
    vertex = 0
    for h in range(H):
        for w in range(W):
            if A[h][w] == "
                continue
            bfs_2d(h, w, vertex)
            vertex += 1

    
    G = [[] for _ in range(vertex)]
    used = [set() for _ in range(vertex)]
    for h in range(H):
        for w in range(W):
            if A[h][w] == "
                continue
            for dh in range(-2, 3):
                for dw in range(-2, 3):
                    if h + dh < 0 or H <= h + dh or w + dw < 0 or W <= w + dw:
                        continue
                    if A[h+dh][w+dw] == "
                        continue
                    if V[h][w] == V[h+dh][w+dw]:
                        continue
                    if V[h+dh][w+dw] in used[V[h][w]]:
                        continue
                    G[V[h][w]].append((V[h+dh][w+dw]))
                    used[V[h][w]].add((V[h+dh][w+dw]))

    
    

    
    

    
    s = V[Ch][Cw]
    g = V[Dh][Dw]
    que = deque([0])

    dist = [-1 for _ in range(vertex)]
    que = deque()
    dist[s] = 0
    que.append(s)
    while que:
        u = que.popleft()
        for v in G[u]:
            if dist[v] != -1:
                continue
            dist[v] = dist[u] + 1
            que.append(v)

    print(dist[g])


if __name__ == '__main__':
    main()

Solution (Python):
import heapq
INF = 1e7
def main():
    H,W=map(int, input().split())
    sx, sy = map(int, input().split())
    dh, dw = map(int, input().split())
    sx -= 1
    sy -= 1
    S = [input() for _ in range(H)]
    def dijkstra(sx, sy):
        D = [[INF]*(W) for _ in range(H)] 
        D[sx][sy] = 0
        PQ = [(0, (sx, sy))]
        heapq.heapify(PQ)
        while PQ: 
            _, (ux, uy) = heapq.heappop(PQ) 
            for dx in range(-2, 3):
                for dy in range(-2, 3):
                    xx, yy = ux+dx, uy+dy
                    if 0 <= xx < H and 0 <= yy < W and S[xx][yy]==".":
                        is_not_neighbor = int(abs(dx) + abs(dy) != 1)
                        
                        if abs(dx) + abs(dy) == 1:
                            if D[ux][uy] < D[xx][yy]:
                                D[xx][yy] = D[ux][uy]
                                heapq.heappush(PQ, (D[xx][yy], (xx, yy)))
                        else:
                            if D[ux][uy] + 1 < D[xx][yy]:
                                D[xx][yy] = D[ux][uy] + 1
                                heapq.heappush(PQ, (D[xx][yy], (xx, yy)))
        return D
    D = dijkstra(sx, sy)
    if D[dh-1][dw-1] >= INF:
        print(-1)
    else:
        print(D[dh-1][dw-1])
main()

Solution (Python):
from collections import deque

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


widou = []
for i in range(-2,3):
    for j in range(-2, 3):
        widou.append((i, j))
widou.remove((0,0))
widou.remove((1,0))
widou.remove((0,1))
widou.remove((-1,0))
widou.remove((0,-1))


start = (ch-1, cw-1)
goal = (dh-1, dw-1)

explored = set()
tempexplored = set()

ytemp= deque()
ytemp.append(start)

wap = 0

while ytemp:
    now = ytemp.popleft()

    if now == goal:
        print(wap)
        exit(0)

    x = now[0]
    y = now[1]

    explored.add((x, y))
    tempexplored.add((x, y))

    idou = [(1,0), (0,1), (0,-1), (-1,0)]
    for i, j in idou:
        a = x + i
        b = y + j
        if 0 <= a < h and 0 <= b < w:
            if ls[a][b] == '.':
                if (a, b) not in explored:
                    ytemp.append((a,b))
                    explored.add((a,b))
                    tempexplored.add((a,b))

    if len(ytemp) ==0:
        wap += 1
        tempx = set()
        for s,t in tempexplored:
            for i, j in widou:
                a = s + i
                b = t + j
                if 0 <= a < h and 0 <= b < w:
                    if ls[a][b] == '.':
                        if (a, b) not in explored:
                            ytemp.append((a, b))
                            explored.add((a, b))
                            tempx = set()
        tempexplored = tempx
print('-1')

Solution (Python):
import heapq
INF = 1e7
def main():
    H,W=map(int, input().split())
    sx, sy = map(int, input().split())
    dh, dw = map(int, input().split())
    sx -= 1
    sy -= 1
    S = [input() for _ in range(H)]
    def dijkstra(sx, sy):
        D = [[INF]*(W) for _ in range(H)] 
        D[sx][sy] = 0
        PQ = [(0, (sx, sy))]
        heapq.heapify(PQ)
        while PQ: 
            _, (ux, uy) = heapq.heappop(PQ) 
            for dx in range(-2, 3):
                for dy in range(-2, 3):
                    xx, yy = ux+dx, uy+dy
                    if 0 <= xx < H and 0 <= yy < W and S[xx][yy]==".":
                        is_not_neighbor = int(abs(dx) + abs(dy) != 1)
                        
                        if D[ux][uy] + is_not_neighbor < D[xx][yy]:
                            D[xx][yy] = D[ux][uy] + is_not_neighbor
                            heapq.heappush(PQ, (D[xx][yy], (xx, yy)))
        return D
    D = dijkstra(sx, sy)
    if D[dh-1][dw-1] >= INF:
        print(-1)
    else:
        print(D[dh-1][dw-1])
main()

Solution (Python):
import heapq
INF = 1e7
H,W=map(int, input().split())
sx, sy = map(int, input().split())
dh, dw = map(int, input().split())
sx -= 1
sy -= 1
S = [input() for _ in range(H)]
def dijkstra(sx, sy):
    D = [[INF]*(W) for _ in range(H)] 
    D[sx][sy] = 0
    PQ = [(0, (sx, sy))]
    heapq.heapify(PQ)
    while PQ: 
        _, (ux, uy) = heapq.heappop(PQ) 
        for dx in range(-2, 3):
            for dy in range(-2, 3):
                xx, yy = ux+dx, uy+dy
                if 0 <= xx < H and 0 <= yy < W and S[xx][yy]==".":
                    is_not_neighbor = int(abs(dx) + abs(dy) != 1)
                    
                    if D[ux][uy] + is_not_neighbor < D[xx][yy]:
                        D[xx][yy] = D[ux][uy] + is_not_neighbor
                        heapq.heappush(PQ, (D[xx][yy], (xx, yy)))
    return D
D = dijkstra(sx, sy)
if D[dh-1][dw-1] >= INF:
    print(-1)
else:
    print(D[dh-1][dw-1])

Solution (Python):


import sys
from collections import deque
sys.setrecursionlimit(10**7)
input=sys.stdin.readline
h,w=map(int,input().split())
c=[int(i)-1 for i in input().split()]
d=[int(i)-1 for i in input().split()]
s=[input() for i in range(h)]
inf=100000000
dp=[[-1 if s[i][j]=="
dp[c[0]][c[1]]=0
now=deque([[c[0],c[1]]])
def bfs():
    global h,w,s,dp,now
    while len(now):
        i,j=now.popleft()
        for k,l in [[i-1,j],[i+1,j],[i,j-1],[i,j+1]]:
            if 0<=k<h and 0<=l<w:
                if dp[k][l]!=-1:
                    if dp[k][l]>dp[i][j]:
                        dp[k][l]=dp[i][j]
                        now.appendleft([k,l])
        for k in range(i-2,i+3):
            for l in range(j-2,j+3):
                if 0<=k<h and 0<=l<w:
                    if dp[k][l]!=-1:
                        if dp[k][l]>dp[i][j]+1:
                            dp[k][l]=dp[i][j]+1
                            now.append([k,l])
bfs()
print(dp[d[0]][d[1]] if dp[d[0]][d[1]]!=inf else -1)

Solution (Python):
from collections import deque

H, W = map(int, input().split())
C = tuple(map(lambda a: int(a) - 1, input().split()))
D = tuple(map(lambda a: int(a) - 1, input().split()))
S = [input() for _ in range(H)]
INF = 10**18

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

minDist = [INF] * (H * W + 10)
que = deque([(C, 0)])
minDist[cord(*C)] = 0

while que:
    (h, w), dist = que.popleft()

    for dh in range(-2, 3):
        for dw in range(-2, 3):
            toH, toW = h + dh, w + dw
            if not (0 <= toH < H and 0 <= toW < W) or (h, w) == (toH, toW):
                continue
            if S[toH][toW] == '
                continue

            d = 0 if (abs(dh) + abs(dw) <= 1) else 1
            if minDist[cord(toH, toW)] > dist + d:
                minDist[cord(toH, toW)] = dist + d
                if d == 0: que.appendleft(((toH, toW), dist + d))
                else: que.append(((toH, toW), dist + d))

print(minDist[cord(*D)] if minDist[cord(*D)] < INF else -1)

Solution (Python):
import heapq
INF = 1e7
H,W=map(int, input().split())
sx, sy = map(int, input().split())
dh, dw = map(int, input().split())
sx -= 1
sy -= 1
S = [input() for _ in range(H)]
D = [[INF]*(W) for _ in range(H)] 
D[sx][sy] = 0
PQ = [(0, (sx, sy))]
heapq.heapify(PQ)
while PQ: 
    _, (ux, uy) = heapq.heappop(PQ) 
    for dx in range(-2, 3):
        for dy in range(-2, 3):
            xx, yy = ux+dx, uy+dy
            if 0 <= xx < H and 0 <= yy < W and S[xx][yy]==".":
                is_not_neighbor = int(abs(dx) + abs(dy) != 1)
                
                if D[ux][uy] + is_not_neighbor < D[xx][yy]:
                    D[xx][yy] = D[ux][uy] + is_not_neighbor
                    heapq.heappush(PQ, (D[xx][yy], (xx, yy)))
if D[dh-1][dw-1] >= INF:
    print(-1)
else:
    print(D[dh-1][dw-1])