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 queue import Queue


def ok(h, w, x, y):
    if 0 <= x < h and 0 <= y < w:
        return True
    else:
        return False


def main():
    h, w, k = map(int, input().split())
    sx, sy, gx, gy = map(lambda z: int(z) - 1, input().split())
    c = [list(input().replace('\n', '')) for _ in range(h)]
    c[sx][sy] = 0
    q = Queue()
    q.put((sx, sy))
    while not q.empty():
        x, y = q.get()
        d = c[x][y]
        # 南へ移動
        for i in range(1, k + 1):
            if not ok(h, w, x + i, y):
                break
            if c[x + i][y] == '@':
                break
            elif isinstance(c[x + i][y], int):
                continue
            else:
                c[x + i][y] = d + 1
                if (ok(h,
                       w,
                       x + i,
                       y + 1) and c[x + i][y + 1] != '@') \
                        or (ok(h,
                               w,
                               x + i,
                               y - 1) and c[x + i][y - 1] != '@'):
                    q.put((x + i, y))
        # 北へ移動
        for i in range(1, k + 1):
            if not ok(h, w, x - i, y):
                break
            if c[x - i][y] == '@':
                break
            elif isinstance(c[x - i][y], int):
                continue
            else:
                c[x - i][y] = d + 1
                if (ok(h,
                       w,
                       x - i,
                       y + 1) and c[x - i][y + 1] != '@') \
                        or (ok(h,
                               w,
                               x - i,
                               y - 1) and c[x - i][y - 1] != '@'):
                    q.put((x - i, y))
        # 東へ移動
        for i in range(1, k + 1):
            if not ok(h, w, x, y + i):
                break
            if c[x][y + i] == '@':
                break
            elif isinstance(c[x][y + i], int):
                continue
            else:
                c[x][y + i] = d + 1
                if (ok(h,
                       w,
                       x + 1,
                       y + i) and c[x + 1][y + i] != '@') \
                        or (ok(h,
                               w,
                               x - 1,
                               y + i) and c[x - 1][y + i] != '@'):
                    q.put((x, y + i))
        # 西へ移動
        for i in range(1, k + 1):
            if not ok(h, w, x, y - i):
                break
            if c[x][y - i] == '@':
                break
            elif isinstance(c[x][y - i], int):
                continue
            else:
                c[x][y - i] = d + 1
                if (ok(h,
                       w,
                       x + 1,
                       y - i) and c[x + 1][y - i] != '@') \
                        or (ok(h,
                               w,
                               x - 1,
                               y - i) and c[x - 1][y - i] != '@'):
                    q.put((x, y - i))
    print(-1 if c[gx][gy] == '.' else c[gx][gy])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from queue import Queue


def ok(h, w, x, y):
    if 0 <= x < h and 0 <= y < w:
        return True
    else:
        return False


def main():
    h, w, k = map(int, input().split())
    sx, sy, gx, gy = map(lambda z: int(z) - 1, input().split())
    c = [list(input().replace('\n', '')) for _ in range(h)]
    c[sx][sy] = 0
    q = Queue()
    q.put((sx, sy))
    while not q.empty():
        x, y = q.get()
        d = c[x][y]
        # 南へ移動
        for i in range(1, k + 1):
            if not ok(h, w, x + i, y):
                break
            if c[x + i][y] == '@':
                break
            elif isinstance(c[x + i][y], int):
                continue
            else:
                c[x + i][y] = d + 1
                if (ok(h,
                       w,
                       x + i,
                       y + 1) and c[x + i][y + 1] == '.') \
                        or (ok(h,
                               w,
                               x + i,
                               y - 1) and c[x + i][y - 1] == '.'):
                    q.put((x + i, y))
        # 北へ移動
        for i in range(1, k + 1):
            if not ok(h, w, x - i, y):
                break
            if c[x - i][y] == '@':
                break
            elif isinstance(c[x - i][y], int):
                continue
            else:
                c[x - i][y] = d + 1
                if (ok(h,
                       w,
                       x - i,
                       y + 1) and c[x - i][y + 1] == '.') \
                        or (ok(h,
                               w,
                               x - i,
                               y - 1) and c[x - i][y - 1] == '.'):
                    q.put((x - i, y))
        # 東へ移動
        for i in range(1, k + 1):
            if not ok(h, w, x, y + i):
                break
            if c[x][y + i] == '@':
                break
            elif isinstance(c[x][y + i], int):
                continue
            else:
                c[x][y + i] = d + 1
                if (ok(h,
                       w,
                       x + 1,
                       y + i) and c[x + 1][y + i] == '.') \
                        or (ok(h,
                               w,
                               x - 1,
                               y + i) and c[x - 1][y + i] == '.'):
                    q.put((x, y + i))
        # 西へ移動
        for i in range(1, k + 1):
            if not ok(h, w, x, y - i):
                break
            if c[x][y - i] == '@':
                break
            elif isinstance(c[x][y - i], int):
                continue
            else:
                c[x][y - i] = d + 1
                if (ok(h,
                       w,
                       x + 1,
                       y - i) and c[x + 1][y - i] == '.') \
                        or (ok(h,
                               w,
                               x - 1,
                               y - i) and c[x - 1][y - i] == '.'):
                    q.put((x, y - i))
    print(-1 if c[gx][gy] == '.' else c[gx][gy])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys

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

  grid = [None]*h
  for i in range(h):
    grid[i] = input()

  not_yet = deque([])
  already = [[False]*w for _ in range(h)]
  dist = [[0]*w for _ in range(h)]
  judgement_h = [[False]*w for _ in range(h)]
  judgement_w = [[False]*w for _ in range(h)]

  not_yet.append((x1-1, y1-1))
  already[x1-1][y1-1] = True

  while not_yet:
    key = not_yet.popleft()
    k1, k2 = key[0], key[1]
    judgement_h[k1][k2] = True
    judgement_w[k1][k2] = True
  
    border = k1
    for i in range(k1-1, max([-1, k1-k-1]), -1):
      if grid[i][k2] == "@":
        break
      border -= 1
    for i in range(max([0, k1-k, border]), k1):
      if judgement_h[i][k2]:
        break
      elif grid[i][k2] == "@":
        continue
      elif not already[i][k2] and grid[i][k2] == ".":
        dist[i][k2] = dist[k1][k2] + 1
        already[i][k2] = True
        not_yet.append((i, k2))
        judgement_h[i][k2] = True
  
    border = k1
    for i in range(k1+1, min([h, k1+k+1])):
      if grid[i][k2] == "@":
        break
      border += 1
    for i in range(min([h-1, k1+k, border]), k1, -1):
      if judgement_h[i][k2]:
        break
      elif grid[i][k2] == "@":
        break
      elif not already[i][k2] and grid[i][k2] == ".":
        dist[i][k2] = dist[k1][k2] + 1
        already[i][k2] = True
        not_yet.append((i, k2))
        judgement_h[i][k2] = True

    border = k2
    for i in range(k2-1, min([-1, k2-k-1]), -1):
      if grid[k1][i] == "@":
        break
      border -= 1
    for i in range(max([0, k2-k]), k2):
      if judgement_w[k1][i] == True:
        break
      elif grid[k1][i] == "@":
        break
      elif not already[k1][i] and grid[k1][i] == ".":
        dist[k1][i] = dist[k1][k2] + 1
        already[k1][i] = True
        not_yet.append((k1, i))
        judgement_w[k1][i] = True
  
    border = k2
    for i in range(k2+1, min([w, k2+k+1])):
      if grid[k1][i] == "@":
        break
      border += 1
    for i in range(min([w-1, k2+k, border]), k2, -1):
      if judgement_w[k1][i] == True:
        break
      elif grid[k1][i] == "@":
        break
      elif not already[k1][i] and grid[k1][i] == ".":
        dist[k1][i] = dist[k1][k2] + 1
        already[k1][i] = True
        not_yet.append((k1, i))
        judgement_w[k1][i] = True
  
  if dist[x2-1][y2-1] == 0:
    print(-1)
  else:
    print(dist[x2-1][y2-1])

if __name__ == "__main__":
  main()

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())

grid = [None]*h
for i in range(h):
  grid[i] = input()

not_yet = deque([])
already = [[False]*w for _ in range(h)]
dist = [[0]*w for _ in range(h)]
judgement_h = [[False]*w for _ in range(h)]
judgement_w = [[False]*w for _ in range(h)]

not_yet.append((x1-1, y1-1))
already[x1-1][y1-1] = True

while not_yet:
  key = not_yet.popleft()
  k1, k2 = key[0], key[1]
  judgement_h[k1][k2] = True
  judgement_w[k1][k2] = True
  
  border = k1
  for i in range(k1-1, max([-1, k1-k-1]), -1):
    if grid[i][k2] == "@":
      break
    border -= 1
  for i in range(max([0, k1-k, border]), k1):
    if judgement_h[i][k2]:
      break
    elif grid[i][k2] == "@":
      continue
    elif not already[i][k2] and grid[i][k2] == ".":
      dist[i][k2] = dist[k1][k2] + 1
      already[i][k2] = True
      not_yet.append((i, k2))
      judgement_h[i][k2] = True
  
  border = k1
  for i in range(k1+1, min([h, k1+k+1])):
    if grid[i][k2] == "@":
      break
    border += 1
  for i in range(min([h-1, k1+k, border]), k1, -1):
    if judgement_h[i][k2]:
      break
    elif grid[i][k2] == "@":
      break
    elif not already[i][k2] and grid[i][k2] == ".":
      dist[i][k2] = dist[k1][k2] + 1
      already[i][k2] = True
      not_yet.append((i, k2))
      judgement_h[i][k2] = True

  border = k2
  for i in range(k2-1, min([-1, k2-k-1]), -1):
    if grid[k1][i] == "@":
      break
    border -= 1
  for i in range(max([0, k2-k]), k2):
    if judgement_w[k1][i] == True:
      break
    elif grid[k1][i] == "@":
      break
    elif not already[k1][i] and grid[k1][i] == ".":
      dist[k1][i] = dist[k1][k2] + 1
      already[k1][i] = True
      not_yet.append((k1, i))
      judgement_w[k1][i] = True
  
  border = k2
  for i in range(k2+1, min([w, k2+k+1])):
    if grid[k1][i] == "@":
      break
    border += 1
  for i in range(min([w-1, k2+k, border]), k2, -1):
    if judgement_w[k1][i] == True:
      break
    elif grid[k1][i] == "@":
      break
    elif not already[k1][i] and grid[k1][i] == ".":
      dist[k1][i] = dist[k1][k2] + 1
      already[k1][i] = True
      not_yet.append((k1, i))
      judgement_w[k1][i] = True
  
if dist[x2-1][y2-1] == 0:
  print(-1)
else:
  print(dist[x2-1][y2-1])

Submission status: Wrong Answer

Solution code:
#16:41
h,w,k = map(int,input().split())
x1,y1,x2,y2 = map(lambda x:int(x)-1, input().split())
raw = []
for _ in range(h):
  raw.append(input())
inf = 10**7
seen = [[[inf for _ in range(4)] for _ in range(w)] for _ in range(h)]
seen[x1][y1] = [0,0,0,0]

dire = [[1,0],[-1,0],[0,1],[0,-1]]
def add(x,y,di,I,J,s):
  dx,dy = dire[di]
  if J == 0:
    if 0 <= x+dx < h and 0 <= y+dy < w:
      if raw[x+dx][y+dy] == '.' and seen[x+dx][y+dy][di] > s+I:
        #print(s+I,'&&',x+dx,y+dy,di)
        now.append([s+I,x+dx,y+dy,di])
        seen[x+dx][y+dy][di] = s+I
  else:
    if seen[x][y][di] > s+I:
      nex.append([s+I,x,y,di])
      seen[x][y][di] = s+I

nex = [[0,x1,y1,d] for d in range(4)]
while nex:
  now = nex
  nex = []
  for i in range(k):
    last = now
    now = []
    for sxydi in last:
      s,x,y,di = sxydi
      if seen[x][y][di] < s:
        continue
      if x == x2 and y == y2:
        print(s)
        #for xx in seen:
          #print(xx)
        exit()
      #print('&',s,x,y,di)
      add(x,y,di,1-int(bool(i)),0,s)
      for d in range(4):
        if di != d:
          add(x,y,d,0,1,s)
    #print(now)
  #print('#',nex)
  nex += now
  #print('#',nex)
print(-1)
#for xx in seen:
  #print(xx)

Submission status: Wrong Answer

Solution code:
#16:41
h,w,k = map(int,input().split())
x1,y1,x2,y2 = map(lambda x:int(x)-1, input().split())
raw = []
for _ in range(h):
  raw.append(input())
inf = 10**6
seen = [[[inf for _ in range(4)] for _ in range(w)] for _ in range(h)]
seen[x1][y1] = [0,0,0,0]

dire = [[1,0],[-1,0],[0,1],[0,-1]]
def add(x,y,di,I,J,s):
  dx,dy = dire[di]
  if J == 0:
    if 0 <= x+dx < h and 0 <= y+dy < w:
      if raw[x+dx][y+dy] == '.' and seen[x+dx][y+dy][di] > s+I:
        #print(s+I,'&&',x+dx,y+dy,di)
        now.append([s+I,x+dx,y+dy,di])
        seen[x+dx][y+dy][di] = s+I
  else:
    if seen[x][y][di] > s+I:
      nex.append([s+I,x,y,di])
      seen[x][y][di] = s+I

nex = [[0,x1,y1,d] for d in range(4)]
while nex:
  now = nex
  nex = []
  for i in range(k):
    last = now
    now = []
    for sxydi in last:
      s,x,y,di = sxydi
      if seen[x][y][di] < s:
        continue
      if x == x2 and y == y2:
        print(s)
        #for xx in seen:
          #print(xx)
        exit()
      #print('&',s,x,y,di)
      add(x,y,di,1-int(bool(i)),0,s)
      for d in range(4):
        if di != d:
          add(x,y,d,0,1,s)
    #print(now)
  #print('#',nex)
  nex += now
  #print('#',nex)
print(-1)
#for xx in seen:
  #print(xx)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
import sys
input = sys.stdin.readline

h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1
c = [tuple(input()) for _ in range(h)]
# print(x1, y1)
# print(x2, y2)

dx = [+1, 0, -1, 0]
dy = [0, +1, 0, -1]
MAX = 10**7


def convert(x, y, d):
    return (x * w + y) * 4 + d


# dp = [[[(MAX, MAX)] * 4 for _ in range(w)] for _ in range(h)]
dp = [(MAX, MAX)] * (h * w * 4)

# q = [((1, 0, x1, y1, i)) for i in range(4)]
q = [((1, 0, convert(x1, y1, i))) for i in range(4)]

while q:
    c1, c2, xyd = heappop(q)
    # print(x, y, d, c1, c2)
    d = xyd % 4
    y = (xyd // 4) % w
    x = (xyd // 4) // w
    if c1 >= dp[xyd][0]:
        continue
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        nxyi = convert(nx, ny, i)
        if nx < 0 or nx >= h or ny < 0 or ny >= w:
            continue
        if c[nx][ny] == '@':
            continue
        if i == d and c2 < k:
            if dp[nxyi] > (c1, c2 + 1):
                dp[nxyi] = (c1, c2 + 1)
                heappush(q, (c1, c2 + 1, nxyi))
        else:
            if dp[nxyi] > (c1 + 1, 1):
                dp[nxyi] = (c1 + 1, 1)
                heappush(q, (c1 + 1, 1, nxyi))

ans = min(dp[convert(x2, y2, i)][0] for i in range(4))
if ans == MAX:
    ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
#関数リスト
import sys
input = sys.stdin.readline
from collections import defaultdict, deque

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    h, w, k = MI()
    x1, y1, x2, y2 = MI()
    x1 -= 1
    y1 -= 1
    x2 -= 1
    y2 -= 1

    mylist = [0] * h

    #tableの読み込み
    for i in range(h):
        temp = input().rstrip()
        temp = [temp[j] for j in range(w)]
        mylist[i] = temp
    #初期化
    distance = [[-1 for i in range(w)] for j in range(h)]
    distance[x1][y1] = 0
    d = deque()
    start = (x1, y1)
    goal = [x2, y2]
    d.append(start)
    move = ([-1,0],[1,0],[0,1],[0,-1])

    while d:
        pos = d.popleft()
        x = pos[0]
        y = pos[1]
        if x == x2 and y == y2:
            print(distance[x2][y2])
            exit()
        dist = distance[x][y]
        for i in move:
            #tempxの初期化
            tempx = x
            tempy = y
            for l in range(k):
                tempx += i[0]
                tempy += i[1]
                if not ((tempx >= 0 and tempx <= h-1) and (tempy >= 0 and tempy <= w - 1)):
                    break
                elif mylist[tempx][tempy] == "." and distance[tempx][tempy] == -1:
                    distance[tempx][tempy] = dist + 1
                    d.append((tempx, tempy))
                elif dist + 1 <= distance[tempx][tempy] and mylist[tempx][tempy] == ".":
                    distance[tempx][tempy] = dist + 1
                else:
                    break
                
        
main()

Submission status: Wrong Answer

Solution code:
#関数リスト
import sys
input = sys.stdin.readline
from collections import defaultdict, deque

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    h, w, k = MI()
    x1, y1, x2, y2 = MI()
    x1 -= 1
    y1 -= 1
    x2 -= 1
    y2 -= 1

    mylist = [0] * h

    #tableの読み込み
    for i in range(h):
        temp = input().rstrip()
        temp = [temp[j] for j in range(w)]
        mylist[i] = temp
    #初期化
    distance = [[float('inf') for i in range(w)] for j in range(h)]
    distance[x1][y1] = 0
    d = deque()
    start = (x1, y1)
    goal = [x2, y2]
    d.append(start)
    move = ([-1,0],[1,0],[0,1],[0,-1])

    while d:
        pos = d.pop()
        x = pos[0]
        y = pos[1]
        dist = distance[x][y]
        for i in move:
            #tempxの初期化
            tempx = x
            tempy = y
            for l in range(k):
                tempx += i[0]
                tempy += i[1]
                if not ((tempx >= 0 and tempx <= h-1) and (tempy >= 0 and tempy <= w - 1)):
                    break
                if dist + 1 < distance[tempx][tempy] and mylist[tempx][tempy] == ".":
                    distance[tempx][tempy] = dist + 1
                    d.append((tempx, tempy))
                else:
                    break
        if distance[x2][y2] != float('inf'):
            print(distance[x2][y2])
            exit()
    print(-1)
        
main()

Submission status: Wrong Answer

Solution code:
#関数リスト
import sys
input = sys.stdin.readline
from collections import defaultdict, deque

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    h, w, k = MI()
    x1, y1, x2, y2 = MI()
    x1 -= 1
    y1 -= 1
    x2 -= 1
    y2 -= 1

    mylist = [0] * h

    #tableの読み込み
    for i in range(h):
        temp = input().rstrip()
        temp = [temp[j] for j in range(w)]
        mylist[i] = temp
    #初期化
    distance = [[float('inf') for i in range(w)] for j in range(h)]
    distance[x1][y1] = 0
    d = deque()
    start = (x1, y1)
    goal = [x2, y2]
    d.append(start)
    move = ([-1,0],[1,0],[0,1],[0,-1])

    while d:
        pos = d.pop()
        x = pos[0]
        y = pos[1]
        if x == x2 and y == y2:
            break
        dist = distance[x][y]
        for i in move:
            #tempxの初期化
            tempx = x
            tempy = y
            for l in range(k):
                tempx += i[0]
                tempy += i[1]
                if not ((tempx >= 0 and tempx <= h-1) and (tempy >= 0 and tempy <= w - 1)):
                    break
                if dist + 1 < distance[tempx][tempy] and mylist[tempx][tempy] == ".":
                    distance[tempx][tempy] = dist + 1
                    d.append((tempx, tempy))
                else:
                    break
    if distance[x2][y2] == float('inf'):
        print(-1)
    else:
        print(distance[x2][y2])
        
main()

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush 
def dijkstra(xs, ys, xg, yg,h,w,k,field):
    # que->(cost, x, y)
    que = [(0, xs, ys)]
    inf = 10 ** 6
    dp = [[inf for _ in range(w+1)] for _ in range(h+1)]
    visited = [[False for _ in range(w+1)] for _ in range(h+1)]
    dp[xs][ys] = 0
    dx = [-1,0,1,0]
    dy = [0,1,0,-1]
    while que:
        cost, x, y= heappop(que)
        # 現在のコストより高いなら無視する
        # if cost > dp[x][y]:
        #     continue
        if x == xg and y == yg:
            print(cost)
            exit(0)
        if visited[x][y]:
            for i in range(4):
                for j in range(1, k+1):
                    x_, y_ = x+j*dx[i], y+j*dy[i]
                    if not field[x_][y_]:
                        break
                    if dp[x_][y_] > dp[x][y] + 1:
                        dp[x_][y_] = dp[x][y] + 1
                        heappush(que, (dp[x_][y_], x_, y_))
        visited[x][y] = True
    print(-1)
        

def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    h, w, k = map(int, input().split())
    xs, ys, xg, yg = map(int, input().split())
    field = [[False]*(w+2) for _ in range(h+2)]
    for i in range(h):
        # False で覆うことでx,yの制限をなくす。
        s = [True if _ == '.' else False for _ in input()]
        field[i+1] = [False]+s+[False]
    dijkstra(xs, ys, xg, yg, h, w, k, field)

if __name__ == '__main__':
    main()

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())

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

inf=10**7
cost=[[inf]*W for _ in range(H)]
cost[x1][y1]=0
from heapq import heapify,heappop,heappush
hq=[(0,x1,y1)]

while hq:
    t,x,y=heappop(hq)
    #北へ
    for i in range(1,K+1):
        if x-i<0 or pond[x-i][y]=="@" or cost[x-i][y]<=t+1:
            break
        if cost[x-i][y]>t+1:
            cost[x-i][y]=t+1
            heappush(hq,(t+1,x-i,y))
    #東へ
    for i in range(1,K+1):
        if y+i>W-1 or pond[x][y+i]=="@" or cost[x][y+i]<=t+1:
            break
        if cost[x][y+i]>t+1:
            cost[x][y+i]=t+1
            heappush(hq,(t+1,x,y+i))
    #南へ
    for i in range(1,K+1):
        if x+i>H-1 or pond[x+i][y]=="@" or cost[x+i][y]<=t+1:
            break
        if cost[x+i][y]>t+1:
            cost[x+i][y]=t+1
            heappush(hq,(t+1,x+i,y))
    #西へ
    for i in range(1,K+1):
        if y-i<0 or pond[x][y-i]=="@" or cost[x][y-i]<=t+1:
            break
        if cost[x][y-i]>t+1:
            cost[x][y-i]=t+1
            heappush(hq,(t+1,x,y-i))

if cost[x2][y2]==inf:
    print(-1)
    exit()  
print(cost[x2][y2])

Submission status: Wrong Answer

Solution code:
#16:07
h,w,k = map(int,input().split())
x1,y1,x2,y2 = map(lambda x:int(x)-1,input().split())
nex = [[di,x1,y1] for di in range(4)]
raw = []
for _ in range(h):
  raw.append(input())
inf = 10**12
score = [[[inf for _ in range(w)] for _ in range(h)] for _ in range(4)]
seen = [[[0 for _ in range(w)] for _ in range(h)] for _ in range(4)]
for di in range(4):
  score[di][x1][y1] = 0
while nex:
  now = nex[:]
  nex = []
  for i in range(k):
    last = now[:]
    now = []
    #print(last)
    for dixy in last:
      di,x,y = dixy
      if seen[di][x][y]:
        continue
      if x == x2 and y == y2:
        print(score[di][x][y])
        exit()
      seen[di][x][y] = 1
      if di == 0 and x != 0:
        if score[0][x-1][y] > score[0][x][y] + 1-int(bool(i)) and raw[x-1][y] == '.':
          score[0][x-1][y] = score[0][x][y] + 1-int(bool(i))
          now.append([0,x-1,y])
      if di == 1 and x != h-1:
        if score[1][x+1][y] > score[1][x][y] + 1-int(bool(i)) and raw[x+1][y] == '.':
          score[1][x+1][y] = score[1][x][y] + 1-int(bool(i))
          now.append([1,x+1,y])
      if di == 2 and y != 0:
        if score[2][x][y-1] > score[2][x][y] + 1-int(bool(i)) and raw[x][y-1] == '.':
          score[2][x][y-1] = score[2][x][y] + 1-int(bool(i))
          now.append([2,x,y-1])
      if di == 3 and y != w-1:
        if score[3][x][y+1] > score[3][x][y] + 1-int(bool(i)) and raw[x][y+1] == '.':
          score[3][x][y+1] = score[3][x][y] + 1-int(bool(i))
          now.append([3,x,y+1])
      for dj in range(4):
        #print(di,dj,score[dj][x][y],score[di][x][y])
        if di != dj and score[dj][x][y] > score[di][x][y]:
          #print('!')
          score[dj][x][y] = score[di][x][y]
          nex.append([dj,x,y])
  nex += now
  #print('#',nex)
print(-1)

Submission status: Wrong Answer

Solution code:
#16:07
h,w,k = map(int,input().split())
x1,y1,x2,y2 = map(lambda x:int(x)-1,input().split())
nex = [[di,x1,y1] for di in range(4)]
raw = []
for _ in range(h):
  raw.append(input())
inf = 10**12
score = [[[inf for _ in range(w)] for _ in range(h)] for _ in range(4)]
seen = [[[0 for _ in range(w)] for _ in range(h)] for _ in range(4)]
for di in range(4):
  score[di][x1][y1] = 0
while nex:
  now = nex[:]
  nex = []
  for i in range(k):
    last = now[:]
    now = []
    #print(last)
    for dixy in last:
      di,x,y = dixy
      if seen[di][x][y]:
        continue
      seen[di][x][y] = 1
      if di == 0 and x != 0:
        if score[0][x-1][y] > score[0][x][y] + 1-int(bool(i)) and raw[x-1][y] == '.':
          score[0][x-1][y] = score[0][x][y] + 1-int(bool(i))
          now.append([0,x-1,y])
      if di == 1 and x != h-1:
        if score[1][x+1][y] > score[1][x][y] + 1-int(bool(i)) and raw[x+1][y] == '.':
          score[1][x+1][y] = score[1][x][y] + 1-int(bool(i))
          now.append([1,x+1,y])
      if di == 2 and y != 0:
        if score[2][x][y-1] > score[2][x][y] + 1-int(bool(i)) and raw[x][y-1] == '.':
          score[2][x][y-1] = score[2][x][y] + 1-int(bool(i))
          now.append([2,x,y-1])
      if di == 3 and y != w-1:
        if score[3][x][y+1] > score[3][x][y] + 1-int(bool(i)) and raw[x][y+1] == '.':
          score[3][x][y+1] = score[3][x][y] + 1-int(bool(i))
          now.append([3,x,y+1])
      for dj in range(4):
        #print(di,dj,score[dj][x][y],score[di][x][y])
        if di != dj and score[dj][x][y] > score[di][x][y]:
          #print('!')
          score[dj][x][y] = score[di][x][y]
          nex.append([dj,x,y])
  nex += now
  #print('#',nex)
  for di in range(4):
    if score[di][x2][y2] != inf:
      print(score[di][x2][y2])
      break
  else:
    continue
  break
else:
  print(-1)
for xx in score:
  for yy in xx:
    #print(yy)
    pass
for xx in seen:
  for yy in xx:
    #print(yy)
    pass

Submission status: Wrong Answer

Solution code:
#16:07
h,w,k = map(int,input().split())
x1,y1,x2,y2 = map(lambda x:int(x)-1,input().split())
nex = [[di,x1,y1] for di in range(4)]
raw = []
for _ in range(h):
  raw.append(input())
inf = 10**6
score = [[[inf for _ in range(w)] for _ in range(h)] for _ in range(4)]
seen = [[[0 for _ in range(w)] for _ in range(h)] for _ in range(4)]
for di in range(4):
  score[di][x1][y1] = 0
while nex:
  now = nex[:]
  nex = []
  for i in range(k):
    last = now[:]
    now = []
    #print(last)
    for dixy in last:
      di,x,y = dixy
      if seen[di][x][y]:
        continue
      seen[di][x][y] = 1
      if di == 0 and x != 0:
        if score[0][x-1][y] > score[0][x][y] + 1-int(bool(i)) and raw[x-1][y] == '.':
          score[0][x-1][y] = score[0][x][y] + 1-int(bool(i))
          now.append([0,x-1,y])
      if di == 1 and x != h-1:
        if score[1][x+1][y] > score[1][x][y] + 1-int(bool(i)) and raw[x+1][y] == '.':
          score[1][x+1][y] = score[1][x][y] + 1-int(bool(i))
          now.append([1,x+1,y])
      if di == 2 and y != 0:
        if score[2][x][y-1] > score[2][x][y] + 1-int(bool(i)) and raw[x][y-1] == '.':
          score[2][x][y-1] = score[2][x][y] + 1-int(bool(i))
          now.append([2,x,y-1])
      if di == 3 and y != w-1:
        if score[3][x][y+1] > score[3][x][y] + 1-int(bool(i)) and raw[x][y+1] == '.':
          score[3][x][y+1] = score[3][x][y] + 1-int(bool(i))
          now.append([3,x,y+1])
      for dj in range(4):
        #print(di,dj,score[dj][x][y],score[di][x][y])
        if di != dj and score[dj][x][y] > score[di][x][y]:
          #print('!')
          score[dj][x][y] = score[di][x][y]
          nex.append([dj,x,y])
  nex += now
  #print('#',nex)
  for di in range(4):
    if score[di][x2][y2] != inf:
      print(score[di][x2][y2])
      break
  else:
    continue
  break
else:
  print(-1)
for xx in score:
  for yy in xx:
    #print(yy)
    pass
for xx in seen:
  for yy in xx:
    #print(yy)
    pass

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:
            _x, _y = x+dx, y+dy
            if 0 <= _x < H and 0 <= _y < W and S[_x][_y] == '.':
                _h = geth(_x, _y, z)
                if D[_h] <= d+1:
                    continue
                heappush(q, (d+1, _h))
                D[_h] = d+1
        else:
            _h = geth(x, y, _z)
            _d = (d-1)//K*K+K
            if D[_h] > _d:
                heappush(q, (_d, _h))
                D[_h] = _d

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