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:
import sys
from collections import deque as dq
input = sys.stdin.readline
H, W, K = map(int, input().split())
x, y, u, v = map(int, input().split())
x -= 1
y -= 1
u -= 1
v -= 1
a = [list(input())[: -1] for _ in range(H)]
Q = dq([x * W + y])
inf = H * W + 1
dp = [inf] * (H * W)
dp[x * W + y] = 0
while len(Q):
  t = Q.popleft()
  p = t // W
  q = t % W
  if (p, q) == (u, v):
    print(dp[t])
    exit(0)
  for i in range(1, K + 1):
    pp = p + i
    if pp in range(H):
      if a[pp][q] == "@": break
      if dp[pp * W + q] <= dp[t] + 1: break
      dp[pp * W + q] = dp[t] + 1
      Q.append(pp * W + q)
  for i in range(1, K + 1):
    pp = p - i
    if pp in range(H):
      if a[pp][q] == "@": break
      if dp[pp * W + q] <= dp[t] + 1: break
      dp[pp * W + q] = dp[t] + 1
      Q.append(pp * W + q)

  for i in range(1, K + 1):
    qq = q + i
    if qq in range(W):
      if a[p][qq] == "@": break
      if dp[p * W + qq] <= dp[t] + 1: break
      dp[p * W + qq] = dp[t] + 1
      Q.append(p * W + qq)

  for i in range(1, K + 1):
    qq = q - i
    if qq in range(W):
      if a[p][qq] == "@": break
      if dp[p * W + qq] <= dp[t] + 1: break
      dp[p * W + qq] = dp[t] + 1
      Q.append(p * W + qq)
print(-1)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque


def bfs(x1, y1, d):
    q = deque([])
    q.append((d, x1, y1))
    M[x1][y1] = d

    while q:
        d, x1, y1 = q.popleft()

        if [x1, y1] == [xg, yg]:
            print(d)
            return

        for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
            for k in range(1, K + 1):
                x2 = x1 + dx * k
                y2 = y1 + dy * k
                # if m[x2][y2] == '@':
                #     break
                # if x2 < 0 or x2 >= H:
                #     break
                # if y2 < 0 or y2 >= W:
                #     break
                # if M[x2][y2] < d + 1:
                #     break
                # if M[x2][y2] == d + 1:
                #     continue

                if (0 <= x2 < H) and (0 <= y2 < W):
                    if m[x2][y2] == '@':
                        break
                    elif M[x2][y2] == -1:
                        M[x2][y2] = d + 1
                        q.append((d + 1, x2, y2))
                    elif M[x2][y2] <= d:
                        break
                    # elif M[x2][y2] == d + 1:
                    #     continue
                    else:
                        break
                else:
                    break
    print(-1)


H, W, K = map(int, sys.stdin.readline().strip().split())
xs, ys, xg, yg = map(int, sys.stdin.readline().strip().split())
xg -= 1
yg -= 1

m = []
for _ in range(H):
    m.append(list(map(str, sys.stdin.readline().strip())))

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

bfs(xs - 1, ys - 1, 0)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque


def bfs(x1, y1, d):
    q = deque([])
    q.append((x1, y1))
    M[x1][y1] = d

    while q:
        x1, y1 = q.popleft()

        if [x1, y1] == [xg, yg]:
            print(d)
            return

        for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
            for k in range(1, K + 1):
                x2 = x1 + dx * k
                y2 = y1 + dy * k
                # if m[x2][y2] == '@':
                #     break
                # if x2 < 0 or x2 >= H:
                #     break
                # if y2 < 0 or y2 >= W:
                #     break
                # if M[x2][y2] < d + 1:
                #     break
                # if M[x2][y2] == d + 1:
                #     continue

                if (0 <= x2 < H) and (0 <= y2 < W):
                    if m[x2][y2] == '@':
                        break
                    elif M[x2][y2] == -1:
                        M[x2][y2] = d + 1
                        q.append((x2, y2))
                    elif M[x2][y2] <= d:
                        break
                    elif M[x2][y2] == d + 1:
                        continue
                    else:
                        break
    print(-1)


H, W, K = map(int, sys.stdin.readline().strip().split())
xs, ys, xg, yg = map(int, sys.stdin.readline().strip().split())
xg -= 1
yg -= 1

m = []
for _ in range(H):
    m.append(list(map(str, sys.stdin.readline().strip())))

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

bfs(xs - 1, ys - 1, 0)

Submission status: Wrong Answer

Solution code:
from collections import deque
h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(lambda x: int(x)-1, input().split())

pond = []
for _ in range(h):
    pond.append(list(input()))

dD = [[10**13 for _ in range(w)] for _ in range(h)]
dU = [[10**13 for _ in range(w)] for _ in range(h)]
dR = [[10**13 for _ in range(w)] for _ in range(h)]
dL = [[10**13 for _ in range(w)] for _ in range(h)]

dD[x1][y1] = 0
dU[x1][y1] = 0
dR[x1][y1] = 0
dL[x1][y1] = 0

q = deque([((x1, y1), "D", 0), ((x1, y1), "U", 0),
           ((x1, y1), "R", 0), ((x1, y1), "L", 0)])


while q:
    v, direction, p = q.popleft()
    if v == (x2, y2):
        continue
    x = v[0]
    y = v[1]
    if 0 <= x + 1 < h and 0 <= y < w and pond[x+1][y] == '.' and direction == "D":
        if dD[x+1][y] > dD[x][y] + (p == 0):
            dD[x+1][y] = dD[x][y] + (p == 0)
            q.append(((x+1, y), "D", (p+1) % k))
    elif 0 <= x+1 < h and 0 <= y < w and pond[x+1][y] == '.':
        if dD[x+1][y] > dR[x][y] + 1:
            dD[x+1][y] = dR[x][y] + 1
            q.append(((x+1, y), "D", 1))
        if dD[x+1][y] > dL[x][y] + 1:
            dD[x+1][y] = dL[x][y] + 1
            q.append(((x+1, y), "D", 1))

    if 0 <= x - 1 < h and 0 <= y < w and pond[x-1][y] == '.' and direction == "U":
        if dU[x-1][y] > dU[x][y]+(p == 0):
            dU[x-1][y] = dU[x][y]+(p == 0)
            q.append(((x-1, y), "U", (p+1) % k))
    elif 0 <= x-1 < h and 0 <= y < w and pond[x-1][y] == '.':
        if dU[x-1][y] > dR[x][y] + 1:
            dU[x-1][y] = dR[x][y] + 1
            q.append(((x-1, y), "U", 1))
        if dU[x-1][y] > dL[x][y] + 1:
            dU[x-1][y] = dL[x][y] + 1
            q.append(((x-1, y), "U", 1))

    if 0 <= x < h and 0 <= y+1 < w and pond[x][y+1] == '.' and direction == "R":
        if dR[x][y+1] > dR[x][y] + (p == 0):
            dR[x][y+1] = dR[x][y] + (p == 0)
            q.append(((x, y+1), "R", (p+1) % k))
    elif 0 <= x < h and 0 <= y+1 < w and pond[x][y+1] == '.':
        if dR[x][y+1] > dD[x][y] + 1:
            dR[x][y+1] = dD[x][y] + 1
            q.append(((x, y+1), "R", 1))
        if dR[x][y+1] > dU[x][y] + 1:
            dR[x][y+1] = dU[x][y] + 1
            q.append(((x, y+1), "R", 1))

    if 0 <= x < h and 0 <= y-1 < w and pond[x][y-1] == '.' and direction == "L":
        if dL[x][y-1] > dL[x][y] + (p == 0):
            dL[x][y-1] = dL[x][y] + (p == 0)
            q.append(((x, y-1), "L", (p+1) % k))
    elif 0 <= x < h and 0 <= y-1 < w and pond[x][y-1] == '.':
        if dL[x][y-1] > dD[x][y] + 1:
            dL[x][y-1] = dD[x][y] + 1
            q.append(((x, y-1), "L", 1))
        if dL[x][y-1] > dU[x][y] + 1:
            dL[x][y-1] = dU[x][y] + 1
            q.append(((x, y-1), "L", 1))

ans = min(dD[x2][y2], dU[x2][y2], dR[x2][y2], dL[x2][y2])
print(-1 if ans == 10**13 else ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque


def bfs(x1, y1, d):
    q = deque([])
    q.append((x1, y1))

    while q:
        x1, y1 = q.popleft()

        M[x1][y1] = d

        if [x1, y1] == [xg, yg]:
            print(d)
            return

        for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
            for k in range(1, K + 1):
                x2 = x1 + dx * k
                y2 = y1 + dy * k
                # if m[x2][y2] == '@':
                #     break
                # if x2 < 0 or x2 >= H:
                #     break
                # if y2 < 0 or y2 >= W:
                #     break
                # if M[x2][y2] < d + 1:
                #     break
                # if M[x2][y2] == d + 1:
                #     continue

                if (0 <= x2 < H) and (0 <= y2 < W):
                    if m[x2][y2] == '@':
                        break
                    elif M[x2][y2] == -1:
                        M[x2][y2] = d + 1
                        q.append((x2, y2))
                    elif M[x2][y2] <= d:
                        break
                    elif M[x2][y2] == d + 1:
                        continue
                    else:
                        break
    print(-1)


H, W, K = map(int, sys.stdin.readline().strip().split())
xs, ys, xg, yg = map(int, sys.stdin.readline().strip().split())
xg -= 1
yg -= 1

m = []
for _ in range(H):
    m.append(list(map(str, sys.stdin.readline().strip())))

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

bfs(xs - 1, ys - 1, 0)

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys


def bfs(x1, y1, d):
    q = deque()
    q.append((d, x1, y1))

    while q:
        d, x1, y1 = q.popleft()

        M[x1][y1] = d

        if [x1, y1] == [xg, yg]:
            print(d)
            return

        for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
            for k in range(1, K + 1):
                x2 = x1 + dx * k
                y2 = y1 + dy * k

                if (0 <= x2 < H) and (0 <= y2 < W):
                    if m[x2][y2] == '@':
                        break
                    elif M[x2][y2] == -1:
                        M[x2][y2] = d + 1
                        q.append((d + 1, x2, y2))
                    elif M[x2][y2] < d + 1:
                        break
                else:
                    break


H, W, K = map(int, input().split())
# K = min(K, max(H, W))
xs, ys, xg, yg = map(int, input().split())
xs, ys, xg, yg = xs - 1, ys - 1, xg - 1, yg - 1

m = []
for i in range(H):
    m.append(list(map(str, sys.stdin.readline().strip())))

M = [[-1] * W for i in range(H)]

bfs(xs, ys, 0)

print(M[xg][yg])

Submission status: Wrong Answer

Solution code:
from collections import deque, defaultdict
from heapq import heappop, heappush
import sys
from math import ceil
input = sys.stdin.readline


H, W, K = map(int, input().split())
y1, x1, y2, x2 = map(int, input().split())
l = [input() for _ in range(H)]
INF = 10**12

x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1

ddy = [0, 0, -1, 1]
ddx = [-1, 1, 0, 0]
source = (y1, x1, -1)
INF = 10**12

dist = defaultdict(lambda :INF)
dist[source] = 0
q = [(0, source)]

while q:
    cost, (y, x, direction) = heappop(q)
    u = (y, x, direction)
    if dist[u] < cost:
        continue
    for i in range(4):
        dy, dx = ddy[i], ddx[i]
        ny, nx = y+dy, x+dx
        if (not (0<=ny<H and 0<=nx<W)) or l[ny][nx]=="@":continue

        if direction==-1 or direction==i:
            v = (ny, nx, i)
            c = 1/K
            if dist[v] > dist[u] + c:
                dist[v] = dist[u] + c
                heappush(q, (dist[v], v))
        else:
            v = (y, x, i)
            if dist[v] > ceil(dist[u]):
                dist[v] = ceil(dist[u])
                heappush(q, (dist[v], v))


ans = 10**9
for i in range(4):
    ans = min(ans, ceil(dist[(y2, x2, i)]))
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
H,W,K = map(int,input().split())
s1,e1,s2,e2 = map(int,input().split())
s1-=1
e1-=1
s2-=1
e2-=1
L = []
board = []
for _ in range(H):
    a = input()
    L.append(a)
for i in range(H):
    l = L[i]
    tmp = []
    for j in range(len(l)):
        tmp.append(l[j])
    board.append(tmp)

hq = []
heapq.heappush(hq,[s1,e1,0])
seen = [[False]*(W) for _ in range(H)]
destination = [(1,0),(-1,0),(0,1),(0,-1)]
while hq:
    x,y,cost = heapq.heappop(hq)
    seen[x][y] = True
    if x==s2 and y ==e2:
        print(cost);exit()
        break
    for dx,dy in destination:
        X,Y = x,y
        for i in range(K):
            X +=dx
            Y += dy
            if 0>X or X>=H or 0>Y or Y>=W: break
            else:
                if board[X][Y]=='@':
                    break
                elif not seen[X][Y]:
                    heapq.heappush(hq,[X,Y,cost+1])
                    seen[X][Y] = True

print(-1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
import heapq

h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(lambda x:int(x)-1, input().split())
S = [input().rstrip() for _ in range(h)]
dist = [[[float("inf")]*w  for i in range(h)] for _ in range(4)]
que = []
que.append((0, 0, x1, y1))
dist[0][x1][y1] = 0
  
while que:
  c, di, x, y = heapq.heappop(que)
  if dist[di][x][y] < c:
    continue
  l, r = c+1, -(-c//k)*k
  if di == 0:
    if x > 0 and S[x-1][y] == "." and dist[di][x-1][y] > l:
      dist[di][x-1][y] = l
      heapq.heappush(que, (l, di, x-1, y))
  elif di == 1:
    if y > 0 and S[x][y-1] == "." and dist[di][x][y-1] > l:
      dist[di][x][y-1] = l
      heapq.heappush(que, (l, di, x, y-1))
  elif di == 2:
    if x < h-1 and S[x+1][y] == "." and dist[di][x+1][y] > l:
      dist[di][x+1][y] = l
      heapq.heappush(que, (l, di, x+1, y))
  else:
    if y < w-1 and S[x][y+1] == "." and dist[di][x][y+1] > l:
      dist[di][x][y+1] = l
      heapq.heappush(que, (l, di, x, y+1))
      
  if di%2:
    if dist[0][x][y] > r:
      dist[0][x][y] = r
      heapq.heappush(que, (r, 0, x, y))
    if dist[2][x][y] > r:
      dist[2][x][y] = r
      heapq.heappush(que, (r, 2, x, y))
    
  else:
    if dist[1][x][y] > r:
      dist[1][x][y] = r
      heapq.heappush(que, (r, 1, x, y))
    if dist[3][x][y] > r:
      dist[3][x][y] = r
      heapq.heappush(que, (r, 3, x, y))
ans = -(-min(dist[0][x2][y2], dist[1][x2][y2], dist[2][x2][y2], dist[3][x2][y2]))
if ans == float("inf"):
  print(-1)
else:
  ans //= k
  print(ans)

Submission status: Wrong Answer

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


def solve():
    H, W, K = map(int, input().split())
    y1, x1, y2, x2 = map(int, input().split())
    l = [[1 if s=="@" else 0 for s in input()] for _ in range(H)]
    INF = 10**12

    x1 -= 1
    y1 -= 1
    x2 -= 1
    y2 -= 1

    ddy = [0, 0, -1, 1]
    ddx = [-1, 1, 0, 0]

    dist = defaultdict(lambda :INF)
    dist[(y1, x1)] = 0
    q = deque([(y1, x1)])

    while q:
        y, x = q.popleft()
        for i in range(4):
            dx = ddx[i]
            dy = ddy[i]
            for j in range(1, K+1):
                if not (0<=y+dy*j<H and 0<=x+dx*j<W):
                    break
                if l[y+dy*j][x+dx*j]:
                    break

                if dist[(y+dy*j, x+dx*j)] > dist[(y, x)] + 1:
                    dist[(y+dy*j, x+dx*j)] = dist[(y, x)] + 1
                    q.append((y+dy*j, x+dx*j))
                else:
                    break


    ans = dist[(y2, x2)]
    print(ans if ans!=INF else -1)


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    inf=10**9
    h,w,k=MI()
    x1,y1,x2,y2=MI1()
    aa=[[0 if c=="@" else inf for c in SI()] for _ in range(h)]
    p2D(aa)
    aa[x1][y1]=0
    q=deque()
    q.append((x1,y1,0))
    while q:
        x,y,d=q.popleft()
        for nx in range(x+1,min(x+k+1,h)):
            if aa[nx][y]<d+1:break
            aa[nx][y]=d+1
            q.append((nx,y,d+1))
        for nx in range(x-1,max(x-k-1,-1),-1):
            if aa[nx][y]<d+1:break
            aa[nx][y]=d+1
            q.append((nx,y,d+1))
        for ny in range(y+1,min(y+k+1,w)):
            if aa[x][ny]<d+1:break
            aa[x][ny]=d+1
            q.append((x,ny,d+1))
        for ny in range(y-1,max(y-k-1,-1),-1):
            if aa[x][ny]<d+1:break
            aa[x][ny]=d+1
            q.append((x,ny,d+1))
        #p2D(aa)
        #print()

    ans=aa[x2][y2]
    if ans==inf:print(-1)
    else:print(ans)

main()

Submission status: Wrong Answer

Solution code:
from collections import deque

he,w,k = map(int,input().split())
sx,sy,gx,gy = map(int,input().split())
l = [list(input()) for i in range(he)]

sx -= 1
sy -= 1
gx -= 1
gy -= 1
q = deque([])
d = [[float("INF")]*w for i in range(he)]

d[sx][sy] = 0
q.append((0,sx,sy))
while q:
    dis,bx,by = q.popleft()
    if bx == gx and by == gy:
        continue
    for i in range(1,k+1):
        if 0 <= bx+i < he and l[bx+i][by] != "@" and d[bx+i][by] > dis+1:
            d[bx+i][by] = dis+1
            q.append((dis+1,bx+i,by))
        else:
            break
    for i in range(1,k+1):
        if 0 <= bx-i < he and l[bx-i][by] != "@" and d[bx-i][by] > dis+1:
            d[bx-i][by] = dis+1
            q.append((dis+1,bx-i,by))
        else:
            break
    for i in range(1,k+1):
        if 0 <= by+i < w and l[bx][by+i] != "@" and d[bx][by+i] > dis+1:        
            d[bx][by+i] = dis+1
            q.append((dis+1,bx,by+i))
        else:
            break
    for i in range(1,k+1):
        if 0 <= by-i < w and l[bx][by-i] != "@" and d[bx][by-i] > dis+1:
            d[bx][by-i] = dis+1
            q.append((dis+1,bx,by-i))
        else:
            break

if d[gx][gy] == float("INF"):
    print(-1)
else:
    print(d[gx][gy])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return map(int, input().split())
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    mxy = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    h, w, K = LI()
    y1, x1, y2, x2 = LI()
    q = deque()
    field = [[False] * (w + 2)] + [None] * h + [[False] * (w + 2)]
    for i in range(1, h + 1):
        field[i] = [False] + [True] * w + [False]
    for i in range(1, h + 1):
        s = input().rstrip()
        for j in range(1, w + 1):
            if s[j-1] == "@":
                field[i][j] = False
    return
    q.append((x1, y1))
    ans = 0
    c = defaultdict(int)
    while q:
        next_q = deque()
        while q:
            x, y = q.popleft()
            if (x, y) == (x2, y2):
                print(ans)
                return
            if field[y][x]:
                for i in range(1, K + 1):
                    if field[y + i][x]:
                        if not c[(y + i, x)]:
                            next_q.append((x, y + i))
                            c[(y + i, x)] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x + i]:
                        if not c[(y, x + i)]:
                            next_q.append((x + i, y))
                            c[(y, x + i)] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y - i][x]:
                        if not c[(y - i, x)]:
                            next_q.append((x, y - i))
                            c[(y - i, x)] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x - i]:
                        if not c[(y, x - i)]:
                            next_q.append((x - i, y))
                            c[(y, x - i)] = 1
                    else:
                        break
                field[y][x] = False
        ans += 1
        q = next_q
    print(-1)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
from collections import deque

he,w,k = map(int,input().split())
sx,sy,gx,gy = map(int,input().split())
l = [list(input()) for i in range(he)]

sx -= 1
sy -= 1
gx -= 1
gy -= 1
q = deque([])
d = [[float("INF")]*w for i in range(he)]

d[sx][sy] = 0
q.append((0,sx,sy))
while q:
    dis,bx,by = q.popleft()
    if bx == gx and by == gy:
        print(d[gx][gy])
        exit()
    for i in range(1,k+1):
        if 0 <= bx+i < he and l[bx+i][by] != "@" and d[bx+i][by] > dis+1:
            d[bx+i][by] = dis+1
            q.append((dis+1,bx+i,by))
        else:
            break
    for i in range(1,k+1):
        if 0 <= bx-i < he and l[bx-i][by] != "@" and d[bx-i][by] > dis+1:
            d[bx-i][by] = dis+1
            q.append((dis+1,bx-i,by))
        else:
            break
    for i in range(1,k+1):
        if 0 <= by+i < w and l[bx][by+i] != "@" and d[bx][by+i] > dis+1:        
            d[bx][by+i] = dis+1
            q.append((dis+1,bx,by+i))
        else:
            break
    for i in range(1,k+1):
        if 0 <= by-i < w and l[bx][by-i] != "@" and d[bx][by-i] > dis+1:
            d[bx][by-i] = dis+1
            q.append((dis+1,bx,by-i))
        else:
            break

if d[gx][gy] == float("INF"):
    print(-1)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return map(int, input().split())
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    mxy = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    h, w, K = LI()
    y1, x1, y2, x2 = LI_()
    c = SR(h)
    q = deque()
    q.append((x1, y1, 0, 0))
    q.append((x1, y1, 1, 0))
    q.append((x1, y1, 2, 0))
    q.append((x1, y1, 3, 0))
    field = [None] * h
    for i in range(h):
        field[i] = [inf] * w
    field[y1][x1] = 0
    while q:
        x, y, d, k = q.popleft()
        mx, my = mxy[d]
        mx += x
        my += y
        if 0 <= my < h and 0 <= mx < w:
            if c[my][mx] == "@": continue
            s = field[y][x] + (k == 0)
            k += 1
            if K == k:
                if field[my][mx] > s:
                    field[my][mx] = s
                    q.append((mx, my, 0, 0))
                    q.append((mx, my, 1, 0))
                    q.append((mx, my, 2, 0))
                    q.append((mx, my, 3, 0))
            else:
                if field[my][mx] > s:
                    field[my][mx] = s
                    for i in range(4):
                        if i == d:
                            q.appendleft((mx, my, d, k))
                        else:
                            q.append((mx, my, i, 0))
    print(field[y2][x2] if field[y2][x2] != inf else -1)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return map(int, input().split())
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    mxy = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    h, w, K = LI()
    y1, x1, y2, x2 = LI_()
    c = SR(h)
    q = deque()
    q.append((x1, y1, 0, 0))
    q.append((x1, y1, 1, 0))
    q.append((x1, y1, 2, 0))
    q.append((x1, y1, 3, 0))
    field = [None] * h
    for i in range(h):
        field[i] = [inf] * w
    field[y1][x1] = 0
    while q:
        x, y, d, k = q.popleft()
        mx, my = mxy[d]
        mx += x
        my += y
        if 0 <= my < h and 0 <= mx < w:
            if c[my][mx] == "@": continue
            s = field[y][x] + (k == 0)
            k += 1
            if k > K:
                k = 0
                if field[my][mx] > s:
                    field[my][mx] = s
                    q.append((mx, my, 0, 0))
                    q.append((mx, my, 1, 0))
                    q.append((mx, my, 2, 0))
                    q.append((mx, my, 3, 0))
            else:
                if field[my][mx] > s:
                    field[my][mx] = s
                    for i in range(4):
                        if i == d:
                            q.appendleft((mx, my, d, k))
                        else:
                            q.append((mx, my, i, 0))
    print(field[y2][x2] if field[y2][x2] != inf else -1)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return map(int, input().split())
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    mxy = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    h, w, K = LI()
    y1, x1, y2, x2 = LI_()
    c = SR(h)
    q = deque()
    q.append((x1, y1, 0, 0))
    q.append((x1, y1, 1, 0))
    q.append((x1, y1, 2, 0))
    q.append((x1, y1, 3, 0))
    field = [None] * h
    for i in range(h):
        field[i] = [inf] * w
    field[y1][x1] = 0
    while q:
        x, y, d, k = q.popleft()
        mx, my = mxy[d]
        mx += x
        my += y
        if 0 <= my < h and 0 <= mx < w:
            if c[my][mx] == "@": continue
            s = field[y][x] + (k == 0)
            k += 1
            if k > K:
                s += 1
                k = 0
                if field[my][mx] > s:
                    field[my][mx] = s
                    q.append((mx, my, 0, k))
                    q.append((mx, my, 1, k))
                    q.append((mx, my, 2, k))
                    q.append((mx, my, 3, k))
            else:
                if field[my][mx] > s:
                    field[my][mx] = s
                    for i in range(4):
                        if i == d:
                            q.appendleft((mx, my, d, k))
                        else:
                            q.append((mx, my, i, 0))
    print(field[y2][x2] if field[y2][x2] != inf else -1)
    return


#main
if __name__ == '__main__':
    solve()