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:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=tf-8
#

"""
"""

from collections import defaultdict
import bisect
from itertools import product
from collections import deque

import sys
input = sys.stdin.readline

def solve(h,w,ch,cw,dh,dw,s):
    d = [[10**7]*w for _ in range(h)]
    d[ch][cw] = 0
    dis0 = [(1,0),(0,1),(-1,0),(0,-1)]
    dis1 = [(i,j) for i,j in product(range(-2,3),range(-2,3)) if 1 < abs(i)+abs(j)]
    Q = deque([(ch,cw)])
    ans = -1
    while Q:
        pos1 = Q.pop()
        i, j = pos1
        alt = d[i][j] 
        s[i][j] = "v"
        if i == dh and j==dw:
            ans = d[dh][dw]
            break
        #for k,l in [(i+a,j+b) for a,b in dis0 if 0 <= i+a < h and 0 <= j+b <w and s[i+a][j+b]=="." and d[i+a][j+b] > alt]:
        for k,l in [(i+a,j+b) for a,b in dis0 if 0 <= i+a < h and 0 <= j+b <w and s[i+a][j+b]=="."]:
            d[k][l] = alt
            Q.append((k,l))
        alt += 1
        for k,l in [(i+a,j+b) for a,b in dis1 if 0 <= i+a < h and 0 <= j+b <w and s[i+a][j+b]=="."]:
            d[k][l] = alt
            Q.appendleft((k,l))
    print(ans)



def main():
    h, w = list(map(int,input().split()))
    ch, cw = list(map(int,input().split()))
    dh, dw = list(map(int,input().split()))
    ch -= 1
    cw -= 1
    dh -= 1
    dw -= 1
    s = []
    for _ in range(h):
        s.append(list(input().rstrip()))
    solve(h,w,ch,cw,dh,dw,s)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

H, W = map(int, input().split())
cy, cx = map(int, input().split())
sy, sx = map(int, input().split())
tiles = [list(input()) for _ in range(H)]

# initialization
INF = 10**4
arrived = [[INF for _ in range(W)] for __ in range(H)]
paths = deque()
warps = deque()
dx = [1, 0, -1, 0]
dy = [0, -1, 0, 1]
moves = [-2, -1, 0, 1, 2]
arrived[cy-1][cx-1] = 0
paths.append([cx-1, cy-1])

# BFS
while(len(paths) > 0):
  while(len(paths) > 0):
    nx, ny = paths.popleft()
    count = arrived[ny][nx]
    for i in range(4):
      if 0 <= nx+dx[i] < W and 0 <= ny+dy[i] < H and arrived[ny+dy[i]][nx+dx[i]] > count and tiles[ny+dy[i]][nx+dx[i]] == '.':
        paths.append([nx+dx[i], ny+dy[i]])
        arrived[ny+dy[i]][nx+dx[i]] = count
    for x in moves:
      for y in moves:
        if 0 <= nx+x < W and 0 <= ny+y < H and arrived[ny+y][nx+x] == INF and tiles[ny+y][nx+x] == '.':
          warps.append([nx+x, ny+y])
          arrived[ny+y][nx+x] = count + 1
  while(len(warps) > 0):
    nx, ny = warps.popleft()
    count = arrived[ny][nx]
    for i in range(4):
      if 0 <= nx+dx[i] < W and 0 <= ny+dy[i] < H and arrived[ny+dy[i]][nx+dx[i]] > count and tiles[ny+dy[i]][nx+dx[i]] == '.':
        paths.append([nx+dx[i], ny+dy[i]])
        arrived[ny+dy[i]][nx+dx[i]] = count

if arrived[sy-1][sx-1] != INF:
  print(arrived[sy-1][sx-1])
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque

H, W = map(int, input().split())
cy, cx = map(int, input().split())
sy, sx = map(int, input().split())
tiles = [list(input()) for _ in range(H)]

# initialization
INF = 10**4
arrived = [[INF for _ in range(W)] for __ in range(H)]
paths = deque()
warps = deque()
dx = [1, 0, -1, 0]
dy = [0, -1, 0, 1]
moves = [-2, -1, 0, 1, 2]
arrived[cy-1][cx-1] = 0
paths.append([cx-1, cy-1])

# BFS
while(len(paths) > 0):
  while(len(paths) > 0):
    nx, ny = paths.popleft()
    count = arrived[ny][nx]
    for i in range(4):
      if 0 <= nx+dx[i] < W and 0 <= ny+dy[i] < H and arrived[ny+dy[i]][nx+dx[i]] > count and tiles[ny+dy[i]][nx+dx[i]] == '.':
        paths.append([nx+dx[i], ny+dy[i]])
        arrived[ny+dy[i]][nx+dx[i]] = count
    for x in moves:
      for y in moves:
        if 0 <= nx+x < W and 0 <= ny+y < H and arrived[ny+y][nx+x] > count and tiles[ny+y][nx+x] == '.':
          warps.append([nx+x, ny+y])
          arrived[ny+y][nx+x] = count + 1
  while(len(warps) > 0):
    nx, ny = warps.popleft()
    count = arrived[ny][nx]
    for i in range(4):
      if 0 <= nx+dx[i] < W and 0 <= ny+dy[i] < H and arrived[ny+dy[i]][nx+dx[i]] > count and tiles[ny+dy[i]][nx+dx[i]] == '.':
        paths.append([nx+dx[i], ny+dy[i]])
        arrived[ny+dy[i]][nx+dx[i]] = count

if arrived[sy-1][sx-1] != INF:
  print(arrived[sy-1][sx-1])
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
input = lambda: sys.stdin.readline().rstrip()

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

ch, cw = map(int, input().split())
dh, dw = map(int, input().split())

visited = {}

maze = []
for i in range(H):
    a = list(input())
    maze.append(a)

queue = deque([(ch-1, cw-1, 0)])
ans = float("inf")

visited[(ch-1, cw-1)] = 0
while queue:
    y, x, cnt = queue.popleft()

    if y == dh-1 and x == dw-1:
        ans = min(ans, cnt)

    for a, b in ([1, 0], [-1, 0], [0, 1], [0, -1]):
        new_y, new_x = y+a, x+b
        if 0 <= new_y < H and 0 <= new_x < W and maze[new_y][new_x] != '#':
            if (new_y, new_x) not in visited or visited[(new_y, new_x)] > cnt:
                queue.append((new_y, new_x, cnt))
                visited[(new_y, new_x)] = cnt

    for a in range(-2, 3):
        for b in range(-2, 3):
            new_y, new_x = y + a, x + b
            if 0 <= new_y < H and 0 <= new_x < W and maze[new_y][new_x] != '#':
                if (new_y, new_x) not in visited:
                    queue.append((new_y, new_x, cnt+1))
                    visited[(new_y, new_x)] = cnt+1

print(-1 if ans == float("inf") else ans)

Submission status: Wrong Answer

Solution code:

import sys
from collections import deque

#def input(): return sys.stdin.readline().strip()

icase=0
if icase==0:
    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 i in range(h)]
    for i in range(h):
        s[i]=input()
        
ij=[(1,0),(-1,0),(0,1),(0,-1)]
wpij=[(1,1),(-1,-1),(-1,1),(1,-1),
      (2,-2),(2,-1),(2,0),(2,1),(2,2),
      (-2,-2),(-2,-1),(-2,0),(-2,1),(-2,2),
      (1,2),(0,2),(-1,2),(1,-2),(0,-2),(-1,-2)]

g=[[[] for i in range(w)] for i in range(h)]
gg=[[[] for i in range(w)] for i in range(h)]
wpg=[]
for i in range(h):
    for j in range(w):
        for ii,jj in ij:
            if 0<=i+ii and i+ii<h and 0<=j+jj and \
                j+jj<w and s[i+ii][j+jj]==".":
                g[i][j].append((i+ii,j+jj))        
            if 0<=i+ii and i+ii<h and 0<=j+jj and \
                j+jj<w and s[i+ii][j+jj]=="#":
                wpg.append((i,j))        
#                print(i,j,g[i][j])
for i,j in wpg:
    for ii,jj in wpij:
         if 0<=i+ii and i+ii<h and 0<=j+jj \
            and j+jj<w and s[i+ii][j+jj]==".":
                gg[i][j].append((i+ii,j+jj))        

q=deque([(ch,cw)])

parent=[[-1]*w for i in range(h)]
parent[ch][cw]=0
dist=[[-1]*w for i in range(h)]
dist[ch][cw]=0
while True:
    while q:
#    print(q)
        hi,wi = q.popleft()
        for hj,wj in g[hi][wi]:
            if parent[hj][wj] !=-1:
                continue
            else:
                parent[hj][wj]=(hi,wi)
                dist[hj][wj]=dist[hi][wi]+1
                q.append((hj,wj))
                if hj==dh and wj==dw:
                    print(dist[dh][dw]//(10**7))   
                    sys.exit()

    for hj,wj in gg[hi][wi]:
        if parent[hj][wj] !=-1:
            continue
        else:
            parent[hj][wj]=(hi,wi)
            dist[hj][wj]=dist[hi][wi]+10**7
            q.append((hj,wj))
            if hj==dh and wj==dw:
                print(dist[dh][dw]//(10**7))   
                sys.exit()
    if len(q)==0:
        break
print(-1)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

#def input(): return sys.stdin.readline().strip()

icase=0
if icase==0:
    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 i in range(h)]
    for i in range(h):
        s[i]=input()
        
ij=[(1,0),(-1,0),(0,1),(0,-1)]
wpij=[(1,1),(-1,-1),(-1,1),(1,-1),
      (2,-2),(2,-1),(2,0),(2,1),(2,2),
      (-2,-2),(-2,-1),(-2,0),(-2,1),(-2,2),
      (1,2),(0,2),(-1,2),(1,-2),(0,-2),(-1,-2)]

g=[[[] for i in range(w)] for i in range(h)]
gg=[[[] for i in range(w)] for i in range(h)]
wpg=[[[] for i in range(w)] for i in range(h)]
for i in range(h):
    for j in range(w):
        for ii,jj in ij:
            if 0<=i+ii and i+ii<h and 0<=j+jj and \
                j+jj<w and s[i+ii][j+jj]==".":
                g[i][j].append((i+ii,j+jj))        
            if 0<=i+ii and i+ii<h and 0<=j+jj and \
                j+jj<w and s[i+ii][j+jj]=="#":
                wpg[i][j].append((i+ii,j+jj))        
#                print(i,j,g[i][j])
for i in range(h):
    for j in range(w):
        for ii,jj in wpg[i][j]:
            for iii,jjj in wpij:
                 if 0<=ii+iii and ii+iii<h and 0<=jj+jjj \
                    and jj+jjj<w and s[ii+iii][jj+jjj]==".":
                        gg[ii][jj].append((ii+iii,jj+jjj))        

q=deque([(ch,cw)])

parent=[[-1]*w for i in range(h)]
parent[ch][cw]=0
dist=[[-1]*w for i in range(h)]
dist[ch][cw]=0
while True:
    while q:
#    print(q)
        hi,wi = q.popleft()
        for hj,wj in g[hi][wi]:
            if parent[hj][wj] !=-1:
                continue
            else:
                parent[hj][wj]=(hi,wi)
                dist[hj][wj]=dist[hi][wi]+1
                q.append((hj,wj))
                if hj==dh and wj==dw:
                    print(dist[dh][dw]//(10**7))   
                    sys.exit()

    for hj,wj in gg[hi][wi]:
        if parent[hj][wj] !=-1:
            continue
        else:
            parent[hj][wj]=(hi,wi)
            dist[hj][wj]=dist[hi][wi]+10**7
            q.append((hj,wj))
            if hj==dh and wj==dw:
                print(dist[dh][dw]//(10**7))   
                sys.exit()
    if len(q)==0:
        break
print(-1)

Submission status: Wrong Answer

Solution code:

import sys
from collections import deque

#def input(): return sys.stdin.readline().strip()

icase=0
if icase==0:
    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 i in range(h)]
    for i in range(h):
        s[i]=input()
        
ij=[(1,0),(-1,0),(0,1),(0,-1)]
wpij=[(1,1),(-1,-1),(-1,1),(1,-1),
      (2,-2),(2,-1),(2,0),(2,1),(2,2),
      (-2,-2),(-2,-1),(-2,0),(-2,1),(-2,2),
      (1,2),(0,2),(-1,2),(1,-2),(0,-2),(-1,-2)]

g=[[[] for i in range(w)] for i in range(h)]
gg=[[[] for i in range(w)] for i in range(h)]
wpg=[[[] for i in range(w)] for i in range(h)]
for i in range(h):
    for j in range(w):
        for ii,jj in ij:
            if 0<=i+ii and i+ii<h and 0<=j+jj and \
                j+jj<w and s[i+ii][j+jj]==".":
                g[i][j].append((i+ii,j+jj))        
            if 0<=i+ii and i+ii<h and 0<=j+jj and \
                j+jj<w and s[i+ii][j+jj]=="#":
                wpg[i][j].append((i+ii,j+jj))        
#                print(i,j,g[i][j])
for i in range(h):
    for j in range(w):
        for ii,jj in wpg[i][j]:
            for iii,jjj in wpij:
                 if 0<=i+iii and i+iii<h and 0<=j+jjj \
                    and j+jjj<w and s[i+iii][j+jjj]==".":
                        gg[i][j].append((i+iii,j+jjj))        

q=deque([(ch,cw)])

parent=[[-1]*w for i in range(h)]
parent[ch][cw]=0
dist=[[-1]*w for i in range(h)]
dist[ch][cw]=0
while True:
    while q:
#    print(q)
        hi,wi = q.popleft()
        for hj,wj in g[hi][wi]:
            if parent[hj][wj] !=-1:
                continue
            else:
                parent[hj][wj]=(hi,wi)
                dist[hj][wj]=dist[hi][wi]+1
                q.append((hj,wj))
                if hj==dh and wj==dw:
                    print(dist[dh][dw]//(10**7))   
                    sys.exit()

    for hj,wj in gg[hi][wi]:
        if parent[hj][wj] !=-1:
            continue
        else:
            parent[hj][wj]=(hi,wi)
            dist[hj][wj]=dist[hi][wi]+10**7
            q.append((hj,wj))
            if hj==dh and wj==dw:
                print(dist[dh][dw]//(10**7))   
                sys.exit()
    if len(q)==0:
        break
print(-1)

Submission status: Wrong Answer

Solution code:
import numpy as np
from queue import Queue

H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Dh, Dw = map(int, input().split())

Ch -= 1
Cw -= 1
Dh -= 1
Dw -= 1

maze = []
for i in range(H):
    maze.append(input())

INF = 10 ** 12

score = np.zeros((H, W), dtype=int)
score[:, :] = INF

score[Ch, Cw] = 0

#visited = np.zeros((H, W), dtype=bool)

candidates = Queue()
candidates.put((Ch, Cw))

while not candidates.empty():
    moved = False
    h, w = candidates.get()
    # move
    dh = [-1, +1, 0, 0]
    dw = [0, 0, +1, -1]
    for i in range(4):
        y = h + dh[i]
        x = w + dw[i]
        if y < 0 or H <= y:
            continue
        if x < 0 or W <= x:
            continue        
        if maze[y][x] == '.' and score[y, x] > score[h, w]:
            score[y, x] = score[h, w]
            candidates.put((y, x))
            moved = True        
    # warp
    #if not moved: 
    neighbors = [-2, -1, 0, 1, -2]
    for dh in neighbors:
        for dw in neighbors:
            y = h + dh
            x = w + dw
            if y < 0 or H <= y:
                continue
            if x < 0 or W <= x:
                continue
            if maze[y][x] == '.' and score[y, x] > score[h, w] + 1:
                score[y, x] = score[h, w] + 1
                candidates.put((y, x))

if score[Dh, Dw] == INF:
    print(-1)
else:
    print (score[Dh, Dw])

Submission status: Wrong Answer

Solution code:
import numpy as np
from queue import Queue

H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Dh, Dw = map(int, input().split())

Ch -= 1
Cw -= 1
Dh -= 1
Dw -= 1

maze = []
for i in range(H):
    maze.append(input())

INF = 10 ** 12

score = np.zeros((H, W), dtype=int)
score[:, :] = INF

score[Ch, Cw] = 0

visited = np.zeros((H, W), dtype=bool)

candidates = Queue()
candidates.put((Ch, Cw))

while not candidates.empty():
    moved = False
    h, w = candidates.get()
    # move
    dh = [-1, +1, 0, 0]
    dw = [0, 0, +1, -1]
    for i in range(4):
        y = h + dh[i]
        x = w + dw[i]
        if y < 0 or H <= y:
            continue
        if x < 0 or W <= x:
            continue        
        if maze[y][x] == '.' and score[y, x] > score[h, w] :
            score[y, x] = score[h, w]
            candidates.put((y, x))
            moved = True        
    # warp
    if not moved: 
        neighbors = [-2, -1, 0, 1, -2]
        for dh in neighbors:
            for dw in neighbors:
                y = h + dh
                x = w + dw
                if y < 0 or H <= y:
                    continue
                if x < 0 or W <= x:
                    continue
                if maze[y][x] == '.' and score[y, x] > score[h, w] + 1:
                    score[y, x] = score[h, w] + 1
                    candidates.put((y, x))

if score[Dh, Dw] == INF:
    print(-1)
else:
    print (score[Dh, Dw])

Submission status: Wrong Answer

Solution code:
import numpy as np
from queue import Queue

H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Dh, Dw = map(int, input().split())

Ch -= 1
Cw -= 1
Dh -= 1
Dw -= 1

maze = []
for i in range(H):
    maze.append(input())

score = np.zeros((H, W), dtype=int)
score[:, :] = -1

score[Ch, Cw] = 0

visited = np.zeros((H, W), dtype=bool)

candidates = Queue()
candidates.put((Ch, Cw))

while not candidates.empty():
    moved = False
    h, w = candidates.get()
    # move
    dh = [-1, +1, 0, 0]
    dw = [0, 0, +1, -1]
    for i in range(4):
        y = h + dh[i]
        x = w + dw[i]
        if y < 0 or H <= y:
            continue
        if x < 0 or W <= x:
            continue        
        if maze[y][x] == '.' and score[y, x] < 0:
            score[y, x] = score[h, w]
            candidates.put((y, x))
            moved = True        
    # warp
    if not moved: 
        neighbors = [-2, -1, 0, 1, -2]
        for dh in neighbors:
            for dw in neighbors:
                y = h + dh
                x = w + dw
                if y < 0 or H <= y:
                    continue
                if x < 0 or W <= x:
                    continue
                if maze[y][x] == '.' and score[y, x] > score[h, w] + 1:
                    score[y, x] = score[h, w] + 1
                    candidates.put((y, x))

print (score[Dh, Dw])

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys

imput = sys.stdin.readline

h,w = map(int,input().split())
sy,sx= map(int,input().split())
gy,gx= map(int,input().split())
S=[]



for i in range(h):
    S.append(input())

walk = [[1,0],[-1,0],[0,1],[0,-1]]
move = [[x,y] for x in range(-2,3) for y in range(-2,3) if [x,y] not in walk + [[0,0]]] 

dist = [[99999]*w for i in range(h)]
#visited = [[0]*w for i in range(h)]

q=deque()
q.append((sx-1,sy-1,0))
dist[sy-1][sx-1]=0
goaled = 0
endwarp=0
#while goaled==0:
while len(q):
    endwarp=0
    
    x,y,d = q.popleft()
    
#    if x==gx-1 and y==gy-1:
#        goaled=1
#        print(d)
#        break
    
#    print(x,y,d)
    for i in walk:
        if 0<=x+i[0]<w and 0<=y+i[1]<h and S[y+i[1]][x+i[0]]!="#" and dist[y+i[1]][x+i[0]] > d:

                    dist[y+i[1]][x+i[0]] = d
                    q.appendleft((x+i[0],y+i[1],d+0))

    for i in move:
        if 0<=x+i[0]<w and 0<=y+i[1]<h and S[y+i[1]][x+i[0]]!="#" and dist[y+i[1]][x+i[0]] > d:

                    dist[y+i[1]][x+i[0]] = d+1
                    q.append((x+i[0],y+i[1],d+1))
                        
                        
                         
    
    

if dist[gy-1][gx-1]==99999:
    print(-1)
    
else:
    print(dist[gy-1][gx-1])

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys

imput = sys.stdin.readline

h,w = map(int,input().split())
sy,sx= map(int,input().split())
gy,gx= map(int,input().split())
S=[]



for i in range(h):
    S.append(input())

walk = [[1,0],[-1,0],[0,1],[0,-1]]
move = [[x,y] for x in range(-2,3) for y in range(-2,3) if [x,y] not in walk + [[0,0]]] 

dist = [[99999]*w for i in range(h)]
#visited = [[0]*w for i in range(h)]

q=deque()
q.append((sx-1,sy-1,0))
dist[sy-1][sx-1]=0
goaled = 0
endwarp=0
#while goaled==0:
while len(q):
    endwarp=0
    
    x,y,d = q.popleft()
    
#    if x==gx-1 and y==gy-1:
#        goaled=1
#        print(d)
#        break
    
#    print(x,y,d)
    for i in walk:
        if 0<=x+i[0]<w and 0<=y+i[1]<h:
            if S[y+i[1]][x+i[0]]!="#" :
                if dist[y+i[1]][x+i[0]] > d:

                    dist[y+i[1]][x+i[0]] = d
                    q.appendleft((x+i[0],y+i[1],d+0))

    for i in move:
        if 0<=x+i[0]<w and 0<=y+i[1]<h and S[y+i[1]][x+i[0]]!="#" and dist[y+i[1]][x+i[0]] > d+1:

                    dist[y+i[1]][x+i[0]] = d+1
                    q.append((x+i[0],y+i[1],d+1))
                        
                        
                         
    
    

if dist[gy-1][gx-1]==99999:
    print(-1)
    
else:
    print(dist[gy-1][gx-1])

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys

imput = sys.stdin.readline

h,w = map(int,input().split())
sy,sx= map(int,input().split())
gy,gx= map(int,input().split())
S=[]



for i in range(h):
    S.append(input())

walk = [[1,0],[-1,0],[0,1],[0,-1]]
move = [[x,y] for x in range(-2,3) for y in range(-2,3) if [x,y] not in walk + [[0,0]]] 

dist = [[99999]*w for i in range(h)]
#visited = [[0]*w for i in range(h)]

q=deque()
q.append((sx-1,sy-1,0))
dist[sy-1][sx-1]=0
goaled = 0
endwarp=0
#while goaled==0:
while len(q):
    endwarp=0
    
    x,y,d = q.popleft()
    
#    if x==gx-1 and y==gy-1:
#        goaled=1
#        print(d)
#        break
    
#    print(x,y,d)
    for i in walk:
        if 0<=x+i[0]<w and 0<=y+i[1]<h and S[y+i[1]][x+i[0]]!="#" and dist[y+i[1]][x+i[0]] > d:

                    dist[y+i[1]][x+i[0]] = d
                    q.appendleft((x+i[0],y+i[1],d+0))

    for i in move:
        if 0<=x+i[0]<w and 0<=y+i[1]<h and S[y+i[1]][x+i[0]]!="#" and dist[y+i[1]][x+i[0]] > d+1:

                    dist[y+i[1]][x+i[0]] = d+1
                    q.append((x+i[0],y+i[1],d+1))
                        
                        
                         
    
    

if dist[gy-1][gx-1]==99999:
    print(-1)
    
else:
    print(dist[gy-1][gx-1])

Submission status: Wrong Answer

Solution code:
H,W = map(int , input().split())
sh,sw = map(int , input().split())
eh,ew = map(int , input().split())
mv_a_pts = [[-1,0],[1,0],[0,-1],[0,1]]
mv_b_pts = []
for h in range(-2,3,1):
  for w in range(-2,3,1):
    if h!=0 and w!=0:
      mv_b_pts.append([h,w])
maze = []
warp_cnts = [[None]*(W) for j in range(H)]
for i in range(H):
  maze.append(input())

def mv_a(h,w):
  mv_b_tgs.append([h,w])
  warp_cnts[h][w] = cnt
  for vh,vw in mv_a_pts:
    tgh = h + vh
    tgw = w + vw
    if 0 <= tgh <= H-1 and 0 <= tgw <= W-1 :
      if warp_cnts[tgh][tgw] is None and maze[tgh][tgw] != "#":
        mv_a(tgh,tgw)
        

def mv_b(h,w):
  for vh,vw in mv_b_pts:
    tgh = h + vh
    tgw = w + vw
    if 0 <= tgh <= H-1 and 0 <= tgw <= W-1 :
      if warp_cnts[tgh][tgw] is None and maze[tgh][tgw] != "#":
        mv_a_tgs.append([tgh,tgw])
mv_b_tgs=[]
mv_a_tgs = [[sh-1,sw-1]]
cnt = 0
while len(mv_a_tgs)>0:
  mv_b_tgs=[]
  for tg in mv_a_tgs:
    mv_a(tg[0],tg[1])
  mv_a_tgs = []
  for wp in mv_b_tgs:
    mv_b(wp[0],wp[1])
  cnt+=1
print(-1 if warp_cnts[eh-1][ew-1] is None else warp_cnts[eh-1][ew-1])

Submission status: Wrong Answer

Solution code:
H,W = map(int , input().split())
sh,sw = map(int , input().split())
eh,ew = map(int , input().split())
mv_a_pts = [[-1,0],[1,0],[0,-1],[0,1]]
mv_b_pts = []
for h in range(-2,3,1):
  for w in range(-2,3,1):
    if h!=0 and w!=0:
      mv_b_pts.append([h,w])
print(mv_b_pts)
maze = []
warp_cnts = [[None]*(W) for j in range(H)]
for i in range(H):
  maze.append(input())

def mv_a(h,w):
  mv_b_tgs.append([h,w])
  warp_cnts[h][w] = cnt
  for vh,vw in mv_a_pts:
    tgh = h + vh
    tgw = w + vw
    if 0 <= tgh <= H-1 and 0 <= tgw <= W-1 :
      if warp_cnts[tgh][tgw] is None and maze[tgh][tgw] != "#":
        mv_a(tgh,tgw)
        

def mv_b(h,w):
  for vh,vw in mv_b_pts:
    tgh = h + vh
    tgw = w + vw
    if 0 <= tgh <= H-1 and 0 <= tgw <= W-1 :
      if warp_cnts[tgh][tgw] is None and maze[tgh][tgw] != "#":
        mv_a_tgs.append([tgh,tgw])
mv_b_tgs=[]
mv_a_tgs = [[sh-1,sw-1]]
cnt = 0
while len(mv_a_tgs)>0:
  mv_b_tgs=[]
  for tg in mv_a_tgs:
    mv_a(tg[0],tg[1])
  mv_a_tgs = []
  for wp in mv_b_tgs:
    mv_b(wp[0],wp[1])
  cnt+=1
print(-1 if warp_cnts[eh-1][ew-1] is None else warp_cnts[eh-1][ew-1])

Submission status: Wrong Answer

Solution code:
#ABC176
#D - Wizard in Maze
h,w=map(int,input().split())
ch,cw=map(int,input().split())
dh,dw=map(int,input().split())
b=[ch-1,cw-1]
a,c,d,f=[],[],[],[]
c.append(b)
d.append(b)
x=0

for i in range(h):
  a.append(list(input()))

warp=0

while len(c)>0:
  ch,cw=c[0]
  c.pop(0)
  if ch==dh-1 and cw==dw-1:
    print(warp)
    x=1
    c=[]
  else:
    if a[ch][cw]==".":
      a[ch][cw]=str(warp)
      
    e=[[ch-1,cw],[ch+1,cw],[ch,cw-1],[ch,cw+1]]
    for i in e:
      ch1,cw1=i
      if i not in d and 0<=ch1<h and 0<=cw1<w:
        if a[ch1][cw1]!="#":
          c.append(i)
          d.append(i)
    if len(c)==0:
      warp=warp+1
      for i in range(5):
        ch0=ch-2+i
        for j in range(5):
          cw0=cw-2+j
          f.append([ch0,cw0])
      for i in f:
        ch1,cw1=i
        if i not in d and 0<=ch1<h and 0<=cw1<w:
          if a[ch1][cw1]!="#":
            c.append(i)
            d.append(i)
            
if x!=1:
  print(-1)

Submission status: Wrong Answer

Solution code:
#ABC176
#D - Wizard in Maze
import numpy as np
h,w=map(int,input().split())
ch,cw=map(int,input().split())
dh,dw=map(int,input().split())
b=[ch-1,cw-1]
a,c,d,f=[],[],[],[]
c.append(b)
d.append(b)
y=0
x=0
for i in range(h):
  a.append(list(input()))

warp=0

while len(c)>0:
  ch,cw=c[0]
  c.pop(0)
  if ch==dh-1 and cw==dw-1:
    print(warp)
    x=1
    c=[]
  else:
    if a[ch][cw]==".":
      a[ch][cw]=str(warp)
      
    e=[[ch-1,cw],[ch+1,cw],[ch,cw-1],[ch,cw+1]]
    for i in e:
      ch1,cw1=i
      if i not in d and 0<=ch1<h and 0<=cw1<w:
        if a[ch1][cw1]!="#":
          c.append(i)
          d.append(i)
    if len(c)==0:
      warp=warp+1
      for i in range(5):
        ch0=ch-2+i
        for j in range(5):
          cw0=cw-2+j
          f.append([ch0,cw0])
      for i in f:
        ch1,cw1=i
        if i not in d and 0<=ch1<h and 0<=cw1<w:
          if a[ch1][cw1]!="#":
            c.append(i)
            d.append(i)
            
if x!=1:
  print(-1)

Submission status: Wrong Answer

Solution code:
H,W = map(int , input().split())
sh,sw = map(int , input().split())
eh,ew = map(int , input().split())
mv = [[-1,0],[1,0],[0,-1],[0,1]]
warplist = []
for h in range(-2,2,1):
  for w in range(-2,2,1):
    if h!=0 and w!=0:
      warplist.append([h,w])
maze = []
goned = [[None]*(W) for j in range(H)]
for i in range(H):
  maze.append(input())

def wfs(h,w):
  goned[h][w] = cnt
  for vh,vw in mv:
    tgh = h + vh
    tgw = w + vw
    if 0 <= tgh <= H-1 and 0 <= tgw <= W-1 :
      if goned[tgh][tgw] is None and maze[tgh][tgw] != "#":
        wfs(tgh,tgw)
        wps.append([tgh,tgw])

def warp(h,w):
  for vh,vw in warplist:
    tgh = h + vh
    tgw = w + vw
    if 0 <= tgh <= H-1 and 0 <= tgw <= W-1 :
      if goned[tgh][tgw] is None and maze[tgh][tgw] != "#":
        tgs.append([tgh,tgw])
wps=[]
tgs = [[sh-1,sw-1]]
cnt = 0
while len(tgs)>0:
  wps=[]
  for tg in tgs:
    wfs(tg[0],tg[1])
  tgs = []
  for wp in wps:
    warp(wp[0],wp[1])
  cnt+=1
print(-1 if goned[eh-1][ew-1] is None else goned[eh-1][ew-1])

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

icase=0
if icase==0:
    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 i in range(h)]
    for i in range(h):
        s[i]=input()
        
ij=[(1,0),(-1,0),(0,1),(0,-1)]
wpij=[(1,1),(-1,-1),(-1,1),(1,-1),
      (2,-2),(2,-1),(2,0),(2,1),(2,2),
      (-2,-2),(-2,-1),(-2,0),(-2,1),(-2,2),
      (1,2),(0,2),(-1,2),(1,-2),(0,-2),(-1,-2)]

g=[[[] for i in range(w)] for i in range(h)]
gg=[[[] for i in range(w)] for i in range(h)]
wpg=[[[] for i in range(w)] for i in range(h)]
for i in range(h):
    for j in range(w):
        for ii,jj in ij:
            if 0<=i+ii and i+ii<h and 0<=j+jj and \
                j+jj<w and s[i+ii][j+jj]==".":
                g[i][j].append((i+ii,j+jj))        
#                print(i,j,g[i][j])
for i in range(h):
    for j in range(w):
        for ii,jj in wpg[i][j]:
            for iii,jjj in wpij:
                 if 0<=i+iii and i+iii<h and 0<=j+jjj \
                    and j+jjj<w and s[i+iii][j+jjj]==".":
                        gg[i][j].append((i+iii,j+jjj))        

q=deque([(ch,cw)])

parent=[[-1]*w for i in range(h)]
parent[ch][cw]=0
dist=[[-1]*w for i in range(h)]
dist[ch][cw]=0
while q:
#    print(q)
    hi,wi = q.popleft()
    for hj,wj in g[hi][wi]:
        if parent[hj][wj] !=-1:
            continue
        else:
            parent[hj][wj]=(hi,wi)
            dist[hj][wj]=dist[hi][wi]+1
            q.append((hj,wj))
            if hj==dh and wj==dw:
                print(dist[dh][dw]//(10**7))   
                sys.exit()

    for hj,wj in gg[hi][wi]:
        if parent[hj][wj] !=-1:
            continue
        else:
            parent[hj][wj]=(hi,wi)
            dist[hj][wj]=dist[hi][wi]+10**7
            q.append((hj,wj))
            if hj==dh and wj==dw:
                print(dist[dh][dw]//(10**7))   
                sys.exit()
print(-1)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

icase=0
if icase==0:
    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 i in range(h)]
    for i in range(h):
        s[i]=input()
        
ij=[(1,0),(-1,0),(0,1),(0,-1)]
wpij=[(1,1),(-1,-1),(-1,1),(1,-1),
      (2,-2),(2,-1),(2,0),(2,1),(2,2),
      (-2,-2),(-2,-1),(-2,0),(-2,1),(-2,2),
      (1,2),(0,2),(-1,2),(1,-2),(0,-2),(-1,-2)]

g=[[[] for i in range(w)] for i in range(h)]
gg=[[[] for i in range(w)] for i in range(h)]
for i in range(h):
    for j in range(w):
        for ii,jj in ij:
            if 0<=i+ii and i+ii<h and 0<=j+jj and \
                j+jj<w and s[i+ii][j+jj]==".":
                g[i][j].append((i+ii,j+jj))        
#                print(i,j,g[i][j])
for i in range(h):
    for j in range(w):
        for ii,jj in ij:
            if 0<=i+ii and i+ii<h and 0<=j+jj and j+jj<w and s[i+ii][j+jj]=="#":
                for iii,jjj in wpij:
                    if 0<=i+iii and i+iii<h and 0<=j+jjj \
                       and j+jjj<w and s[i+iii][j+jjj]==".":
                        gg[i][j].append((i+iii,j+jjj))        

q=deque([(ch,cw)])

parent=[[-1]*w for i in range(h)]
parent[ch][cw]=0
dist=[[-1]*w for i in range(h)]
dist[ch][cw]=0
while q:
#    print(q)
    hi,wi = q.popleft()
    for hj,wj in g[hi][wi]:
        if parent[hj][wj] !=-1:
            continue
        else:
            parent[hj][wj]=(hi,wi)
            dist[hj][wj]=dist[hi][wi]+1
            q.append((hj,wj))
            if hj==dh and wj==dw:
                print(dist[dh][dw]//(10**7))   
                sys.exit()

    for hj,wj in gg[hi][wi]:
        if parent[hj][wj] !=-1:
            continue
        else:
            parent[hj][wj]=(hi,wi)
            dist[hj][wj]=dist[hi][wi]+10**7
            q.append((hj,wj))
            if hj==dh and wj==dw:
                print(dist[dh][dw]//(10**7))   
                sys.exit()
print(-1)