Problem:


Score : 600 points

Problem Statement

Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.

Some of the squares have a lotus leaf on it and cannot be entered.
The square (i,j) has a lotus leaf on it if c_{ij} is @, and it does not if c_{ij} is ..

In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west.
The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.

Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2).
If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.

Constraints

1 \leq H,W,K \leq 10^6

H \times W \leq 10^6

1 \leq x_1,x_2 \leq H

1 \leq y_1,y_2 \leq W

x_1 \neq x_2 or y_1 \neq y_2.

c_{i,j} is . or @.

c_{x_1,y_1} = .

c_{x_2,y_2} = .

All numbers in input are integers.

Input

Input is given from Standard Input in the following format:

H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}

Output

Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print -1 if the travel is impossible.

Sample Input 1

3 5 2
3 2 3 4
.....
.@..@
..@..

Sample Output 1

5

Initially, Snuke is at the square (3,2).
He can reach the square (3, 4) by making five strokes as follows:

From (3, 2), go west one square to (3, 1).

From (3, 1), go north two squares to (1, 1).

From (1, 1), go east two squares to (1, 3).

From (1, 3), go east one square to (1, 4).

From (1, 4), go south two squares to (3, 4).

Sample Input 2

1 6 4
1 1 1 6
......

Sample Output 2

2

Sample Input 3

3 3 1
2 1 2 3
.@.
.@.
.@.

Sample Output 3

-1



Sample input:
3 5 2
3 2 3 4
.....
.@..@
..@..


Sample output:
5

Submission status: Wrong Answer

Solution code:
from collections import deque
H,W,K = map(int,input().split())
sx,sy,gx,gy = map(int,input().split())
sx-=1;sy-=1;gx-=1;gy-=1
Pond = []; INF = float("inf")
for i in range(H):
  temp = str(input());temp = list(temp)
  Pond.append(temp)
#print(Pond)
Q = deque([])
Q.append([sx,sy])
reach = [[INF]*W for _ in range(H)]
reach[sx][sy] = 0
dxdy = [(1,0),(-1,0),(0,1),(0,-1)]
while Q:
  nx,ny = Q.popleft()
  step = K
  for i in range(4):
    tx,ty = nx,ny
    while step > 0:
      tx += dxdy[i][0]
      ty += dxdy[i][1]
      #print(i,tx,ty)
      if 0>tx or tx>=H or 0>ty or ty>=W or Pond[tx][ty] == "@": #or reach[tx][ty] != INF:
        #print(i,tx,ty,reach[2][0])
        break #範囲外やハスがあった、到達済みの場合には終了
      #print(i,tx,ty)
      if reach[nx][ny] + 1 > reach[tx][ty]: #もし現時点よりもステップが大きい場合はだめ。
        #同じ場合は向き的に得している場合もあるので残す。
        break
      reach[tx][ty] = reach[nx][ny] + 1
      Q.append([tx,ty])
      step -= 1
#print(reach)
ans = reach[gx][gy]
if ans == INF:
  print(-1)
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    h, w, k = map(int, readline().split())
    x1, y1, x2, y2 = map(int, readline().split())
    grid = [["@"] * (w + 2)]
    for _ in range(h):
        s = ["@"] + list(input()) + ["@"]
        grid.append(s)
    grid.append(["@"] * (w + 2))

    def bfs():
        import heapq

        hq = []
        heapq.heappush(hq, (0, x1, y1, "C"))
        dist = [[INF] * (w + 2) for _ in range(h + 2)]
        dist[x1][y1] = 0
        op = {"N": "S", "S": "N", "E": "W", "W": "E", "C": "X"}

        while hq:
            cost, cx, cy, cd = heapq.heappop(hq)
            new_cost = cost + 1

            for mx, my, md in ((1, 0, "N"), (-1, 0, "S"), (0, 1, "E"), (0, -1, "W")):
                if cd == md:
                    continue
                if op[cd] == md:
                    break
                for i in range(1, k):
                    nx, ny = cx + mx * i, cy + my * i
                    if grid[nx][ny] == ".":
                        if new_cost < dist[nx][ny]:
                            heapq.heappush(hq, (new_cost, nx, ny, md))
                            dist[nx][ny] = new_cost
                        else:
                            continue
                    else:
                        break
                else:
                    nx, ny = cx + mx * k, cy + my * k
                    if grid[nx][ny] == ".":
                        if new_cost < dist[nx][ny]:
                            heapq.heappush(hq, (new_cost, nx, ny, "C"))
                            dist[nx][ny] = new_cost

        return dist[x2][y2]

    ans = bfs()
    if ans == INF:
        ans = -1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    h, w, k = map(int, readline().split())
    x1, y1, x2, y2 = map(int, readline().split())
    grid = [["@"] * (w + 2)]
    for _ in range(h):
        s = ["@"] + list(input()) + ["@"]
        grid.append(s)
    grid.append(["@"] * (w + 2))

    def bfs():
        import heapq
        hq = []
        heapq.heappush(hq, (0, x1, y1))
        dist = [[INF] * (w + 2) for _ in range(h + 2)]
        dist[x1][y1] = 0
        while hq:
            cost, cx, cy = heapq.heappop(hq)
            new_cost = cost + 1
            for mx, my in ((1, 0), (-1, 0), (0, 1), (0, -1)):
                for i in range(1, k + 1):
                    nx, ny = cx + mx * i, cy + my * i
                    if grid[nx][ny] == ".":
                        if cost + 1 < dist[nx][ny]:
                            heapq.heappush(hq, (new_cost, nx, ny))
                            dist[nx][ny] = cost + 1
                        else:
                            break
                    else:
                        break
        return dist[x2][y2]

    ans = bfs()
    if ans == INF:
        ans = -1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import deque
    ()

    h, w, k = map(int, readline().split())
    x1, y1, x2, y2 = map(int, readline().split())
    grid = [["@"] * (w + 2)]
    for _ in range(h):
        s = ["@"] + list(input()) + ["@"]
        grid.append(s)
    grid.append(["@"] * (w + 2))

    def bfs():
        import heapq
        hq = []
        heapq.heappush(hq, (0, x1, y1))
        grid[x1][y1] = "!"
        dist = [[INF] * (w + 2) for _ in range(h + 2)]
        dist[x1][y1] = 0
        while hq:
            cost, cx, cy = heapq.heappop(hq)
            new_cost = cost + 1
            for mx, my in ((1, 0), (-1, 0), (0, 1), (0, -1)):
                for i in range(1, k + 1):
                    nx, ny = cx + mx * i, cy + my * i
                    if grid[nx][ny] == ".":
                        if cost + 1 < dist[nx][ny]:
                            heapq.heappush(hq, (new_cost, nx, ny))
                            dist[nx][ny] = cost + 1
                            grid[nx][ny] = "!"
                    else:
                        break
        return dist[x2][y2]

    ans = bfs()
    if ans == INF:
        ans = -1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
INF =  float("inf")
import bisect

H, W, K = list(map(int, input().split()))
x1, y1, x2, y2 = list(map(int, input().split()))
c = []
for i in range(H):
    c.append(input())

# for i in range(H):
#     print(c[i])

dp = [[INF]*W for i in range(H)]
visited = [[0]*W for i in range(H)]

for i in range(H):
    for j in range(W):
        if c[i][j] == "@": dp[i][j] = -1

stack = [(x1-1,y1-1)]
dp[x1-1][y1-1] = 0
visited[x1-1][y1-1] = 1

# for i in range(H):
#     print(dp[i])

while len(stack) > 0:
    (i,j) = stack.pop()
    visited[i][j] = 1

    for k in range(1,min(K+1,W-j)):
        if dp[i][j+k] == -1:
            break
        dp[i][j+k] = min(dp[i][j] + 1, dp[i][j+k])
        if visited[i][j+k] == 0: stack.append((i,j+k))

    for k in range(1, min(K + 1, j+1)):
        if dp[i][j - k] == -1:
            break
        dp[i][j-k] = min(dp[i][j] + 1, dp[i][j-k])
        if visited[i][j - k] == 0: stack.append((i,j-k))

    for k in range(1,min(K+1,H-i)):
        if dp[i+k][j] == -1:
            break
        dp[i+k][j] = min(dp[i][j] + 1, dp[i+k][j])
        if visited[i+k][j] == 0: stack.append((i+k,j))

    for k in range(1, min(K + 1, i+1)):
        if dp[i-k][j] == -1:
            break
        dp[i-k][j] = min(dp[i][j] + 1, dp[i-k][j])
        if visited[i-k][j] == 0: stack.append((i-k,j))

# for i in range(H):
#     print(dp[i])

if dp[x2-1][y2-1] == INF:
    print(-1)
else:
    print(dp[x2-1][y2-1])

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w,k=map(int,input().split())
sy,sx,gy,gx=map(int,input().split())
G=[["@"]*(w+2) for i in range(h+2)]
for i in range(1,h+1):
  G[i]=["@"]+list(input())+["@"]
V=[[10**10]*(w+2) for i in range(h+2)]
V[sy][sx]=0
D=[[[0,0,0,0] for j in range(w+2)] for i in range(h+2)]
D[sy][sx]=[1,1,1,1]
X=deque([])
Y=deque([])
i=1
while G[sy-i][sx]==".":
  Y.append(sy-i)
  X.append(sx)
  V[sy-i][sx]=1
  D[sy-i][sx][0]=1
  D[sy-i][sx][1]=1
  if i==k:
    D[sy-i][sx][0]=0
    break
  i=i+1
i=1
while G[sy+i][sx]==".":
  Y.append(sy+i)
  X.append(sx)
  V[sy+i][sx]=1
  D[sy+i][sx][0]=1
  D[sy-i][sx][1]=1
  if i==k:
    D[sy+i][sx][1]=0
    break
  i=i+1

i=1
while G[sy][sx-i]==".":
  Y.append(sy)
  X.append(sx-i)
  V[sy][sx-i]=1
  D[sy][sx-i][2]=1
  D[sy][sx-i][3]=1
  if i==k:
    D[sy][sx-i][2]=0
    break
  i=i+1
i=1
while G[sy][sx+i]==".":
  Y.append(sy)
  X.append(sx+i)
  V[sy][sx+i]=1
  D[sy][sx+i][2]=1
  D[sy][sx+i][3]=1
  if i==k:
    D[sy][sx+i][3]=0
    break
  i=i+1

def yn(yy,xx):
  D[yy][xx][0]=1
  i=1
  while G[yy-i][xx]==".":
    D[yy-i][xx][0]=1
    D[yy-i][xx][1]=1
    V[yy-i][xx]=min(V[yy-i][xx],V[yy][xx]+1)
    if i==k:
      D[yy-i][xx][0]=0
      Y.append(yy-i)
      X.append(xx)
      break
    if D[yy-i][xx]!=[1,1,1,1]:
      Y.append(yy-i)
      X.append(xx)
    i=i+1


def yp(yy,xx):
  D[yy][xx][1]=1
  i=1
  while G[yy+i][xx]==".":
    D[yy+i][xx][0]=1
    D[yy+i][xx][1]=1
    V[yy+i][xx]=min(V[yy+i][xx],V[yy][xx]+1)
    if i==k:
      D[yy+i][xx][1]=0
      Y.append(yy+i)
      X.append(xx)
      break
    if D[yy+i][xx]!=[1,1,1,1]:
      Y.append(yy+i)
      X.append(xx)
    i=i+1


def xn(yy,xx):
  D[yy][xx][2]=1
  i=1
  while G[yy][xx-i]==".":
    D[yy][xx-i][2]=1
    D[yy][xx-i][3]=1
    V[yy][xx-i]=min(V[yy][xx-i],V[yy][xx]+1)
    if i==k:
      D[yy][xx-i][2]=0
      Y.append(yy)
      X.append(xx-i)
      break
    if D[yy][xx-i]!=[1,1,1,1]:
      Y.append(yy)
      X.append(xx-i)
    i=i+1


def xp(yy,xx):
  D[yy][xx][3]=1
  i=1
  while G[yy][xx+i]==".":
    D[yy][xx+i][2]=1
    D[yy][xx+i][3]=1
    V[yy][xx+i]=min(V[yy][xx+i],V[yy][xx]+1)
    if i==k:
      D[yy][xx+i][3]=0
      Y.append(yy)
      X.append(xx+i)
      break
    if D[yy][xx+i]!=[1,1,1,1]:
      Y.append(yy)
      X.append(xx+i)
    i=i+1


while len(X)>0:
  x=X[0]
  y=Y[0]
  X.popleft()
  Y.popleft()
  if D[y][x][0]==0:
    yn(y,x)
  if D[y][x][1]==0:
    yp(y,x)
  if D[y][x][2]==0:
    xn(y,x)
  if D[y][x][3]==0:
    xp(y,x)
  
  
  
  
  
if V[gy][gx]==10**10:
  print(-1)
else:
  print(V[gy][gx])

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w,k=map(int,input().split())
sy,sx,gy,gx=map(int,input().split())
G=[["@"]*(w+2) for i in range(h+2)]
for i in range(1,h+1):
  G[i]=["@"]+list(input())+["@"]
V=[[10**10]*(w+2) for i in range(h+2)]
V[sy][sx]=0
D=[[[0,0,0,0] for j in range(w+2)] for i in range(h+2)]
D[sy][sx]=[1,1,1,1]
X=deque([])
Y=deque([])
i=1
while G[sy-i][sx]==".":
  Y.append(sy-i)
  X.append(sx)
  V[sy-i][sx]=1
  D[sy-i][sx][0]=1
  D[sy-i][sx][1]=1
  if i==k:
    D[sy-i][sx][0]=0
    break
  i=i+1
i=1
while G[sy+i][sx]==".":
  Y.append(sy+i)
  X.append(sx)
  V[sy+i][sx]=1
  D[sy+i][sx][0]=1
  D[sy-i][sx][1]=1
  if i==k:
    D[sy+i][sx][1]=0
    break
  i=i+1

i=1
while G[sy][sx-i]==".":
  Y.append(sy)
  X.append(sx-i)
  V[sy][sx-i]=1
  D[sy][sx-i][2]=1
  D[sy][sx-i][3]=1
  if i==k:
    D[sy][sx-i][2]=0
    break
  i=i+1
i=1
while G[sy][sx+i]==".":
  Y.append(sy)
  X.append(sx+i)
  V[sy][sx+i]=1
  D[sy][sx+i][2]=1
  D[sy][sx+1][3]=1
  if i==k:
    D[sy][sx+i][3]=0
    break
  i=i+1

def yn(yy,xx):
  D[yy][xx][0]=1
  i=1
  while G[yy-i][xx]==".":
    D[yy-i][xx][0]=1
    D[yy-i][xx][1]=1
    V[yy-i][xx]=min(V[yy-i][xx],V[yy][xx]+1)
    if i==k:
      D[yy-i][xx][0]=0
      Y.append(yy-i)
      X.append(xx)
      break
    if D[yy-i][xx]!=[1,1,1,1]:
      Y.append(yy-i)
      X.append(xx)
    i=i+1


def yp(yy,xx):
  D[yy][xx][1]=1
  i=1
  while G[yy+i][xx]==".":
    D[yy+i][xx][0]=1
    D[yy+i][xx][1]=1
    V[yy+i][xx]=min(V[yy+i][xx],V[yy][xx]+1)
    if i==k:
      D[yy+i][xx][1]=0
      Y.append(yy+i)
      X.append(xx)
      break
    if D[yy+i][xx]!=[1,1,1,1]:
      Y.append(yy+i)
      X.append(xx)
    i=i+1


def xn(yy,xx):
  D[yy][xx][2]=1
  i=1
  while G[yy][xx-i]==".":
    D[yy][xx-i][2]=1
    D[yy][xx-i][3]=1
    V[yy][xx-i]=min(V[yy][xx-i],V[yy][xx]+1)
    if i==k:
      D[yy][xx-i][2]=0
      Y.append(yy)
      X.append(xx-i)
      break
    if D[yy][xx-i]!=[1,1,1,1]:
      Y.append(yy)
      X.append(xx-i)
    i=i+1


def xp(yy,xx):
  D[yy][xx][3]=1
  i=1
  while G[yy][xx+i]==".":
    D[yy][xx+i][2]=1
    D[yy][xx+i][3]=1
    V[yy][xx+i]=min(V[yy][xx+i],V[yy][xx]+1)
    if i==k:
      D[yy][xx+i][3]=0
      Y.append(yy)
      X.append(xx+i)
      break
    if D[yy][xx+i]!=[1,1,1,1]:
      Y.append(yy)
      X.append(xx+i)
    i=i+1



while len(X)>0:
  x=X[0]
  y=Y[0]
  X.popleft()
  Y.popleft()
  if D[y][x][0]==0:
    yn(y,x)
  if D[y][x][1]==0:
    yp(y,x)
  if D[y][x][2]==0:
    xn(y,x)
  if D[y][x][3]==0:
    xp(y,x)
  
  
if V[gy][gx]==10**10:
  print(-1)
else:
  print(V[gy][gx])

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w,k=map(int,input().split())
sy,sx,gy,gx=map(int,input().split())
G=[["@"]*(w+2) for i in range(h+2)]
for i in range(1,h+1):
  G[i]=["@"]+list(input())+["@"]
V=[[10**10]*(w+2) for i in range(h+2)]
V[sy][sx]=0
D=[[[0,0,0,0] for j in range(w+2)] for i in range(h+2)]
D[sy][sx]=[1,1,1,1]
X=deque([])
Y=deque([])
i=1
while G[sy-i][sx]==".":
  Y.append(sy-i)
  X.append(sx)
  V[sy-i][sx]=1
  D[sy-i][sx][0]=1
  D[sy-i][sx][1]=1
  if i==k:
    D[sy-i][sx][0]=0
    break
  i=i+1
i=1
while G[sy+i][sx]==".":
  Y.append(sy+i)
  X.append(sx)
  V[sy+i][sx]=1
  D[sy+i][sx][0]=1
  D[sy-i][sx][1]=1
  if i==k:
    D[sy+i][sx][1]=0
    break
  i=i+1

i=1
while G[sy][sx-i]==".":
  Y.append(sy)
  X.append(sx-i)
  V[sy][sx-i]=1
  D[sy][sx-i][2]=1
  D[sy][sx-i][3]=1
  if i==k:
    D[sy][sx-i][2]=0
    break
  i=i+1
i=1
while G[sy][sx+i]==".":
  Y.append(sy)
  X.append(sx+i)
  V[sy][sx+i]=1
  D[sy][sx+i][2]=1
  D[sy][sx+1][3]=1
  if i==k:
    D[sy][sx+i][3]=0
    break
  i=i+1

def yn(yy,xx):
  D[yy][xx][0]=1
  i=1
  while G[yy-i][xx]==".":
    D[yy-i][xx][0]=1
    D[yy-i][xx][1]=1
    V[yy-i][xx]=min(V[yy-i][xx],V[yy][xx]+1)
    if i==k:
      D[yy-i][xx][0]=0
      Y.append(yy-i)
      X.append(xx)
      break
    if D[yy-i][xx]!=[1,1,1,1]:
      Y.append(yy-i)
      X.append(xx)
    i=i+1


def yp(yy,xx):
  D[yy][xx][1]=1
  i=1
  while G[yy+i][xx]==".":
    D[yy+i][xx][0]=1
    D[yy+i][xx][1]=1
    V[yy+i][xx]=min(V[yy+i][xx],V[yy][xx]+1)
    if i==k:
      D[yy+i][xx][1]=0
      Y.append(yy+i)
      X.append(xx)
      break
    if D[yy+i][xx]!=[1,1,1,1]:
      Y.append(yy+i)
      X.append(xx)
    i=i+1


def xn(yy,xx):
  D[yy][xx][2]=1
  i=1
  while G[yy][xx-i]==".":
    D[yy][xx-i][2]=1
    D[yy][xx-i][3]=1
    V[yy][xx-i]=min(V[yy][xx-i],V[yy][xx]+1)
    if i==k:
      D[yy][xx-i][2]=0
      Y.append(yy)
      X.append(xx-i)
      break
    if D[yy][xx-i]!=[1,1,1,1]:
      Y.append(yy)
      X.append(xx-i)
    i=i+1


def xp(yy,xx):
  D[yy][xx][3]=1
  i=1
  while G[yy][xx+i]==".":
    D[yy][xx+i][2]=1
    D[yy][xx+i][3]=1
    V[yy][xx+i]=min(V[yy][xx+i],V[yy][xx]+1)
    if i==k:
      D[yy][xx+i][3]=0
      Y.append(yy)
      X.append(xx+i)
      break
    if D[yy][xx+i]!=[1,1,1,1]:
      Y.append(yy)
      X.append(xx+i)
    i=i+1



while len(X)>0:
  x=X[0]
  y=Y[0]
  X.popleft()
  Y.popleft()
  if D[y][x][0]==0:
    yn(y,x)
  if D[y][x][1]==0:
    yp(y,x)
  if D[y][x][2]==0:
    xn(y,x)
  if D[y][x][3]==0:
    xp(y,x)
  
  
  
  
  
if V[gy][gx]==10**10:
  print(-1)
else:
  print(V[gy][gx])
print(V[1])

Submission status: Wrong Answer

Solution code:
import time
from collections import deque
t=time.time()
h,w,k=map(int,input().split())
sy,sx,gy,gx=map(int,input().split())
G=[["@"]*(w+2) for i in range(h+2)]
for i in range(1,h+1):
  G[i]=["@"]+list(input())+["@"]
V=[[10**10]*(w+2) for i in range(h+2)]
V[sy][sx]=0
D=[[[0,0] for j in range(w+2)] for i in range(h+2)]
D[sy][sx]=[1,1]
X=deque([])
Y=deque([])
i=1
while G[sy-i][sx]==".":
  Y.append(sy-i)
  X.append(sx)
  V[sy-i][sx]=1
  D[sy-i][sx][0]=1
  if i==k:
    D[sy-i][sx][0]=0
    break
  i=i+1
i=1
while G[sy+i][sx]==".":
  Y.append(sy+i)
  X.append(sx)
  V[sy+i][sx]=1
  D[sy+i][sx][0]=1
  if i==k:
    D[sy+i][sx][0]=0
    break
  i=i+1

i=1
while G[sy][sx-i]==".":
  Y.append(sy)
  X.append(sx-i)
  V[sy][sx-i]=1
  D[sy][sx-i][1]=1
  if i==k:
    D[sy][sx-i][1]=0
    break
  i=i+1
i=1
while G[sy][sx+i]==".":
  Y.append(sy)
  X.append(sx+i)
  V[sy][sx+i]=1
  D[sy][sx+i][1]=1
  if i==k:
    D[sy][sx+i][1]=0
    break
  i=i+1

while len(X)>0 and V[gy][gx]==10**10 and time.time()-t<2:
  x=X[0]
  y=Y[0]
  X.popleft()
  Y.popleft()
  if D[y][x]==[1,1]:
    continue
  if D[y][x][1]==0:
    i=1
    D[y][x][1]=1
    while G[y][x-i]==".":
      D[y][x-i][1]=1
      if D[y][x-i][0]==0:
        Y.append(y)
        X.append(x-i)
      V[y][x-i]=min(V[y][x-i],V[y][x]+1)
      if i==k:
        D[y][x-i][1]=0
        break
      i=i+1
    i=1
    while G[y][x+i]==".":
      D[y][x+i][1]=1
      if D[y][x+i][0]==0:
        Y.append(y)
        X.append(x+i)
      V[y][x+i]=min(V[y][x+i],V[y][x]+1)
      if i==k:
        D[y][x+i][1]=0
        break
      i=i+1
  if D[y][x][0]==0:
    i=1
    D[y][x][0]=1
    while G[y-i][x]==".":
      D[y-i][x][0]=1
      if D[y-i][x][1]==0:
        Y.append(y-i)
        X.append(x)
      V[y-i][x]=min(V[y-i][x],V[y][x]+1)
      if i==k:
        D[y-i][x][0]=0
        break
      i=i+1
    i=1
    while G[y+i][x]==".":
      D[y+i][x][0]=1
      if D[y+i][x][1]==0:
        Y.append(y+i)
        X.append(x)
      V[y+i][x]=min(V[y+i][x],V[y][x]+1)
      if i==k:
        D[y+1][x][0]=0
        break
      i=i+1
if V[gy][gx]==10**10 or time.time()-t>2:
  print(-1)
else:
  print(V[gy][gx])

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
input = sys.stdin.readline


def bfs(i1, j1) -> list:
    U, D, L, R = 0, 1, 2, 3
    INF = 10 ** 18
    dist = [[INF] * w for _ in range(h)]
    dist[i1][j1] = 0
    q = deque([(0, i1, j1)])
    while q:
        d, i, j = q.popleft()
        if dist[i][j] < d:
            continue
        for di, dj in ((-1, 0),  (1, 0), (0, -1), (0, 1)):
            for length in range(1, k + 1):
                nxt_i = i + di * length
                nxt_j = j + dj * length
                if not(0 <= nxt_i < h and 0 <= nxt_j < w):
                    break
                if c[nxt_i][nxt_j] == "@":
                    break
                if dist[i][j] + 1 <= dist[nxt_i][nxt_j]:
                    dist[nxt_i][nxt_j] = dist[i][j] + 1
                    q.append((d + 1, nxt_i, nxt_j))
                else:
                    break
    return dist


h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
c = [input() for i in range(h)]


x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1
dist = bfs(x1, y1)
ans = dist[x2][y2]
print(dist)
if ans == 10 ** 18:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
input = sys.stdin.readline


def bfs(i1, j1) -> list:
    U, D, L, R = 0, 1, 2, 3
    INF = 10 ** 18
    dist = [[INF] * w for _ in range(h)]
    dist[i1][j1] = 0
    q = deque([(0, i1, j1)])
    while q:
        d, i, j = q.popleft()
        if dist[i][j] < d:
            continue
        for di, dj in ((-1, 0),  (1, 0), (0, -1), (0, 1)):
            for length in range(1, k + 1):
                nxt_i = i + di * length
                nxt_j = j + dj * length
                if not(0 <= nxt_i < h and 0 <= nxt_j < w):
                    break
                if c[nxt_i][nxt_j] == "@":
                    break
                if dist[i][j] + 1 < dist[nxt_i][nxt_j]:
                    dist[nxt_i][nxt_j] = dist[i][j] + 1
                    q.append((d + 1, nxt_i, nxt_j))
                else:
                    break

    return dist


h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
c = [input() for i in range(h)]


x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1
dist = bfs(x1, y1)
ans = dist[x2][y2]

if ans == 10 ** 18:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
#from collections import deque
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7
#R = 998244353

def ddprint(x):
  if DBG:
    print(x)

import heapq
h,w,k = inm()
x1,y1,x2,y2 = inm()
x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1
c = []
for i in range(h):
    c.append(ins())
d = [[-1]*w for i in range(h)]
q = [(0,x1,y1)]
d[x1][y1] = 0
dir = [(0,1),(0,-1),(1,0),(-1,0)]
while len(q)>0:
    dd,x,y = heapq.heappop(q)
    dd += 1
    for dx,dy in dir:
        for i in range(1,k+1):
            xx = x+i*dx
            yy = y+i*dy
            if xx<0 or h<=xx or yy<0 or w<=yy or \
               d[xx][yy]>=0 or c[xx][yy]=='@':
                break
            d[xx][yy] = dd
            heapq.heappush(q,(dd,xx,yy))
v = d[x2][y2]
print(v if v>=0 else -1)

Submission status: Wrong Answer

Solution code:
#from collections import deque
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7
#R = 998244353

def ddprint(x):
  if DBG:
    print(x)

import heapq
h,w,k = inm()
x1,y1,x2,y2 = inm()
x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1
c = []
for i in range(h):
    c.append(ins())
d = [[-1]*w for i in range(h)]
q = [(0,x1,y1)]
d[x1][y1] = 0
dir = [(0,1),(0,-1),(1,0),(-1,0)]
while len(q)>0:
    dd,x,y = heapq.heappop(q)
    dd += 1
    for dx,dy in dir:
        for i in range(1,k+1):
            xx = x+i*dx
            yy = y+i*dy
            if xx<0 or h<=xx or yy<0 or w<=yy or \
               d[xx][yy]>=0 or c[xx][yy]=='@':
                break
            d[xx][yy] = dd
            heapq.heappush(q,(dd,xx,yy))
            if xx==x2 and yy==y2:
                print(dd)
                exit()
print(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7
#R = 998244353

def ddprint(x):
  if DBG:
    print(x)

h,w,k = inm()
x1,y1,x2,y2 = inm()
x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1
c = []
for i in range(h):
    c.append(ins())
d = [[-1]*w for i in range(h)]
q = deque([(x1,y1)])
d[x1][y1] = 0
dir = [(0,1),(0,-1),(1,0),(-1,0)]
while len(q)>0:
    x,y = q.popleft()
    dd = d[x][y]+1
    for dx,dy in dir:
        for i in range(1,k+1):
            xx = x+i*dx
            yy = y+i*dy
            if xx<0 or h<=xx or yy<0 or w<=yy or \
               d[xx][yy]>=0 or c[xx][yy]=='@':
                break
            d[xx][yy] = dd
            q.append((xx,yy))
            if xx==x2 and yy==y2:
                print(dd)
                exit()
print(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
input = sys.stdin.readline


def bfs(i1, j1) -> list:
    U, D, L, R = 0, 1, 2, 3
    INF = 10 ** 18
    dist = [[[INF] * w for _ in range(h)] for _ in range(4)]
    dist[U][i1][j1] = 0
    q = deque([(U, i1, j1)])
    while q:
        dire, i, j = q.popleft()

        for cnt in range(4):
            if dist[dire][i][j] < dist[(dire + cnt) % 4][i][j]:
                dist[(dire + cnt) % 4][i][j] = dist[dire][i][j]
                q.appendleft(((dire + cnt) % 4, i, j))

        if dire == U:
            for length in range(1, k + 1):
                nxt_i = i - length
                if 0 <= nxt_i and c[nxt_i][j] == "." and dist[dire][i][j] + 1 != dist[dire][nxt_i][j]:
                    dist[dire][nxt_i][j] = dist[dire][i][j] + 1
                    q.append((dire, nxt_i, j))
                else:
                    break
        if dire == D:
            for length in range(1, k + 1):
                nxt_i = i + length
                if nxt_i < h and c[nxt_i][j] == "." and dist[dire][i][j] + 1 != dist[dire][nxt_i][j]:
                    dist[dire][nxt_i][j] = dist[dire][i][j] + 1
                    q.append((dire, nxt_i, j))
                else:
                    break
        if dire == L:
            for length in range(1, k + 1):
                nxt_j = j - length
                if 0 <= nxt_j and c[i][nxt_j] == "." and dist[dire][i][j] + 1 != dist[dire][i][nxt_j]:
                    dist[dire][i][nxt_j] = dist[dire][i][j] + 1
                    q.append((dire, i, nxt_j))
                else:
                    break
        if dire == R:
            for length in range(1, k + 1):
                nxt_j = j + length
                if nxt_j < w and c[i][nxt_j] == "." and dist[dire][i][j] + 1 != dist[dire][i][nxt_j]:
                    dist[dire][i][nxt_j] = dist[dire][i][j] + 1
                    q.append((dire, i, nxt_j))
                else:
                    break

    return dist


h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
c = [input() for i in range(h)]


x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1
dist = bfs(x1, y1)
ans = min([dist[i][x2][y2] for i in range(4)])

if ans == 10 ** 18:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
input = sys.stdin.readline


def bfs(i1, j1) -> list:
    U, D, L, R = 0, 1, 2, 3
    INF = 10 ** 18
    dist = [[[INF] * w for _ in range(h)] for _ in range(4)]
    dist[U][i1][j1] = 0
    q = deque([(U, i1, j1)])
    while q:
        dire, i, j = q.popleft()

        for cnt in range(4):
            if dist[dire][i][j] < dist[(dire + cnt) % 4][i][j]:
                dist[(dire + cnt) % 4][i][j] = dist[dire][i][j]
                q.appendleft(((dire + cnt) % 4, i, j))

        if dire == U:
            for length in range(1, k + 1):
                nxt_i = i - length
                if 0 <= nxt_i and c[nxt_i][j] == "." and dist[dire][i][j] + 1 < dist[dire][nxt_i][j]:
                    dist[dire][nxt_i][j] = dist[dire][i][j] + 1
                    q.append((dire, nxt_i, j))
                else:
                    break
        if dire == D:
            for length in range(1, k + 1):
                nxt_i = i + length
                if nxt_i < h and c[nxt_i][j] == "." and dist[dire][i][j] + 1 < dist[dire][nxt_i][j]:
                    dist[dire][nxt_i][j] = dist[dire][i][j] + 1
                    q.append((dire, nxt_i, j))
                else:
                    break
        if dire == L:
            for length in range(1, k + 1):
                nxt_j = j - length
                if 0 <= nxt_j and c[i][nxt_j] == "." and dist[dire][i][j] + 1 < dist[dire][i][nxt_j]:
                    dist[dire][i][nxt_j] = dist[dire][i][j] + 1
                    q.append((dire, i, nxt_j))
                else:
                    break
        if dire == R:
            for length in range(1, k + 1):
                nxt_j = j + length
                if nxt_j < w and c[i][nxt_j] == "." and dist[dire][i][j] + 1 < dist[dire][i][nxt_j]:
                    dist[dire][i][nxt_j] = dist[dire][i][j] + 1
                    q.append((dire, i, nxt_j))
                else:
                    break

    return dist


h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
c = [input() for i in range(h)]


x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1
dist = bfs(x1, y1)
ans = min([dist[i][x2][y2] for i in range(4)])

if ans == 10 ** 18:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
input = sys.stdin.readline


def bfs(i1, j1) -> list:
    U, D, L, R = 0, 1, 2, 3
    INF = 10 ** 18
    dist = [[[INF] * w for _ in range(h)] for _ in range(4)]
    dist[R][i1][j1] = 0
    q = deque([(R, i1, j1)])
    while q:
        dire, i, j = q.popleft()

        for cnt in range(1, 4):
            if dist[dire][i][j] < dist[(dire + cnt) % 4][i][j]:
                dist[(dire + cnt) % 4][i][j] = dist[dire][i][j]
                q.appendleft(((dire + cnt) % 4, i, j))

        if dire == U:
            for length in range(1, k + 1):
                nxt_i = i - length
                if 0 <= nxt_i and c[nxt_i][j] == "." and dist[dire][i][j] + 1 < dist[dire][nxt_i][j]:
                    dist[dire][nxt_i][j] = dist[dire][i][j] + 1
                    q.append((dire, nxt_i, j))
                else:
                    break
        if dire == D:
            for length in range(1, k + 1):
                nxt_i = i + length
                if nxt_i < h and c[nxt_i][j] == "." and dist[dire][i][j] + 1 < dist[dire][nxt_i][j]:
                    dist[dire][nxt_i][j] = dist[dire][i][j] + 1
                    q.append((dire, nxt_i, j))
                else:
                    break
        if dire == L:
            for length in range(1, k + 1):
                nxt_j = j - length
                if 0 <= nxt_j and c[i][nxt_j] == "." and dist[dire][i][j] + 1 < dist[dire][i][nxt_j]:
                    dist[dire][i][nxt_j] = dist[dire][i][j] + 1
                    q.append((dire, i, nxt_j))
                else:
                    break
        if dire == R:
            for length in range(1, k + 1):
                nxt_j = j + length
                if nxt_j < w and c[i][nxt_j] == "." and dist[dire][i][j] + 1 < dist[dire][i][nxt_j]:
                    dist[dire][i][nxt_j] = dist[dire][i][j] + 1
                    q.append((dire, i, nxt_j))
                else:
                    break

    return dist


h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
c = [input() for i in range(h)]


x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1
dist = bfs(x1, y1)
ans = min([dist[i][x2][y2] for i in range(4)])

if ans == 10 ** 18:
    print(-1)
else:
    print(ans)