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 defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gcd
from operator import mul
from functools import reduce
from operator import mul
from pprint import pprint



sys.setrecursionlimit(2147483647)
INF = 10 ** 2
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007

h, w, k = LI()
sy, sx, gy, gx = LI()
cost = [[INF] * w for _ in range(h)]
cost[sy - 1][sx - 1] = 0
grid = SR(h)
q = deque([(sy - 1, sx - 1)])
while q:
    cy, cx = q.popleft()
    for i, j in ((1, 0), (0, 1), (-1, 0), (0, -1)):
        for l in range(1, k + 1):
            ny, nx = cy + i * l, cx + j * l
            if 0 <= ny < h and 0 <= nx < w and grid[ny][nx] == "." and cost[ny][nx] >= cost[cy][cx] + 1:
                if cost[ny][nx] == INF:
                    cost[ny][nx] = cost[cy][cx] + 1
                    q += [(ny, nx)]
            else:
                break


print(cost[gy - 1][gx - 1] if cost[gy - 1][gx - 1] != INF else -1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gcd
from operator import mul
from functools import reduce
from operator import mul
from pprint import pprint



sys.setrecursionlimit(2147483647)
INF = 10 ** 2
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007

h, w, k = LI()
sy, sx, gy, gx = LI()
cost = [[INF] * w for _ in range(h)]
cost[sy - 1][sx - 1] = 0
grid = SR(h)
q = deque([(sy - 1, sx - 1)])
while q:
    cy, cx = q.popleft()
    for i, j in ((1, 0), (0, 1), (-1, 0), (0, -1)):
        for l in range(1, k + 1):
            ny, nx = cy + i * l, cx + j * l
            if 0 <= ny < h and 0 <= nx < w and grid[ny][nx] == "." and cost[ny][nx] >= cost[cy][cx] + 1:
                if cost[ny][nx] > cost[cy][cx] + 1:
                    cost[ny][nx] = cost[cy][cx] + 1
                    q += [(ny, nx)]
            else:
                break


print(cost[gy - 1][gx - 1] if cost[gy - 1][gx - 1] != INF else -1)

Submission status: Wrong Answer

Solution code:
h,w,k=map(int,input().split())
x1,y1,x2,y2=map(lambda x:int(x)-1,input().split())
cmap=[input() for _ in range(h)]
from math import ceil
from heapq import heappop,heappush,heapify
dv={'w':0,'n':1,'e':2,'s':3}
seen=[[[0]*4 for _ in range(w)] for _ in range(h)]
todo=[[0,x1,y1,'w']]

heapify(todo)
d=[[-1]*w for _ in range(h)]
while todo:
  c,x,y,v=heappop(todo)
  if seen[x][y][dv[v]]==1:continue
  seen[x][y][dv[v]]=1
  d[x][y]=c
  if (x,y)==(x2,y2):
    print(ceil(c))
    #for di in d:
    #  print(di)
    exit()
  if v=='w':
    if seen[x][y][dv['n']]==0:heappush(todo,[ceil(c),x,y,'n'])
    if y>0 and seen[x][y-1][dv['w']]==0 and cmap[x][y-1]!='@':heappush(todo,[c+1/k,x,y-1,'w'])
  elif v=='e':
    if seen[x][y][dv['s']]==0:heappush(todo,[ceil(c),x,y,'s'])
    if y<w-1 and seen[x][y+1][dv['e']]==0 and cmap[x][y+1]!='@':heappush(todo,[c+1/k,x,y+1,'e'])
  elif v=='n':
    if seen[x][y][dv['e']]==0:heappush(todo,[ceil(c),x,y,'e'])
    if x>0 and seen[x-1][y][dv['n']]==0 and cmap[x-1][y]!='@':heappush(todo,[c+1/k,x-1,y,'n'])
  elif v=='s':
    if seen[x][y][dv['w']]==0:heappush(todo,[ceil(c),x,y,'w'])
    if x<h-1 and seen[x+1][y][dv['s']]==0 and cmap[x+1][y]!='@':heappush(todo,[c+1/k,x+1,y,'s'])

print(-1)
#print(d)

Submission status: Wrong Answer

Solution code:
h,w,k=map(int,input().split())
x1,y1,x2,y2=map(lambda x:int(x)-1,input().split())
cmap=[input() for _ in range(h)]
from math import ceil
from heapq import heappop,heappush,heapify
dv={'w':0,'n':1,'e':2,'s':3}
seen=[[[0]*4 for _ in range(w)] for _ in range(h)]
todo=[[0,x1,y1,'w'],[0,x1,y1,'n'],[0,x1,y1,'e'],[0,x1,y1,'s']]

heapify(todo)
d=[[0]*w for _ in range(h)]
while todo:
  c,x,y,v=heappop(todo)
  if seen[x][y]==1:continue
  seen[x][y][dv[v]]=1
  d[x][y]=c
  if (x,y)==(x2,y2):
    print(ceil(c))
    #for di in d:
    #  print(di)
    exit()
  if v=='w':
    if seen[x][y][dv['n']]==0:heappush(todo,[ceil(c),x,y,'n'])
    if seen[x][y][dv['s']]==0:heappush(todo,[ceil(c),x,y,'s'])
    if y>0 and seen[x][y-1][dv['w']]==0 and cmap[x][y-1]!='@':heappush(todo,[c+1/k,x,y-1,'w'])
  elif v=='e':
    if seen[x][y][dv['n']]==0:heappush(todo,[ceil(c),x,y,'n'])
    if seen[x][y][dv['s']]==0:heappush(todo,[ceil(c),x,y,'s'])
    if y<w-1 and seen[x][y+1][dv['e']]==0 and cmap[x][y+1]!='@':heappush(todo,[c+1/k,x,y+1,'e'])
  elif v=='n':
    if seen[x][y][dv['w']]==0:heappush(todo,[ceil(c),x,y,'w'])
    if seen[x][y][dv['e']]==0:heappush(todo,[ceil(c),x,y,'e'])
    if x>0 and seen[x-1][y][dv['n']]==0 and cmap[x-1][y]!='@':heappush(todo,[c+1/k,x-1,y,'n'])
  elif v=='s':
    if seen[x][y][dv['w']]==0:heappush(todo,[ceil(c),x,y,'w'])
    if seen[x][y][dv['e']]==0:heappush(todo,[ceil(c),x,y,'e'])
    if x<h-1 and seen[x+1][y][dv['s']]==0 and cmap[x+1][y]!='@':heappush(todo,[c+1/k,x+1,y,'s'])

print(-1)
#print(d)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gcd
from operator import mul
from functools import reduce
from operator import mul
import pprint



sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007

h, w, k = LI()
sy, sx, gy, gx = LI()
cost = [[[INF] * 4 for _ in range(w)] for _ in range(h)]
checked = [[[0] * 4 for _ in range(w)] for _ in range(h)]
D = {0:(1, 0), 1:(0, 1), 2:(-1, 0), 3:(0, -1)}
cost[sy - 1][sx - 1][0] = 0
grid = SR(h)
q = [(0, sy - 1, sx - 1, 0)]
while q:
    c, cy, cx, cd = heappop(q)
    # print(cy, cx, cd)
    if checked[cy][cx][cd]:
        continue
    checked[cy][cx][cd] = 1
    nc = c + 1 / k
    di, dj = D[cd]
    ny = cy + di
    nx = cx + dj
    if 0 <= ny < h and 0 <= nx < w and grid[ny][nx] == "." and cost[ny][nx][cd] > nc:
        cost[ny][nx][cd] = nc
        heappush(q, (nc, ny, nx, cd))
    for nd in range(4):
        if nd == cd:
            continue
        nc = ceil(c)
        if cost[cy][cx][nd] > nc:
            cost[cy][cx][nd] = nc
            heappush(q, (nc, cy, cx, nd))



if ceil(min(cost[gy - 1][gx - 1])) == INF:
    print(-1)
else:
    print(ceil(min(cost[gy - 1][gx - 1])))

Submission status: Wrong Answer

Solution code:
import sys

input=sys.stdin.readline

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

class Dijkstra():
    class Edge():
        def __init__(self, _to, _type):
            self.to = _to
            self.type=_type

    def __init__(self, V):
        self.G = [[] for i in range(V)]
        self._E = 0
        self._V = V

    @property
    def E(self):
        return self._E

    @property
    def V(self):
        return self._V

    def add(self, _from, _to, _type):
        self.G[_from].append(self.Edge(_to, _type))
        self._E += 1

    def shortest_path(self, s):
        import heapq
        que = []
        d = [float("inf")] * self.V
        d[s] = 0
        heapq.heappush(que, (0, s))

        while len(que) != 0:
            cost, v = heapq.heappop(que)
            if d[v] < cost: continue

            for i in range(len(self.G[v])):
                e = self.G[v][i]
                if e.type==0:
                    if d[e.to] > d[v]-d[v]%K+K*(d[v]%K>0):
                        d[e.to]=d[v]-d[v]%K+K*(d[v]%K>0)
                        heapq.heappush(que,(d[e.to],e.to))
                else:
                    if d[e.to] >d[v]+1:
                        d[e.to]=d[v]+1
                        heapq.heappush(que,(d[e.to],e.to))
        return d

x1,y1,x2,y2=map(int,input().split())
c=[input().rstrip() for i in range(H)]
pond=Dijkstra(4*H*W)
for i in range(H*W):
    h=i//W
    w=i%W
    if c[h][w]==".":
        if h!=0 and c[h-1][w]==".":
            pond.add(4*i,4*(i-W),1)
        if h!=H-1 and c[h+1][w]==".":
            pond.add(4*i+2,4*(i+W)+2,1)
        if w!=0 and c[h][w-1]==".":
            pond.add(4*i+3,4*(i-1)+3,1)
        if w!=W-1 and c[h][w+1]==".":
            pond.add(4*i+1,4*(i+1)+1,1)
        for j in range(4):
            pond.add(4*i+j,4*i+(j+1)%4,0)

Submission status: Wrong Answer

Solution code:
import sys

input=sys.stdin.readline

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

class Dijkstra():
    class Edge():
        def __init__(self, _to, _type):
            self.to = _to
            self.type=_type

    def __init__(self, V):
        self.G = [[] for i in range(V)]
        self._E = 0
        self._V = V

    @property
    def E(self):
        return self._E

    @property
    def V(self):
        return self._V

    def add(self, _from, _to, _type):
        self.G[_from].append(self.Edge(_to, _type))
        self._E += 1

    def shortest_path(self, s):
        import heapq
        que = []
        d = [float("inf")] * self.V
        d[s] = 0
        heapq.heappush(que, (0, s))

        while len(que) != 0:
            cost, v = heapq.heappop(que)
            if d[v] < cost: continue

            for i in range(len(self.G[v])):
                e = self.G[v][i]
                if e.type==0:
                    if d[e.to] > d[v]-d[v]%K+K*(d[v]%K>0):
                        d[e.to]=d[v]-d[v]%K+K*(d[v]%K>0)
                        heapq.heappush(que,(d[e.to],e.to))
                else:
                    if d[e.to] >d[v]+1:
                        d[e.to]=d[v]+1
                        heapq.heappush(que,(d[e.to],e.to))
        return d

x1,y1,x2,y2=map(int,input().split())
c=[input().rstrip() for i in range(H)]
pond=Dijkstra(4*H*W)

Submission status: Wrong Answer

Solution code:
from sys import stdin
def input():
    return stdin.readline().strip()

import heapq

def main():
    h, w, k = map(int, input().split())
    x1, y1, x2, y2 = map(int, input().split())

    # surround the square with '@'
    c = [['@'] * (w+2)]
    for _ in range(h):
        i = list('@' + input() + '@')
        c.append(i)
    c.append(['@'] * (w+2))

    # Dijkstra
    # seen[x][y] = the minimum cost of (x, y)
    # if seen[x][y] == -1, (x, y) is unresearched
    seen = [[-1] * (w+2) for _ in range(h+2)]

    # todo[i] == (cost, x, y, direction)
    todo = [(0, x1, y1, 0)]
    heapq.heapify(todo)

    # direction
    dx = [-1, 0, 1, 0]
    dy = [0, 1, 0, -1]

    while len(todo) > 0:
        cost, xi, yi, direction = heapq.heappop(todo)

        # special case
        if seen[xi][yi] != -1:
            if seen[xi][yi] == cost:
                xj, yj = xi + dx[direction], yi + dy[direction]
                if c[xj][yj] == '.' and seen[xj][yj] == -1:
                    heapq.heappush(todo, (cost+1, xj, yj, direction))
            continue
        seen[xi][yi] = cost

        # goal
        if xi == x2 and yi == y2:
            print((cost+k-1)//k)
            exit()
        
        # add
        for j in range(4):
            xj, yj= xi + dx[j], yi + dy[j]
            if c[xj][yj] == '.' and seen[xj][yj] == -1:
                if j == direction:
                    heapq.heappush(todo, (cost+1, xj, yj, j))
                else:
                    heapq.heappush(todo, ((cost+k-1)//k * k + 1, xj, yj, j))

    print(-1)

main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
def input():
    return stdin.readline().strip()

import heapq

def main():
    h, w, k = map(int, input().split())
    x1, y1, x2, y2 = map(int, input().split())

    # surround the square with '@'
    c = [['@'] * (w+2)]
    for _ in range(h):
        i = list('@' + input() + '@')
        c.append(i)
    c.append(['@'] * (w+2))

    # Dijkstra
    # seen[x][y] = the minimum cost of (x, y)
    # if seen[x][y] == -1, (x, y) is unresearched
    seen = [[-1] * (w+2) for _ in range(h+2)]

    # todo[i] == (cost, x, y, direction)
    todo = [(0, x1, y1, 0)]
    heapq.heapify(todo)

    # direction
    dx = [-1, 0, 1, 0]
    dy = [0, 1, 0, -1]

    while len(todo) > 0:
        cost, xi, yi, direction = heapq.heappop(todo)

        # special case
        if seen[xi][yi] != -1:
            if seen[xi][yi] == cost:
                xj, yj = xi + dx[direction], yi + dy[direction]
                if c[xj][yj] == '.' and seen[xj][yj] == -1:
                    heapq.heappush(todo, (cost+1, xj, yj, direction))
            continue
        seen[xi][yi] = cost

        # goal
        if xi == x2 and yi == y2:
            print((cost+k-1)//k)
            for i in seen:
                print(i)
            exit()
        
        # add
        for j in range(4):
            xj, yj= xi + dx[j], yi + dy[j]
            if c[xj][yj] == '.' and seen[xj][yj] == -1:
                if j == direction:
                    heapq.heappush(todo, (cost+1, xj, yj, j))
                else:
                    heapq.heappush(todo, ((cost+k-1)//k * k + 1, xj, yj, j))

    print(-1)

main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
def input():
    return stdin.readline().strip()

import heapq

def main():
    h, w, k = map(int, input().split())
    x1, y1, x2, y2 = map(int, input().split())

    # surround the square with '@'
    c = [['@'] * (w+2)]
    for _ in range(h):
        i = list('@' + input() + '@')
        c.append(i)
    c.append(['@'] * (w+2))


    if k == 1:
        # BFS
        # 直線を進めるだけ進む
        from collections import deque
        seen = [[0] * (w+2) for _ in range(h+2)]
        seen[x1][y1] = 1
        todo = deque([(x1, y1)])
 
        dx = [1, 0, -1, 0]
        dy = [0, 1, 0, -1]
 
        while len(todo) > 0:
            ix, iy = todo.popleft()
            s = seen[ix][iy] + 1
 
            for j in range(4):
                jx, jy = ix, iy
                dxj, dyj = dx[j], dy[j]
 
                for _ in range(k):
                    jx, jy = jx + dxj, jy + dyj
 
                    if c[jx][jy] == '@':
                        break
 
                    if jx == x2 and jy == y2:
                        print(s - 1)
                        exit()
            
                    if seen[jx][jy] == 0:
                        seen[jx][jy] = s
                        todo.append((jx, jy))



    # Dijkstra
    seen = [[-1] * (w+2) for _ in range(h+2)]
    # todo[i] == (cost, x, y, direction)
    todo = [(0, x1, y1, 0)]
    heapq.heapify(todo)

    dx = [-1, 0, 1, 0]
    dy = [0, 1, 0, -1]

    while len(todo) > 0:
        cost, xi, yi, direction = heapq.heappop(todo)

        # special case
        if seen[xi][yi] == cost:
            xj, yj = xi + dx[direction], yi + dy[direction]
            if c[xj][yj] == '.' and seen[xj][yj] == -1:
                heapq.heappush(todo, (cost+1, xj, yj, direction))
            continue

        if seen[xi][yi] != -1:
            continue
        seen[xi][yi] = cost

        # goal
        if xi == x2 and yi == y2:
            print((cost-1)//k + 1)
            exit()
        
        # add
        for j in range(4):
            xj, yj= xi + dx[j], yi + dy[j]

            if c[xj][yj] == '@':
                continue

            if seen[xj][yj] == -1:
                if j == direction:
                    heapq.heappush(todo, (cost+1, xj, yj, j))
                else:
                    heapq.heappush(todo, (((cost-1)//k + 1)*k + 1, xj, yj, j))

    print(-1)

main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
def input():
    return stdin.readline().strip()

import heapq

def main():
    h, w, k = map(int, input().split())
    x1, y1, x2, y2 = map(int, input().split())

    # surround the square with '@'
    c = [['@'] * (w+2)]
    for _ in range(h):
        i = list('@' + input() + '@')
        c.append(i)
    c.append(['@'] * (w+2))

    # Dijkstra
    # seen[x][y] = the minimum cost of (x, y)
    # if seen[x][y] == -1, (x, y) is unresearched
    seen = [[-1] * (w+2) for _ in range(h+2)]

    # todo[i] == (cost, x, y, direction)
    todo = [(0, x1, y1, 0)]
    heapq.heapify(todo)

    # direction
    dx = [-1, 0, 1, 0]
    dy = [0, 1, 0, -1]

    while len(todo) > 0:
        cost, xi, yi, direction = heapq.heappop(todo)

        # special case
        if seen[xi][yi] != -1:
            if seen[xi][yi] == cost:
                xj, yj = xi + dx[direction], yi + dy[direction]
                if c[xj][yj] == '.' and seen[xj][yj] == -1:
                    heapq.heappush(todo, (cost+1, xj, yj, direction))
            continue
        seen[xi][yi] = cost

        # goal
        if xi == x2 and yi == y2:
            print((cost-1)//k + 1)
            exit()
        
        # add
        for j in range(4):
            xj, yj= xi + dx[j], yi + dy[j]
            if c[xj][yj] == '.' and seen[xj][yj] == -1:
                if j == direction:
                    heapq.heappush(todo, (cost+1, xj, yj, j))
                else:
                    heapq.heappush(todo, (((cost-1)//k + 1)*k + 1, xj, yj, j))

    while True:
        pass
    print(-1)

main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
def input():
    return stdin.readline().strip()

import heapq

def main():
    h, w, k = map(int, input().split())
    x1, y1, x2, y2 = map(int, input().split())

    # surround the square with '@'
    c = [['@'] * (w+2)]
    for _ in range(h):
        i = list('@' + input() + '@')
        c.append(i)
    c.append(['@'] * (w+2))

    # Dijkstra
    # seen[x][y] = the minimum cost of (x, y)
    # if seen[x][y] == -1, (x, y) is unresearched
    seen = [[-1] * (w+2) for _ in range(h+2)]

    # todo[i] == (cost, x, y, direction)
    todo = [(0, x1, y1, 0)]
    heapq.heapify(todo)

    # direction
    dx = [-1, 0, 1, 0]
    dy = [0, 1, 0, -1]

    while len(todo) > 0:
        cost, xi, yi, direction = heapq.heappop(todo)

        # special case
        if seen[xi][yi] != -1:
            if seen[xi][yi] == cost:
                xj, yj = xi + dx[direction], yi + dy[direction]
                if c[xj][yj] == '.' and seen[xj][yj] == -1:
                    heapq.heappush(todo, (cost+1, xj, yj, direction))
            continue
        seen[xi][yi] = cost

        # goal
        if xi == x2 and yi == y2:
            print((cost-1)//k)
            exit()
        
        # add
        for j in range(4):
            xj, yj= xi + dx[j], yi + dy[j]
            if c[xj][yj] == '.' and seen[xj][yj] == -1:
                if j == direction:
                    heapq.heappush(todo, (cost+1, xj, yj, j))
                else:
                    heapq.heappush(todo, (((cost-1)//k + 1)*k + 1, xj, yj, j))

    print(-1)

main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
def input():
    return stdin.readline().strip()

import heapq

def main():
    h, w, k = map(int, input().split())
    x1, y1, x2, y2 = map(int, input().split())

    # surround the square with '@'
    c = [['@'] * (w+2)]
    for _ in range(h):
        i = list('@' + input() + '@')
        c.append(i)
    c.append(['@'] * (w+2))

    # Dijkstra
    # seen[x][y] = the minimum cost of (x, y)
    # if seen[x][y] == -1, (x, y) is unresearched
    seen = [[-1] * (w+2) for _ in range(h+2)]

    # todo[i] == (cost, x, y, direction)
    todo = [(0, x1, y1, 0)]
    heapq.heapify(todo)

    # direction
    dx = [-1, 0, 1, 0]
    dy = [0, 1, 0, -1]

    while len(todo) > 0:
        cost, xi, yi, direction = heapq.heappop(todo)

        # special case
        if seen[xi][yi] != -1:
            if seen[xi][yi] == cost:
                xj, yj = xi + dx[direction], yi + dy[direction]
                if c[xj][yj] == '.' and seen[xj][yj] == -1:
                    heapq.heappush(todo, (cost+1, xj, yj, direction))
            continue
        seen[xi][yi] = cost

        # goal
        if xi == x2 and yi == y2:
            print((cost+1)//k + 1)
            exit()
        
        # add
        for j in range(4):
            xj, yj= xi + dx[j], yi + dy[j]
            if c[xj][yj] == '.' and seen[xj][yj] == -1:
                if j == direction:
                    heapq.heappush(todo, (cost+1, xj, yj, j))
                else:
                    heapq.heappush(todo, (((cost-1)//k + 1)*k + 1, xj, yj, j))

    print(-1)

main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
def input():
    return stdin.readline().strip()

import heapq

def main():
    h, w, k = map(int, input().split())
    x1, y1, x2, y2 = map(int, input().split())

    # surround the square with '@'
    c = [['@'] * (w+2)]
    for _ in range(h):
        i = list('@' + input() + '@')
        c.append(i)
    c.append(['@'] * (w+2))

    # Dijkstra
    # seen[x][y] = the minimum cost of (x, y)
    # if seen[x][y] == -1, (x, y) is unresearched
    seen = [[-1] * (w+2) for _ in range(h+2)]

    # todo[i] == (cost, x, y, direction)
    todo = [(0, x1, y1, 0)]
    heapq.heapify(todo)

    # direction
    dx = [-1, 0, 1, 0]
    dy = [0, 1, 0, -1]

    while len(todo) > 0:
        cost, xi, yi, direction = heapq.heappop(todo)

        # special case
        if seen[xi][yi] != -1:
            if seen[xi][yi] == cost:
                xj, yj = xi + dx[direction], yi + dy[direction]
                if c[xj][yj] == '.' and seen[xj][yj] == -1:
                    heapq.heappush(todo, (cost+1, xj, yj, direction))
            continue
        seen[xi][yi] = cost

        # goal
        if xi == x2 and yi == y2:
            print((cost)//k + 1)
            exit()
        
        # add
        for j in range(4):
            xj, yj= xi + dx[j], yi + dy[j]
            if c[xj][yj] == '.' and seen[xj][yj] == -1:
                if j == direction:
                    heapq.heappush(todo, (cost+1, xj, yj, j))
                else:
                    heapq.heappush(todo, (((cost-1)//k + 1)*k + 1, xj, yj, j))

    print(-1)

main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
def input():
    return stdin.readline().strip()

import heapq

def main():
    h, w, k = map(int, input().split())
    x1, y1, x2, y2 = map(int, input().split())

    # surround the square with '@'
    c = [['@'] * (w+2)]
    for _ in range(h):
        i = list('@' + input() + '@')
        c.append(i)
    c.append(['@'] * (w+2))

    # Dijkstra
    seen = [[-1] * (w+2) for _ in range(h+2)]

    # todo[i] == (cost, x, y, direction)
    todo = [(0, x1, y1, 0)]
    heapq.heapify(todo)

    # direction
    dx = [-1, 0, 1, 0]
    dy = [0, 1, 0, -1]

    while len(todo) > 0:
        cost, xi, yi, direction = heapq.heappop(todo)

        # special case
        if seen[xi][yi] != -1:
            if seen[xi][yi] == cost:
                xj, yj = xi + dx[direction], yi + dy[direction]
                if c[xj][yj] == '.' and seen[xj][yj] == -1:
                    heapq.heappush(todo, (cost+1, xj, yj, direction))
            continue
        seen[xi][yi] = cost

        # goal
        if xi == x2 and yi == y2:
            print((cost-1)//k + 1)
            exit()
        
        # add
        for j in range(4):
            xj, yj= xi + dx[j], yi + dy[j]

            if c[xj][yj] == '.' and seen[xj][yj] == -1:
                if j == direction:
                    heapq.heappush(todo, (cost+1, xj, yj, j))
                else:
                    heapq.heappush(todo, (((cost-1)//k + 1)*k + 1, xj, yj, j))

    print(-1)

main()

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys

read = sys.stdin.read
readline = sys.stdin.readline

h, w, k = map(int, readline().split())
x1, y1, x2, y2 = map(int, readline().split())
x1, y1, x2, y2 = x1 - 1, y1 - 1, x2 - 1, y2 - 1

field = [readline() for _ in range(h)]
costs = [[99999999] * w for _ in range(h)]

q = deque([(x1, y1)])
costs[x1][y1] = 0

while q:
    x, y = q.popleft()
    for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
        for i in range(1, k + 1):
            px, py = x + dx * i, y + dy * i
            if not(0 <= px < h and 0 <= py < w) or field[px][py] == '@':
                break
            if costs[px][py] <= costs[x][y] + 1:
                break
            costs[px][py] = costs[x][y] + 1
            q.append((px, py))

print(-1 if costs[x2][y2] == 99999999 else costs[x2][y2])

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys

read = sys.stdin.read
readline = sys.stdin.readline

h, w, k = map(int, readline().split())
x1, y1, x2, y2 = map(int, readline().split())
x1, y1, x2, y2 = x1 - 1, y1 - 1, x2 - 1, y2 - 1

field = [readline() for _ in range(h)]

visited = [[False] * w for _ in range(h)]
costs = [[-1] * w for _ in range(h)]

q = deque([(x1, y1)])
costs[x1][y1] = 0

while q:
    x, y = q.popleft()
    if visited[x][y]:
        continue
    visited[x][y] = True
    for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
        for i in range(1, k + 1):
            px, py = x + dx * i, y + dy * i
            if not(0 <= px < h and 0 <= py < w) or field[px][py] == '@' or visited[px][py] or (costs[px][py] != -1 and costs[px][py] <= costs[x][y] + 1):
                break
            costs[px][py] = costs[x][y] + 1
            q.append((px, py))

print(costs[x2][y2])

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys

read = sys.stdin.read
readline = sys.stdin.readline

h, w, k = map(int, readline().split())
x1, y1, x2, y2 = map(int, readline().split())
x1, y1, x2, y2 = x1 - 1, y1 - 1, x2 - 1, y2 - 1

field = [readline() for _ in range(h)]
costs = [[-1] * w for _ in range(h)]

q = deque([(x1, y1)])
costs[x1][y1] = 0

while q:
    x, y = q.popleft()
    for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
        for i in range(1, k + 1):
            px, py = x + dx * i, y + dy * i
            if not(0 <= px < h and 0 <= py < w) or field[px][py] == '@' or (costs[px][py] != -1 and costs[px][py] <= costs[x][y] + 1):
                break
            costs[px][py] = costs[x][y] + 1
            q.append((px, py))

print(costs[x2][y2])

Submission status: Wrong Answer

Solution code:
from collections import deque

h, w, K = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1
pond = []
for _ in range(h):
    pond.append(list(input()))

dx = (1, 0, -1, 0)
dy = (0, 1, 0, -1)

table = [[[K + 2] * 4 for _ in range(w)] for _ in range(h)]

dist = [[10 ** 8] * w for _ in range(h)]
dist[x1][y1] = 0
q = deque([(x1, y1, -1, 0, 0)])
while q:
#    print(q)
    x, y, prev_k, cnt, dst = q.popleft()
    if dist[x][y] != dst:
        continue
#    print(x, y, prev_k, cnt, dist[x][y])
    cnt += 1
    for k in range(4):
        nx = x + dx[k]
        ny = y + dy[k]
        if 0 <= nx < h and 0 <= ny < w and pond[nx][ny] != "@":
            cost = 0
            cnt2 = cnt
            if k != prev_k or cnt2 > K:
                cost = 1
                cnt2 = 1
            improve = dist[nx][ny] - dist[x][y] - cost
#            print(k, improve)
            if improve >= 0:
                if improve == 0 and cnt2 >= table[nx][ny][k]:
                    continue
                table[nx][ny][k] = cnt2
                dist[nx][ny] = dist[x][y] + cost
#                print(dist[nx][ny])
                if cost == 0:
                    q.appendleft((nx, ny, k, cnt2, dist[nx][ny]))
                else:
                    q.append((nx, ny, k, cnt2, dist[nx][ny]))
if dist[x2][y2] == 10 ** 8:
    print(-1)
else:
    print(dist[x2][y2])