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: Runtime Error

Solution code:
def main():
  import sys
  input=sys.stdin.readline
  h,w,k=map(int,input().split())
  sx,sy,tx,ty=map(int,input().split())
  calc=lambda x,y:x*1000007+y
  b=[[-1]*(w+2)]
  for i in range(h):
    s=input()
    bb=[-1]
    for j in s:
      if j==".":bb.append(0)
      else:bb.append(-1)
    bb.append(-1)
    b.append(bb)
  b.append([-1]*(w+2))
  from collections import deque
  d=deque()
  d.append(calc(sx,sy))
  s=set()
  while d:
    x,y=divmod(d.popleft(),1000007)
    if calc(x,y)in s:
      s.discard(calc(x,y))
      continue
    if x==tx and y==ty:
      exit(print(b[x][y]))
    for i in range(1,k+1):
      if b[x-i][y]<=b[x][y] and b[x-i][y]!=0:break
      d.append(calc(x-i,y))
      if b[x-i][y]!=0:
        s.add(clac(x-i,y))
      b[x-i][y]=b[x][y]+1
    for i in range(1,k+1):
      if b[x+i][y]<=b[x][y] and b[x+i][y]!=0:break
      d.append(calc(x+i,y))
      if b[x+i][y]!=0:
        s.add(calc(x+i,y))
      b[x+i][y]=b[x][y]+1
    for j in range(1,k+1):
      if b[x][y-j]<=b[x][y] and b[x][y-j]!=0:break
      d.append(calc(x,y-j))
      if b[x][y-j]!=0:
        s.add(calc(x,y-j))
      b[x][y-j]=b[x][y]+1
    for j in range(1,k+1):
      if b[x][y+j]<=b[x][y] and b[x][y+j]!=0:break
      d.append(calc(x,y+j))
      if b[x][y+j]!=0:
        s.add(calc(x,y+j))
      b[x][y+j]=b[x][y]+1
  print(-1)
if __name__ == "__main__":
  main()

Submission status: Runtime Error

Solution code:
h, w, k = map(int, input().split())
x1, y1, x2, y2 = [int(i)-1 for i in input().split()]
ans = [[-1] * w for _ in range(h)]
ans[x1][y1] = 0
c = [input() for _ in range(h)]
qu = [(x1, y1)]


for x, y in qu:
    if x == x2 and y == y2:
        print(ans[x2][y2])
        break

    for i in range(x + 1, min(x + k + 1, h)):
        if c[i][y] == "@":
            break
        if ans[i][y] == -1:
            ans[i][y] = ans[x][y] + 1
            qu.append((i, y))
    for i in reversed(range(max(0, x - k), x)):
        if c[i][y] == "@":
            break
        if ans[i][y] == -1:
            ans[i][y] = ans[x][y] + 1
            qu.append((i, y))

    for i in range(y + 1, min(y + k + 1, w)):
        if c[x][i] == "@":
            break
        if ans[x][i] == -1:
            ans[x][i] = ans[x][y] + 1
            qu.append((x, i))
    for i in reversed(range(max(0, y - k), x)):
        if c[x][i] == "@":
            break
        if ans[x][i] == -1:
            ans[x][i] = ans[x][y] + 1
            qu.append((x, i))
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
                      
h,w,k = map(int,input().split())
x1,y1,x2,y2 = map(int,input().split())
c = []
for i in range(h):
  c.append(list(input()))

def dfs(x,y,kk,d,a):
  #print(kk,d)
  if c[x][y] == '.':
    c[x][y] = a
  else:
    c[x][y] = min(c[x][y],a)
  for dx,dy in ((-1,0),(1,0),(0,-1),(0,1)):
    if 0 <= x+dx < h and 0 <= y+dy < w and (c[x+dx][y+dy] == '.' or (c[x+dx][y+dy] != '@' and c[x+dx][y+dy] > a)):
      if kk == -1:
        kk = k-1
        a = 1
      elif kk == 0 or d != (dx,dy):
        kk = k
        a += 1
      else:
        kk -= 1
      dfs(x+dx,y+dy,kk,(dx,dy),a)

dfs(x1-1,y1-1,-1,(),0)
print(-1 if c[x2-1][y2-1] == '.' else c[x2-1][y2-1])
#print(c)

Submission status: Runtime Error

Solution code:
from collections import deque
import sys

H,W,K=map(int,input().split())
x,y,u,v=map(int,input().split())
C=[[]]
for _ in range(H):
    C.append("*"+input())

Q=deque([(x,y)])
S=set((x,y))
D={(x,y):0}

while Q:
    (p,q)=Q.popleft()

    N=[]
    for m in range(1,K+1):
        X=p-m
        if 1<=X<=H:
            if (C[X][q]=="."):
                if (X,q) not in S:
                    N.append((X,q))
                    S.add((X,q))
            else:
                break
        else:
            break

    for m in range(1,K+1):
        X=p+m
        if 1<=X<=H:
            if (C[X][q]=="."):
                if (X,q) not in S:
                    N.append((X,q))
                    S.add((X,q))
            else:
                break
        else:
            break

    for m in range(1,K+1):
        Y=q-m
        if 1<=Y<=W:
            if (C[p][Y]=="."):
                if (p,Y) not in S:
                    N.append((p,Y))
                    S.add((p,Y))
            else:
                break
        else:
            break

    for m in range(1,K+1):
        Y=q+m
        if 1<=Y<=W:
            if (C[p][Y]=="."):
                if (p,Y) not in S:
                    N.append((p,Y))
                    S.add((p,Y))
            else:
                break
        else:
            break

    d=D[(p,q)]
    for (s,t) in N:
        Q.append((s,t))
        D[(s,t)]=d+1

    if (u,v) in D:
        print(D[(u,v)])
        sys.exit()

print(-1)
while True:
    j=2/0

Submission status: Runtime Error

Solution code:
import sys
from collections import deque

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    h, w, k = nm()
    sy, sx, gy, gx = nm()
    d = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    sx -= 1; sy -= 1
    gx -= 1; gy -= 1
    C = [ns() for _ in range(w)]
    G = [[[10**10]*w for _ in range(h)] for i in range(4)]
    for i in range(4):
        G[i][sy][sx] = 0
    q = deque([(sy, sx, i, 0) for i in range(4)])
    while q:
        y, x, t, c = q.popleft()
        if c:
            dy, dx = d[t]
            ny = y + dy; nx = x + dx
            if 0 <= ny < h and 0 <= nx < w and C[ny][nx] != '@' and G[t][ny][nx] > G[t][y][x]:
                G[t][ny][nx] = G[t][y][x]
                q.appendleft((ny, nx, t, c-1))
        for i in range(4):
            dy, dx = d[i]
            ny = y + dy; nx = x + dx
            if 0 <= ny < h and 0 <= nx < w and C[ny][nx] != '@' and G[i][ny][nx] > G[t][y][x] + 1:
                G[i][ny][nx] = G[t][y][x] + 1
                q.append((ny, nx, i, k-1))
    ans = min(G[i][gy][gx] for i in range(4))
    print(ans if ans < 10**10 else -1)
    return

solve()

Submission status: Runtime Error

Solution code:
import sys
from collections import deque

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    h, w, k = nm()
    sy, sx, gy, gx = nm()
    d = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    sx -= 1; sy -= 1
    gx -= 1; gy -= 1
    C = [ns() for _ in range(w)]
    G = [[[10**10]*w for _ in range(h)] for i in range(4)]
    for i in range(4):
        G[i][sy][sx] = 0
    q = deque([(sy, sx, i, 0) for i in range(4)])
    while q:
        y, x, t, c = q.popleft()
        dy, dx = d[t]
        ny = y + dy; nx = x + dx
        if 0 <= ny < h and 0 <= nx < w and C[ny][nx] != '@' and G[t][ny][nx] > G[t][y][x] + (c == 0):
            G[t][ny][nx] = G[t][y][x] + (c == 0)
            if c:
                q.appendleft((ny, nx, t, c-1))
            else:
                q.append((ny, nx, t, k-1))
        for i in range(4):
            if i == t:
                continue
            dy, dx = d[i]
            ny = y + dy; nx = x + dx
            if 0 <= ny < h and 0 <= nx < w and C[ny][nx] != '@' and G[i][ny][nx] > G[t][y][x] + 1:
                G[i][ny][nx] = G[t][y][x] + 1
                q.append((ny, nx, i, k-1))
    ans = min(G[i][gy][gx] for i in range(4))
    print(ans if ans < 10**10 else -1)
    return

solve()