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 heapq import heappop, heappush
import sys
input = sys.stdin.readline
H, W, K = map(int, input().split())
x1, y1, x2, y2 = map(lambda x: int(x)-1, input().split())
S = [input()[:-1] for _ in [0]*H]

def geth(x, y, z):
    return (x*W+y)*4+z

INF = 1<<30
MAX = H*W*4
D = [INF]*MAX
dirs = [(-1, 0), (0, -1), (1, 0), (0, 1)]

q = []
for z in range(4):
    heappush(q, (0, geth(x1, y1, z)))

while q:
    d, h = heappop(q)
    z = h % 4
    x, y = divmod((h//4), W)

    if d >= D[h]:
        continue
    D[h] = d

    for _z, (dx, dy) in enumerate(dirs):
        if _z == z:
            _h = h+dy*4+dx*4*W
            _x, _y = x+dx, y+dy
            if 0 <= _x < H and 0 <= _y < W and S[_x][_y] == '.' and D[_h] > d+1:
                heappush(q, (d+1, _h))
        else:
            _h = h - z + _z
            _d = (d-1)//K*K+K
            if D[_h] > _d:
                heappush(q, (_d, _h))

g = geth(x2, y2, 0)
ans = min(D[g:g+4])
ans = (ans-1)//K+1
print(-1 if ans == INF else ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush
import sys
input = sys.stdin.readline
H, W, K = map(int, input().split())
x1, y1, x2, y2 = map(lambda x: int(x)-1, input().split())
S = [input()[:-1] for _ in [0]*H]

dirs = [(0, -1), (1, 0), (0, 1), (-1, 0)]

INF = 1<<30
D = [[[INF]*4 for _ in [0]*W] for _ in [0]*H]

q = []
for z in range(4):
    heappush(q, (0, x1, y1, z))

while q:
    d, x, y, z = heappop(q)

    if d >= D[x][y][z]:
        continue
    D[x][y][z] = d

    for _z, (dx, dy) in enumerate(dirs):
        if _z == z:
            _x = x+dx
            _y = y+dy
            if 0 <= _x < H and 0 <= _y < W and S[_x][_y] == '.' and D[_x][_y][z] > d+1:
                heappush(q, (d+1, _x, _y, z))
        else:
            new_d = (d-1)//K*K+K
            if D[x][y][_z] > new_d:
                heappush(q, (new_d, x, y, _z))

ans = min(D[x2][y2])
ans = (ans-1)//K+1
print(-1 if ans == INF else ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush
import sys
input = sys.stdin.readline
H, W, K = map(int, input().split())
x1, y1, x2, y2 = map(lambda x: int(x)-1, input().split())
S = [input()[:-1] for _ in [0]*H]

dirs = [(0, -1), (1, 0), (0, 1), (-1, 0)]

INF = 1<<30
D = [[[INF]*4 for _ in [0]*W] for _ in [0]*H]

q = []
for z in range(4):
    heappush(q, (0, x1, y1, z))

while q:
    d, x, y, z = heappop(q)

    if d >= D[x][y][z]:
        continue
    D[x][y][z] = d

    for _z, (dx, dy) in enumerate(dirs):
        _x = x+dx
        _y = y+dy
        if 0 <= _x < H and 0 <= _y < W and S[_x][_y] == '.':
            if _z == z:
                heappush(q, (d+1, _x, _y, _z))
            else:
                heappush(q, ((d-1)//K*K+K+1, _x, _y, _z))

ans = min(D[x2][y2])
ans = (ans-1)//K+1
print(-1 if ans == INF else ans)

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] == '.':
                    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] == '.':
                if j == direction:
                    if seen[xj][yj] == -1 or (seen[xj][yj]+k-1)//k * k >= cost:
                        heapq.heappush(todo, (cost+1, xj, yj, j))
                elif seen[xj][yj] == -1:
                    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] == '.':
                    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] == '.':
                if j == direction:
                    if seen[xj][yj] == -1 or (seen[xj][yj]+k-1)//k * k > cost:
                        heapq.heappush(todo, (cost+1, xj, yj, j))
                elif seen[xj][yj] == -1:
                    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 or (seen[xj][yj]+k-1)//k * k > cost):
                    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 - 1)
            exit()
        
        # add
        for j in range(4):
            xj, yj= xi + dx[j], yi + dy[j]
            if c[xj][yj] == '.':
                if j == direction:
                    if seen[xj][yj] == -1 or (seen[xj][yj]+k-1)//k * k > cost:
                        heapq.heappush(todo, (cost+1, xj, yj, j))
                elif seen[xj][yj] == -1:
                    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 or (seen[xj][yj]+k-1)//k * k > cost):
                    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 + 1)
            exit()
        
        # add
        for j in range(4):
            xj, yj= xi + dx[j], yi + dy[j]
            if c[xj][yj] == '.':
                if j == direction:
                    if seen[xj][yj] == -1 or (seen[xj][yj]+k-1)//k * k > cost:
                        heapq.heappush(todo, (cost+1, xj, yj, j))
                elif seen[xj][yj] == -1:
                    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 or (seen[xj][yj]+k-1)//k * k > cost):
                    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] == '.':
                if j == direction:
                    if seen[xj][yj] == -1 or (seen[xj][yj]+k-1)//k * k > cost:
                        heapq.heappush(todo, (cost+1, xj, yj, j))
                elif seen[xj][yj] == -1:
                    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 or seen[xj][yj] == cost + 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] == '.':
                if j == direction:
                    if seen[xj][yj] == -1 or seen[xj][yj] == cost + 1:
                        heapq.heappush(todo, (cost+1, xj, yj, j))
                elif seen[xj][yj] == -1 or seen[xj][yj] == (cost+k-1)//k * k + 1:
                    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 or seen[xj][yj] == cost + 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 or seen[xj][yj] == cost + 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:
import sys
input = sys.stdin.readline
# 文字列をinput()した場合、末尾に改行が入るので注意
def main():
  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}
  inf=float('inf')
  seen=[[[inf]*4 for _ in range(w)] for _ in range(h)]
  todo=[[0,x1,y1,'w']]
  seen[x1][y1][dv['w']]=0
  heapify(todo)
  #d=[[-1]*w for _ in range(h)]
  cnt=1
  while todo:
    c,x,y,v=heappop(todo)
    #print(c,x,y,v)
    if (x,y)==(x2,y2):
      print((c+k-1)//k)
      exit()
      #break
    if v=='w':
      if seen[x][y][dv['n']]>k*((c+k-1)//k):
        heappush(todo,[k*((c+k-1)//k),x,y,'n'])
        seen[x][y][dv['n']]=k*((c+k-1)//k)
      if y>0 and cmap[x][y-1]!='@' and seen[x][y-1][dv['w']]>c+1:
        heappush(todo,[c+1,x,y-1,'w'])
        seen[x][y-1][dv['w']]=c+1
    elif v=='e':
      if seen[x][y][dv['s']]>k*((c+k-1)//k):
        heappush(todo,[k*((c+k-1)//k),x,y,'s'])
        seen[x][y][dv['s']]=k*((c+k-1)//k)
      if y<w-1 and cmap[x][y+1]!='@' and seen[x][y+1][dv['e']]>c+1:
        heappush(todo,[c+1,x,y+1,'e'])
        seen[x][y+1][dv['e']]=c+1
    elif v=='n':
      if seen[x][y][dv['e']]>k*((c+k-1)//k):
        heappush(todo,[ceil(c),x,y,'e'])
        seen[x][y][dv['e']]=k*((c+k-1)//k)
      if x>0 and cmap[x-1][y]!='@' and seen[x-1][y][dv['n']]>c+1:
        heappush(todo,[c+1,x-1,y,'n'])
        seen[x-1][y][dv['n']]=c+1
    elif v=='s':
      if seen[x][y][dv['w']]>k*((c+k-1)//k):
        heappush(todo,[ceil(c),x,y,'w'])
        seen[x][y][dv['w']]=k*((c+k-1)//k)
      if x<h-1 and cmap[x+1][y]!='@' and seen[x+1][y][dv['s']]>c+1:
        heappush(todo,[c+1,x+1,y,'s'])
        seen[x+1][y][dv['s']]=c+1

  print(-1)
  #for si in seen:
  #  print([min(sij) for sij in si])

if __name__=='__main__':
  #import datetime
  #print(datetime.datetime.now())
  main()
  #print(datetime.datetime.now())

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
# 文字列をinput()した場合、末尾に改行が入るので注意
def main():
  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}
  inf=float('inf')
  seen=[[[inf]*4 for _ in range(w)] for _ in range(h)]
  todo=[[0,x1,y1,'w']]
  seen[x1][y1][dv['w']]=0
  heapify(todo)
  #d=[[-1]*w for _ in range(h)]
  cnt=1
  while todo:
    c,x,y,v=heappop(todo)
    print(c,x,y,v)
    if (x,y)==(x2,y2):
      print((c+k-1)//k)
      exit()
      #break
    if v=='w':
      if seen[x][y][dv['n']]>k*((c+k-1)//k):
        heappush(todo,[k*((c+k-1)//k),x,y,'n'])
        seen[x][y][dv['n']]=k*((c+k-1)//k)
      if y>0 and cmap[x][y-1]!='@' and seen[x][y-1][dv['w']]>c+1:
        heappush(todo,[c+1,x,y-1,'w'])
        seen[x][y-1][dv['w']]=c+1
    elif v=='e':
      if seen[x][y][dv['s']]>k*((c+k-1)//k):
        heappush(todo,[k*((c+k-1)//k),x,y,'s'])
        seen[x][y][dv['s']]=k*((c+k-1)//k)
      if y<w-1 and cmap[x][y+1]!='@' and seen[x][y+1][dv['e']]>c+1:
        heappush(todo,[c+1,x,y+1,'e'])
        seen[x][y+1][dv['e']]=c+1
    elif v=='n':
      if seen[x][y][dv['e']]>k*((c+k-1)//k):
        heappush(todo,[ceil(c),x,y,'e'])
        seen[x][y][dv['e']]=k*((c+k-1)//k)
      if x>0 and cmap[x-1][y]!='@' and seen[x-1][y][dv['n']]>c+1:
        heappush(todo,[c+1,x-1,y,'n'])
        seen[x-1][y][dv['n']]=c+1
    elif v=='s':
      if seen[x][y][dv['w']]>k*((c+k-1)//k):
        heappush(todo,[ceil(c),x,y,'w'])
        seen[x][y][dv['w']]=k*((c+k-1)//k)
      if x<h-1 and cmap[x+1][y]!='@' and seen[x+1][y][dv['s']]>c+1:
        heappush(todo,[c+1,x+1,y,'s'])
        seen[x+1][y][dv['s']]=c+1

  print(-1)
  #for si in seen:
  #  print([min(sij) for sij in si])

if __name__=='__main__':
  #import datetime
  #print(datetime.datetime.now())
  main()
  #print(datetime.datetime.now())

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
# 文字列をinput()した場合、末尾に改行が入るので注意
def main():
  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}
  inf=float('inf')
  seen=[[[inf]*4 for _ in range(w)] for _ in range(h)]
  todo=[[0,x1,y1,'w']]
  heapify(todo)
  #d=[[-1]*w for _ in range(h)]
  cnt=1
  while todo:
    c,x,y,v=heappop(todo)
    #d[x][y]=min(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']]>ceil(c):
        heappush(todo,[ceil(c),x,y,'n'])
        seen[x][y][dv['n']]=ceil(c)
      if y>0 and cmap[x][y-1]!='@' and seen[x][y-1][dv['w']]>c+1/k:
        heappush(todo,[c+1/k,x,y-1,'w'])
        seen[x][y-1][dv['w']]=c+1/k
    elif v=='e':
      if seen[x][y][dv['s']]>ceil(c):
        heappush(todo,[ceil(c),x,y,'s'])
        seen[x][y][dv['s']]=ceil(c)
      if y<w-1 and cmap[x][y+1]!='@' and seen[x][y+1][dv['e']]>c+1/k:
        heappush(todo,[c+1/k,x,y+1,'e'])
        seen[x][y+1][dv['e']]=c+1/k
    elif v=='n':
      if seen[x][y][dv['e']]>ceil(c):
        heappush(todo,[ceil(c),x,y,'e'])
        seen[x][y][dv['e']]=ceil(c)
      if x>0 and cmap[x-1][y]!='@' and seen[x-1][y][dv['n']]>c+1/k:
        heappush(todo,[c+1/k,x-1,y,'n'])
        seen[x-1][y][dv['n']]=c+1/k
    elif v=='s':
      if seen[x][y][dv['w']]>ceil(c):
        heappush(todo,[ceil(c),x,y,'w'])
        seen[x][y-1][dv['w']]=ceil(c)
      if x<h-1 and cmap[x+1][y]!='@' and seen[x+1][y][dv['s']]>c+1/k:
        heappush(todo,[c+1/k,x+1,y,'s'])
        seen[x+1][y][dv['s']]=c+1/k

  print(-1)
  #print(seen)

if __name__=='__main__':
  #import datetime
  #print(datetime.datetime.now())
  main()
  #print(datetime.datetime.now())

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}
inf=float('inf')
seen=[[[inf]*4 for _ in range(w)] for _ in range(h)]
todo=[[0,x1,y1,'w']]
heapify(todo)
#d=[[-1]*w for _ in range(h)]
cnt=1
while todo:
  c,x,y,v=heappop(todo)
  #d[x][y]=min(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']]>ceil(c):
      heappush(todo,[ceil(c),x,y,'n'])
      seen[x][y][dv['n']]=ceil(c)
    if y>0 and cmap[x][y-1]!='@' and seen[x][y-1][dv['w']]>c+1/k:
      heappush(todo,[c+1/k,x,y-1,'w'])
      seen[x][y-1][dv['w']]=c+1/k
  elif v=='e':
    if seen[x][y][dv['s']]>ceil(c):
      heappush(todo,[ceil(c),x,y,'s'])
      seen[x][y][dv['s']]=ceil(c)
    if y<w-1 and cmap[x][y+1]!='@' and seen[x][y+1][dv['e']]>c+1/k:
      heappush(todo,[c+1/k,x,y+1,'e'])
      seen[x][y+1][dv['e']]=c+1/k
  elif v=='n':
    if seen[x][y][dv['e']]>ceil(c):
      heappush(todo,[ceil(c),x,y,'e'])
      seen[x][y][dv['e']]=ceil(c)
    if x>0 and cmap[x-1][y]!='@' and seen[x-1][y][dv['n']]>c+1/k:
      heappush(todo,[c+1/k,x-1,y,'n'])
      seen[x-1][y][dv['n']]=c+1/k
  elif v=='s':
    if seen[x][y][dv['w']]>ceil(c):
      heappush(todo,[ceil(c),x,y,'w'])
      seen[x][y-1][dv['w']]=ceil(c)
    if x<h-1 and cmap[x+1][y]!='@' and seen[x+1][y][dv['s']]>c+1/k:
      heappush(todo,[c+1/k,x+1,y,'s'])
      seen[x+1][y][dv['s']]=c+1/k

print(-1)
#print(seen)

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}
inf=float('inf')
seen=[[[inf]*4 for _ in range(w)] for _ in range(h)]
todo=[[0,x1,y1,'w']]
heapify(todo)
#d=[[-1]*w for _ in range(h)]
cnt=1
while todo:
  c,x,y,v=heappop(todo)
  #d[x][y]=min(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']]>ceil(c):
      heappush(todo,[ceil(c),x,y,'n'])
      seen[x][y][dv['n']]=ceil(c)
    if y>0 and cmap[x][y-1]!='@' and seen[x][y-1][dv['w']]>c+1/k:
      heappush(todo,[c+1/k,x,y-1,'w'])
      seen[x][y-1][dv['w']]=c+1/k
  elif v=='e':
    if seen[x][y][dv['s']]>ceil(c):
      heappush(todo,[ceil(c),x,y,'s'])
      seen[x][y][dv['s']]=ceil(c)
    if y<w-1 and cmap[x][y+1]!='@' and seen[x][y+1][dv['e']]>c+1/k:
      heappush(todo,[c+1/k,x,y+1,'e'])
      seen[x][y-1][dv['e']]=c+1/k
  elif v=='n':
    if seen[x][y][dv['e']]>ceil(c):
      heappush(todo,[ceil(c),x,y,'e'])
      seen[x][y][dv['e']]=ceil(c)
    if x>0 and cmap[x-1][y]!='@' and seen[x-1][y][dv['n']]>c+1/k:
      heappush(todo,[c+1/k,x-1,y,'n'])
      seen[x][y-1][dv['n']]=c+1/k
  elif v=='s':
    if seen[x][y][dv['w']]>ceil(c):
      heappush(todo,[ceil(c),x,y,'w'])
      seen[x][y-1][dv['w']]=ceil(c)
    if x<h-1 and cmap[x+1][y]!='@' and seen[x+1][y][dv['s']]>c+1/k:
      heappush(todo,[c+1/k,x+1,y,'s'])
      seen[x][y-1][dv['s']]=c+1/k

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']]

heapify(todo)
#d=[[-1]*w for _ in range(h)]
cnt=1
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]=min(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:
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 ** 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] * 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 nx == gx - 1 and ny == gy - 1:
                    exit(print(cost[ny][nx]))
                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 nx == gx - 1 and ny == gy - 1:
                    exit(print(cost[ny][nx]))
                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)