Problem:


Score : 600 points

Problem Statement

Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.

Some of the squares have a lotus leaf on it and cannot be entered.
The square (i,j) has a lotus leaf on it if c_{ij} is @, and it does not if c_{ij} is ..

In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west.
The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.

Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2).
If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.

Constraints

1 \leq H,W,K \leq 10^6

H \times W \leq 10^6

1 \leq x_1,x_2 \leq H

1 \leq y_1,y_2 \leq W

x_1 \neq x_2 or y_1 \neq y_2.

c_{i,j} is . or @.

c_{x_1,y_1} = .

c_{x_2,y_2} = .

All numbers in input are integers.

Input

Input is given from Standard Input in the following format:

H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}

Output

Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print -1 if the travel is impossible.

Sample Input 1

3 5 2
3 2 3 4
.....
.@..@
..@..

Sample Output 1

5

Initially, Snuke is at the square (3,2).
He can reach the square (3, 4) by making five strokes as follows:

From (3, 2), go west one square to (3, 1).

From (3, 1), go north two squares to (1, 1).

From (1, 1), go east two squares to (1, 3).

From (1, 3), go east one square to (1, 4).

From (1, 4), go south two squares to (3, 4).

Sample Input 2

1 6 4
1 1 1 6
......

Sample Output 2

2

Sample Input 3

3 3 1
2 1 2 3
.@.
.@.
.@.

Sample Output 3

-1



Sample input:
3 5 2
3 2 3 4
.....
.@..@
..@..


Sample output:
5

Submission status: Wrong Answer

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

def calc_next_point(x, y, vec, dist):
    if vec == 0:
        return x-dist, y
    elif vec == 1:
        return x, y+dist
    elif vec == 2:
        return x+dist, y
    else:
        return x, y-dist

def main():
    h, w, k = map(int, input().split())
    x1, y1, x2, y2 = map(int, input().split())
    pond = [[[False]*4 for j in range(w+2)] for i in range(h+2)]
    for i in range(1, h+1):
        s = input()
        for j in range(w):
            if s[j] == ".":
                for l in range(4):
                    pond[i][j+1][l] = True
    qs = [queue.Queue(), queue.Queue()]
    for i in range(4):
        pond[x1][y1][i] = False
        qs[0].put([0, x1, y1, i])
    ans = [float("inf")]*4
    p = 0
    minans = float("inf")
    while not qs[0].empty() or not qs[1].empty():
        while not qs[p].empty():
            cost, x, y, vec = qs[p].get()
            if cost > minans:
                break
            if x == x2 and y == y2 and ans[vec] > cost:
                ans[vec] = cost
                if cost < minans:
                    minans = cost
            for i in range(1, k+1):
                nx, ny = calc_next_point(x, y, vec, i)
                if not pond[nx][ny][vec]:
                    break
                ncost = cost + 1
                np = (p+1)%2
                if nx == x2 and ny == y2 and ans[vec] > ncost:
                    ans[vec] = ncost
                    if ncost < minans:
                        minans = ncost
                for j in range(4):
                    if i == k or j != vec:
                        mx, my = calc_next_point(nx, ny, j, 0)
                        if pond[mx][my][j]:
                            if mx == x2 and my == y2 and ans[j] > ncost:
                                ans[j] = ncost
                                if ncost < minans:
                                    minans = ncost
                            qs[np].put([ncost, mx, my, j])
                            pond[mx][my][j] = False
                pond[nx][ny][vec] = False
        p = (p+1)%2
    if min(ans) == float("inf"):
        print(-1)
    else:
        print(min(ans))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

def calc_next_point(x, y, vec, dist):
    if vec == 0:
        return x-dist, y
    elif vec == 1:
        return x, y+dist
    elif vec == 2:
        return x+dist, y
    else:
        return x, y-dist

def main():
    h, w, k = map(int, input().split())
    x1, y1, x2, y2 = map(int, input().split())
    pond = [[[False]*4 for j in range(w+2)] for i in range(h+2)]
    for i in range(1, h+1):
        s = input()
        for j in range(w):
            if s[j] == ".":
                for l in range(4):
                    pond[i][j+1][l] = True
    qs = [queue.Queue(), queue.Queue()]
    for i in range(4):
        pond[x1][y1][i] = False
        qs[0].put([0, x1, y1, i])
    ans = [float("inf")]*4
    p = 0
    minans = float("inf")
    while not qs[0].empty() or not qs[1].empty():
        while not qs[p].empty():
            cost, x, y, vec = qs[p].get()
            if cost > minans+100:
                break
            if x == x2 and y == y2 and ans[vec] > cost:
                ans[vec] = cost
                if cost < minans:
                    minans = cost
            for i in range(1, k+1):
                nx, ny = calc_next_point(x, y, vec, i)
                if not pond[nx][ny][vec]:
                    break
                ncost = cost + 1
                np = (p+1)%2
                if nx == x2 and ny == y2 and ans[vec] > ncost:
                    ans[vec] = ncost
                    if ncost < minans:
                        minans = ncost
                for j in range(4):
                    if i == k or j != vec:
                        mx, my = calc_next_point(nx, ny, j, 0)
                        if pond[mx][my][j]:
                            qs[np].put([ncost, mx, my, j])
                            pond[mx][my][j] = False
                pond[nx][ny][vec] = False
        p = (p+1)%2
    if min(ans) == float("inf"):
        print(-1)
    else:
        print(min(ans))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

def calc_next_point(x, y, vec, dist):
    if vec == 0:
        return x-dist, y
    elif vec == 1:
        return x, y+dist
    elif vec == 2:
        return x+dist, y
    else:
        return x, y-dist

def main():
    h, w, k = map(int, input().split())
    x1, y1, x2, y2 = map(int, input().split())
    pond = [[[False]*4 for j in range(w+2)] for i in range(h+2)]
    for i in range(1, h+1):
        s = input()
        for j in range(w):
            if s[j] == ".":
                for l in range(4):
                    pond[i][j+1][l] = True
    qs = [queue.Queue(), queue.Queue()]
    for i in range(4):
        pond[x1][y1][i] = False
        qs[0].put([0, x1, y1, i])
    ans = [float("inf")]*4
    p = 0
    minans = float("inf")
    while not qs[0].empty() or not qs[1].empty():
        while not qs[p].empty():
            cost, x, y, vec = qs[p].get()
            if cost > minans+2:
                break
            if x == x2 and y == y2 and ans[vec] > cost:
                ans[vec] = cost
                if cost < minans:
                    minans = cost
            for i in range(1, k+1):
                nx, ny = calc_next_point(x, y, vec, i)
                if not pond[nx][ny][vec]:
                    break
                ncost = cost + 1
                np = (p+1)%2
                if nx == x2 and ny == y2 and ans[vec] > ncost:
                    ans[vec] = ncost
                    if ncost < minans:
                        minans = ncost
                for j in range(4):
                    if i == k or j != vec:
                        mx, my = calc_next_point(nx, ny, j, 0)
                        if pond[mx][my][j]:
                            qs[np].put([ncost, mx, my, j])
                            pond[mx][my][j] = False
                pond[nx][ny][vec] = False
        p = (p+1)%2
    if min(ans) == float("inf"):
        print(-1)
    else:
        print(min(ans))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

def calc_next_point(x, y, vec, dist):
    if vec == 0:
        return x-dist, y
    elif vec == 1:
        return x, y+dist
    elif vec == 2:
        return x+dist, y
    else:
        return x, y-dist

def main():
    h, w, k = map(int, input().split())
    x1, y1, x2, y2 = map(int, input().split())
    pond = [[[False]*4 for j in range(w+2)] for i in range(h+2)]
    for i in range(1, h+1):
        s = input()
        for j in range(w):
            if s[j] == ".":
                for l in range(4):
                    pond[i][j+1][l] = True
    qs = [queue.Queue(), queue.Queue()]
    for i in range(4):
        pond[x1][y1][i] = False
        qs[0].put([0, x1, y1, i])
    ans = [float("inf")]*4
    p = 0
    minans = float("inf")
    while not qs[0].empty() or not qs[1].empty():
        while not qs[p].empty():
            cost, x, y, vec = qs[p].get()
            if cost > minans:
                break
            if x == x2 and y == y2 and ans[vec] > cost:
                ans[vec] = cost
                if cost < minans:
                    minans = cost
            for i in range(1, k+1):
                nx, ny = calc_next_point(x, y, vec, i)
                if not pond[nx][ny][vec]:
                    break
                ncost = cost + 1
                np = (p+1)%2
                if nx == x2 and ny == y2 and ans[vec] > ncost:
                    ans[vec] = ncost
                    if ncost < minans:
                        minans = ncost
                for j in range(4):
                    if i == k or j != vec:
                        mx, my = calc_next_point(nx, ny, j, 0)
                        if pond[mx][my][j]:
                            qs[np].put([ncost, mx, my, j])
                            pond[mx][my][j] = False
                pond[nx][ny][vec] = False
        p = (p+1)%2
    if min(ans) == float("inf"):
        print(-1)
    else:
        print(min(ans))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

def calc_next_point(x, y, vec, dist):
    if vec == 0:
        return x-dist, y
    elif vec == 1:
        return x, y+dist
    elif vec == 2:
        return x+dist, y
    else:
        return x, y-dist

def main():
    h, w, k = map(int, input().split())
    x1, y1, x2, y2 = map(int, input().split())
    pond = [[1]*(w+2) for _ in range(h+2)]
    for i in range(1, h+1):
        s = input()
        for j in range(w):
            if s[j] == ".":
                pond[i][j+1] = 0
    q = queue.Queue()
    row = [[[float("inf")]*4 for j in range(w+2)] for i in range(h+2)]
    for i in range(4):
        row[x1][y1][i] = 0
        nx, ny = calc_next_point(x1, y1, i, 1)
        if pond[nx][ny] == 0:
            row[nx][ny][i] == 1
            q.put([1, nx, ny, i])
    while not q.empty():
        cost, x, y, vec = q.get()
        if x == x2 and y == y2:
            break
        for i in range(k):
            nx, ny = calc_next_point(x, y, vec, i)
            if pond[nx][ny] == 1:
                break
            row[nx][ny][vec] = cost
            for j in range(4):
                mx, my = calc_next_point(nx, ny, j, 1)
                if pond[mx][my] == 1:
                    continue
                if row[mx][my][j] <= cost+1:
                    continue
                if i < k-1 and j == vec:
                    continue
                if row[mx][my][j] > cost+1:
                    row[mx][my][j] = cost+1
                    q.put([cost+1, mx, my, j])
    ans = min(row[x2][y2])
    if ans == float("inf"):
        print(-1)
    else:
        print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

def update(x, y, k, cost, pt, vec, pond, row, q, fs):
    for i in range(1, k+1):
        nx, ny = x, y
        nc = cost+1
        if vec == 0:
            nx -= i
        elif vec == 1:
            ny += i
        elif vec == 2:
            nx += i
        else:
            ny -= i
        if pond[nx][ny] == 1:
            return
        if row[nx][ny][vec] > nc:
            row[nx][ny][vec] = nc
            if i == k:
                q.put([nc, nx, ny, i, vec])
            if fs[nx][ny]:
                for j in range(1, 4):
                    if row[nx][ny][(vec+j)%4] > nc:
                        row[nx][ny][(vec+j)%4] = nc
                        q.put([nc, nx, ny, k, (vec+j)%4])
                fs[nx][ny] = False
        else:
            return

def main():
    h, w, k = map(int, input().split())
    x1, y1, x2, y2 = map(int, input().split())
    pond = [[1]*(w+2) for _ in range(h+2)]
    for i in range(1, h+1):
        s = input()
        for j in range(w):
            if s[j] == ".":
                pond[i][j+1] = 0
    q = queue.Queue()
    row = [[[float("inf")]*4 for j in range(w+2)] for i in range(h+2)]
    fs = [[True]*(w+2) for _ in range(h+2)]
    for i in range(4):
        row[x1][y1][i] = 0
        q.put([0, x1, y1, k, i])
    while not q.empty():
        cost, x, y, pt, vec = q.get()
        if cost > row[x][y][vec]:
            continue
        if x == x2 and y == y2:
            break
        update(x, y, k, cost, pt, vec, pond, row, q, fs)
    ans = min(row[x2][y2])
    if ans == float("inf"):
        print(-1)
    else:
        print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

def update(x, y, k, cost, pt, vec, pond, row, hq):
    for i in range(1, k+1):
        nx, ny = x, y
        nc = cost+1
        if vec == 0:
            nx -= i
        elif vec == 1:
            ny += i
        elif vec == 2:
            nx += i
        else:
            ny -= i
        if pond[nx][ny] == 1:
            return
        if row[nx][ny][vec] > nc:
            row[nx][ny][vec] = nc
            heapq.heappush(hq, [nc, nx, ny, i, vec])
            for j in range(1, 4):
                if row[nx][ny][(vec+j)%4] > nc:
                    row[nx][ny][(vec+j)%4] = nc
                    heapq.heappush(hq, [nc, nx, ny, k, (vec+j)%4])
        else:
            return

def main():
    h, w, k = map(int, input().split())
    x1, y1, x2, y2 = map(int, input().split())
    pond = [[1]*(w+2) for _ in range(h+2)]
    for i in range(1, h+1):
        s = input()
        for j in range(w):
            if s[j] == ".":
                pond[i][j+1] = 0
    hq = []
    row = [[[float("inf")]*4 for j in range(w+2)] for i in range(h+2)]
    for i in range(4):
        row[x1][y1][i] = 0
        heapq.heappush(hq, [0, x1, y1, k, i])
    while len(hq) > 0:
        cost, x, y, pt, vec = heapq.heappop(hq)
        if cost > row[x][y][vec]:
            continue
        if x == x2 and y == y2:
            break
        if pt == k:
            update(x, y, k, cost, pt, vec, pond, row, hq)
    ans = min(row[x2][y2])
    if ans == float("inf"):
        print(-1)
    else:
        print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
print(-1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

H,W,K=map(int,input().split())
sy,sx,gy,gx=map(int,input().split())
st=sx+sy*(W+2)
gl=gx+gy*(W+2)
c=['@']*(W+2)
for i in range(H):
    c.extend(['@']+list(input())+['@'])
c.extend(['@']*(W+2))
que=deque()
dist=[INF]*(W+2)*(H+2)
que.append(st)
dist[st]=0
d=(-1,1,W+2,-(W+2))
while que:
    v=que.popleft()
    tmp_dist=dist[v]+1
    for k in range(4):
        nv=v
        for _ in range(K):
            nv+=+d[k]
            if c[nv]=='.' and dist[nv]>tmp_dist:
                    dist[nv]=tmp_dist
                    que.append(nv)
            else:
                break
print(dist[gl] if dist[gl]!=INF else -1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

H,W,K=map(int,input().split())
sy,sx,gy,gx=map(int,input().split())
st=sx+sy*(W+2)
gl=gx+gy*(W+2)
c=['@']*(W+2)
for i in range(H):
    c.extend(['@']+list(input())+['@'])
c.extend(['@']*(W+2))
que=deque()
d=[INF]*(W+2)*(H+2)
direction=[[0]*4 for _ in range((H+2)*(W+2))]
que.append(st)
d[st]=0
di=(-1,1,W+2,-(W+2))
while que:
    v=que.popleft()
    for k in range(4):
        nv=v+di[k]
        if c[nv]=='.':
            if direction[v][k]>0:
                if d[nv]>d[v]:
                    direction[nv]=[0]*4
                    direction[nv][k]=max(direction[nv][k],direction[v][k]-1)
                    direction[v][k]=0
                    d[nv]=d[v]
                    que.appendleft(nv)
                elif d[nv]==d[v]:
                    direction[v][k]=0
                    direction[nv][k]=max(direction[nv][k],direction[v][k]-1)
            else:
                if d[nv]>d[v]+1:
                    direction[nv]=[0]*4
                    direction[nv][k]=K-1
                    direction[v][k]=0
                    d[nv]=d[v]+1
                    que.append(nv)
                elif d[nv]==d[v]+1:
                    direction[v][k]=0
                    direction[nv][k]=K-1
print(d[gl] if d[gl]!=INF else -1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

H,W,K=map(int,input().split())
sy,sx,gy,gx=map(int,input().split())
st=sx+sy*(W+2)
gl=gx+gy*(W+2)
c=['@']*(W+2)
for i in range(H):
    c.extend(['@']+list(input())+['@'])
c.extend(['@']*(W+2))
que=deque()
d=[INF]*(W+2)*(H+2)
direction=[[0]*4 for _ in range((H+2)*(W+2))]
que.append(st)
d[st]=0
di=(-1,1,W+2,-(W+2))
while que:
    v=que.popleft()
    for k in range(4):
        nv=v+di[k]
        if c[nv]=='.':
            if direction[v][k]>0:
                if d[nv]>d[v]:
                    direction[nv]=[0]*4
                    direction[nv][k]=direction[v][k]-1
                    direction[v][k]=0
                    d[nv]=d[v]
                    que.appendleft(nv)
                elif d[nv]==d[v]:
                    direction[v][k]=0
                    direction[nv][k]=direction[v][k]-1
            else:
                if d[nv]>d[v]+1:
                    direction[nv]=[0]*4
                    direction[nv][k]=K-1
                    direction[v][k]=0
                    d[nv]=d[v]+1
                    que.append(nv)
                elif d[nv]==d[v]+1:
                    direction[v][k]=0
                    direction[nv][k]=K-1
print(d[gl] if d[gl]!=INF else -1)

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush 
from collections import deque
def dijkstra(xs, ys, xg, yg,h,w,k,field):
    # que->(cost, x, y, direction)
    inf = 1e18
    dist = [[inf]*w for _ in range(h)]
    dist[xs][ys] = 0
    que = deque([(0, xs, ys)])
    dx = [-1, 0, 1, 0]
    dy = [0, 1, 0, -1]
    while que:
        cost, x, y = que.popleft()
        if cost > dist[x][y]:
            continue
        for v in range(4):
            nx, ny = x, y
            for _ in range(k):
                nx += dx[v]
                ny += dy[v]
                if not field[nx][ny]:
                    break
                if dist[nx][ny]<=dist[x][y]:
                    break
                if dist[nx][ny] > dist[x][y]+1:
                    dist[nx][ny] = dist[x][y]+1
                    que.append((dist[nx][ny], nx, ny))

    if dist[nx][ny] == inf:
        print(-1)
    else:
        print(dist[nx][ny])

        

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]
    h += 2
    w += 2
    dijkstra(xs, ys, xg, yg, h, w, k, field)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush 
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    def dijkstra():
        inf = 10 ** 18

        dp = [[inf]*(w+2) for _ in range(h+2)]
        dp[xs][ys] = 0

        que = [(0, xs, ys, 0),(0, xs, ys, 2)]
        direction = [[-1, 0], [0, 1], [1, 0], [0, -1]]
        while que:
            cost, x, y, dirt = heappop(que)

            if cost > dp[x][y]:
                continue

            if (x, y) == (xg, yg):
                ans = (dp[x][y] + (-dp[x][y]) % k)//k
                return ans

            for i in range(4):
                nx, ny = x + direction[i][0], y + direction[i][1]
                if field[nx][ny]:
                    if i == dirt:
                        if dp[nx][ny] > dp[x][y] + 1:
                            dp[nx][ny] = dp[x][y] + 1
                            heappush(que, (dp[nx][ny], nx, ny, i))
                    elif i != (dirt+2)%4:
                        if dp[nx][ny] > dp[x][y] + (-dp[x][y])%k + 1:
                            dp[nx][ny] = dp[x][y] + (k - dp[x][y]%k)%k + 1
                            heappush(que, (dp[nx][ny], nx, ny, i))
        return -1


    h, w, k= map(int, input().split())
    xs, ys, xg, yg = map(int, input().split())
    field = [[False]*(w+2)]
    for _ in range(h):
        s = [False]
        s += [True if i == '.' else False for i in input()]
        s += [False]
        field.append(s)
    field.append([False]*(w+2))
    ans = dijkstra()
    print(ans)




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush 
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    def dijkstra():
        inf = 10 ** 9

        dp = [[inf]*(w+2) for _ in range(h+2)]
        dp[xs][ys] = 0

        que = [(0, xs, ys, 0),(0, xs, ys, 2)]
        direction = [[-1, 0], [0, 1], [1, 0], [0, -1]]
        while que:
            cost, x, y, dirt = heappop(que)

            if cost > dp[x][y]:
                continue

            if (x, y) == (xg, yg):
                ans = (dp[x][y] + k - 1)//k
                return ans

            for i in range(4):
                nx, ny = x + direction[i][0], y + direction[i][1]
                if field[nx][ny]:
                    if i == dirt:
                        if dp[nx][ny] > dp[x][y] + 1:
                            dp[nx][ny] = dp[x][y] + 1
                            heappush(que, (dp[nx][ny], nx, ny, i))
                    elif i != (dirt+2)%4:
                        if dp[nx][ny] > dp[x][y] + (k - dp[x][y]%k)%k + 1:
                            dp[nx][ny] = dp[x][y] + (k - dp[x][y]%k)%k + 1
                            heappush(que, (dp[nx][ny], nx, ny, i))
        return -1


    h, w, k= map(int, input().split())
    xs, ys, xg, yg = map(int, input().split())
    field = [[False]*(w+2)]
    for _ in range(h):
        s = [False]
        s += [True if i == '.' else False for i in input()]
        s += [False]
        field.append(s)
    field.append([False]*(w+2))
    ans = dijkstra()
    print(ans)




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush 
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    def dijkstra():
        inf = 10 ** 9
        # visited = [[False]*(w+2) for _ in range(h+2)]

        dp = [[inf]*(w+2) for _ in range(h+2)]
        dp[xs][ys] = 0

        que = [(0, xs, ys, 0)]
        direction = [[-1, 0], [0, 1], [1, 0], [0, -1]]
        while que:
            cost, x, y, dirt = heappop(que)

            if cost > dp[x][y]:
                continue

            if (x, y) == (xg, yg):
                ans = (dp[x][y] + k - 1)//k
                return ans

            for i in range(4):
                nx, ny = x + direction[i][0], y + direction[i][1]
                if field[nx][ny]:
                    if i == dirt:
                        if dp[nx][ny] > dp[x][y] + 1:
                            dp[nx][ny] = dp[x][y] + 1
                            heappush(que, (dp[nx][ny], nx, ny, i))
                    else:
                        if dp[nx][ny] > dp[x][y] + (k - dp[x][y]%k)%k + 1:
                            dp[nx][ny] = dp[x][y] + (k - dp[x][y]%k)%k + 1
                            heappush(que, (dp[nx][ny], nx, ny, i))
            # visited[x][y] = True
        return -1


    h, w, k= map(int, input().split())
    xs, ys, xg, yg = map(int, input().split())
    field = [[False]*(w+2)]
    for _ in range(h):
        s = [False]
        s += [True if i == '.' else False for i in input()]
        s += [False]
        field.append(s)
    field.append([False]*(w+2))
    ans = dijkstra()
    print(ans)




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush 
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    def dijkstra():
        inf = 10 ** 9
        visited = [[False]*(w+2) for _ in range(h+2)]

        dp = [[inf]*(w+2) for _ in range(h+2)]
        dp[xs][ys] = 0

        que = [(0, xs, ys, 0)]
        direction = [[-1, 0], [0, 1], [1, 0], [0, -1]]
        while que:
            cost, x, y, dirt = heappop(que)
            if (x, y) == (xg, yg):
                ans = (dp[x][y] + k - 1)//k
                return ans

            if not visited[x][y]:
                for i in range(4):
                    nx, ny = x + direction[i][0], y + direction[i][1]
                    if field[nx][ny]:
                        if i == dirt:
                            if dp[nx][ny] > dp[x][y] + 1:
                                dp[nx][ny] = dp[x][y] + 1
                                heappush(que, (dp[nx][ny], nx, ny, i))
                        else:
                            if dp[nx][ny] > dp[x][y] + (k - dp[x][y]%k)%k + 1:
                                dp[nx][ny] = dp[x][y] + (k - dp[x][y]%k)%k + 1
                                heappush(que, (dp[nx][ny], nx, ny, i))
                visited[x][y] = True
        return -1


    h, w, k= map(int, input().split())
    xs, ys, xg, yg = map(int, input().split())
    field = [[False]*(w+2)]
    for _ in range(h):
        s = [False]
        s += [True if i == '.' else False for i in input()]
        s += [False]
        field.append(s)
    field.append([False]*(w+2))
    ans = dijkstra()
    print(ans)




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush 
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    def dijkstra():
        inf = 10 ** 9
        visited = [[False]*(w+2) for _ in range(h+2)]

        dp = [[inf]*(w+2) for _ in range(h+2)]
        dp[xs][ys] = 0

        que = [(0, xs, ys, 0)]
        direction = [[-1, 0], [0, 1], [1, 0], [0, -1]]
        while que:
            cost, x, y, dirt = heappop(que)
            # if (x, y) == (xg, yg):
            #     ans = (dp[x][y] + k - 1)//k
            #     return ans

            if not visited[x][y]:
                for i in range(4):
                    nx, ny = x + direction[i][0], y + direction[i][1]
                    if field[nx][ny]:
                        if i == dirt:
                            if dp[nx][ny] > dp[x][y] + 1:
                                dp[nx][ny] = dp[x][y] + 1
                                heappush(que, (dp[nx][ny], nx, ny, i))
                        else:
                            if dp[nx][ny] > (dp[x][y] + k-1)//k*k + 1:
                                dp[nx][ny] = (dp[x][y] + k-1)//k*k + 1
                                heappush(que, (dp[nx][ny], nx, ny, i))
                visited[x][y] = True
        if dp[xg][yg] == inf:
            return -1
        else:
            return (dp[xg][yg] + k - 1)//k


    h, w, k= map(int, input().split())
    xs, ys, xg, yg = map(int, input().split())
    field = [[False]*(w+2)]
    for _ in range(h):
        s = [False]
        s += [True if i == '.' else False for i in input()]
        s += [False]
        field.append(s)
    field.append([False]*(w+2))
    ans = dijkstra()
    print(ans)




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush 
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    def dijkstra():
        inf = 10 ** 15
        visited = [[False]*(w+2) for _ in range(h+2)]

        dp = [[inf]*(w+2) for _ in range(h+2)]
        dp[xs][ys] = 0

        que = [(0, xs, ys, 0)]
        direction = [[-1, 0], [0, 1], [1, 0], [0, -1]]
        while que:
            cost, x, y, dirt = heappop(que)
            if (x, y) == (xg, yg):
                ans = (dp[x][y] + k - 1)//k
                return ans

            if not visited[x][y]:
                for i in range(4):
                    nx, ny = x + direction[i][0], y + direction[i][1]
                    if field[nx][ny]:
                        if i == dirt:
                            if dp[nx][ny] > dp[x][y] + 1:
                                dp[nx][ny] = dp[x][y] + 1
                                heappush(que, (dp[nx][ny], nx, ny, i))
                        else:
                            if dp[nx][ny] > (dp[x][y] + k-1)//k*k + 1:
                                dp[nx][ny] = (dp[x][y] + k-1)//k*k + 1
                                heappush(que, (dp[nx][ny], nx, ny, i))
                visited[x][y] = True
        return -1


    h, w, k= map(int, input().split())
    xs, ys, xg, yg = map(int, input().split())
    field = [[False]*(w+2)]
    for _ in range(h):
        s = [False]
        s += [True if i == '.' else False for i in input()]
        s += [False]
        field.append(s)
    field.append([False]*(w+2))
    ans = dijkstra()
    print(ans)




if __name__ == '__main__':
    main()