Buggy python submission (verdict: Time Limit Exceeded):
h,w,k = map(int, input().split())
sx,sy,gx,gy = map(lambda x: int(x)-1, input().split())
board = [list(map(lambda x: int(x=="."), input())) for i in range(h)]
inf = 1<<30
DP = [[inf for j in range(w)] for i in range(h)]
DP[sx][sy] = 0

que = [(sx,sy)]
dxdy = [(0,1),(1,0),(0,-1),(-1,0)]
for x,y in que:
    for dx,dy in dxdy:
        nx,ny = x,y
        for i in range(k):
            nx += dx
            ny += dy
            if not(0<=nx<h and 0<=ny<w and board[nx][ny] and DP[nx][ny] >= DP[x][y]+1):
                break
            if DP[nx][ny] > DP[x][y]+1:
                DP[nx][ny] = DP[x][y]+1
            que.append((nx,ny))
ans = DP[gx][gy]
if ans == inf:
    ans = -1
print(ans)


Fixed submission:
h,w,k = map(int, input().split())
sx,sy,gx,gy = map(lambda x: int(x)-1, input().split())
board = [list(map(lambda x: int(x=="."), input())) for i in range(h)]
inf = 1<<30
DP = [[inf for j in range(w)] for i in range(h)]
DP[sx][sy] = 0

from collections import deque
que = deque([(sx,sy)])

dxdy = [(0,1),(1,0),(0,-1),(-1,0)]
while que:
    x,y = que.popleft()
    for dx,dy in dxdy:
        nx,ny = x,y
        for i in range(k):
            nx += dx
            ny += dy
            if not(0<=nx<h and 0<=ny<w and board[nx][ny] and DP[nx][ny] >= DP[x][y]+1):
                break
            # コストが変わらない場合は無視か...
            if DP[nx][ny] == DP[x][y]+1:
                continue
            DP[nx][ny] = DP[x][y]+1
            que.append((nx,ny))
ans = DP[gx][gy]
if ans == inf:
    ans = -1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
h,w,k = map(int, input().split())
sx,sy,gx,gy = map(lambda x: int(x)-1, input().split())
board = [list(map(lambda x: int(x=="."), input())) for i in range(h)]
inf = 1<<30
DP = [[inf for j in range(w)] for i in range(h)]
DP[sx][sy] = 0

from collections import deque
que = deque([(sx,sy)])

dxdy = [(0,1),(1,0),(0,-1),(-1,0)]
while que:
    x,y = que.popleft()
    for dx,dy in dxdy:
        nx,ny = x,y
        for i in range(k):
            nx += dx
            ny += dy
            if not(0<=nx<h and 0<=ny<w and board[nx][ny] and DP[nx][ny] >= DP[x][y]+1):
                break
            if DP[nx][ny] > DP[x][y]+1:
                DP[nx][ny] = DP[x][y]+1
            que.append((nx,ny))
ans = DP[gx][gy]
if ans == inf:
    ans = -1
print(ans)


Fixed submission:
h,w,k = map(int, input().split())
sx,sy,gx,gy = map(lambda x: int(x)-1, input().split())
board = [list(map(lambda x: int(x=="."), input())) for i in range(h)]
inf = 1<<30
DP = [[inf for j in range(w)] for i in range(h)]
DP[sx][sy] = 0

from collections import deque
que = deque([(sx,sy)])

dxdy = [(0,1),(1,0),(0,-1),(-1,0)]
while que:
    x,y = que.popleft()
    for dx,dy in dxdy:
        nx,ny = x,y
        for i in range(k):
            nx += dx
            ny += dy
            if not(0<=nx<h and 0<=ny<w and board[nx][ny] and DP[nx][ny] >= DP[x][y]+1):
                break
            # コストが変わらない場合は無視か...
            if DP[nx][ny] == DP[x][y]+1:
                continue
            DP[nx][ny] = DP[x][y]+1
            que.append((nx,ny))
ans = DP[gx][gy]
if ans == inf:
    ans = -1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
h,w,k = map(int, input().split())
sx,sy,gx,gy = map(lambda x: int(x)-1, input().split())
board = [list(map(lambda x: int(x=="."), input())) for i in range(h)]
inf = 1<<30
DP = [[inf for j in range(w)] for i in range(h)]
DP[sx][sy] = 0

from collections import deque
que = deque([(sx,sy)])

dxdy = [(0,1),(1,0),(0,-1),(-1,0)]
while que:
    x,y = que.popleft()
    for dx,dy in dxdy:
        nx,ny = x,y
        for i in range(k):
            nx += dx
            ny += dy
            if not(0<=nx<h and 0<=ny<w and board[nx][ny] and DP[nx][ny] >= DP[x][y]+1):
                break
            DP[nx][ny] = min(DP[nx][ny], DP[x][y]+1)
            que.append((nx,ny))
ans = DP[gx][gy]
if ans == inf:
    ans = -1
print(ans)


Fixed submission:
h,w,k = map(int, input().split())
sx,sy,gx,gy = map(lambda x: int(x)-1, input().split())
board = [list(map(lambda x: int(x=="."), input())) for i in range(h)]
inf = 1<<30
DP = [[inf for j in range(w)] for i in range(h)]
DP[sx][sy] = 0

from collections import deque
que = deque([(sx,sy)])

dxdy = [(0,1),(1,0),(0,-1),(-1,0)]
while que:
    x,y = que.popleft()
    for dx,dy in dxdy:
        nx,ny = x,y
        for i in range(k):
            nx += dx
            ny += dy
            if not(0<=nx<h and 0<=ny<w and board[nx][ny] and DP[nx][ny] >= DP[x][y]+1):
                break
            # コストが変わらない場合は無視か...
            if DP[nx][ny] == DP[x][y]+1:
                continue
            DP[nx][ny] = DP[x][y]+1
            que.append((nx,ny))
ans = DP[gx][gy]
if ans == inf:
    ans = -1
print(ans)

Buggy python submission (verdict: Runtime Error):
h,w,k = map(int, input().split())
sx,sy,gx,gy = map(lambda x: int(x)-1, input().split())
board = [list(map(lambda x: int(x=="."), input())) for i in range(h)]

DP = [[float("inf") for j in range(w)] for i in range(h)]
DP[sx][sy] = 0

from collections import deque
que = deque([(sx,sy)])

dxdy = [(0,1),(1,0),(0,-1),(-1,0)]
while que:
    x,y = que.popleft()
    for dx,dy in dxdy:
        nx,ny = x,y
        for i in range(1,k+1):
            nx += dx
            ny += dy
            if not(0<=nx<h and 0<=ny<w and board[nx][ny] and DP[nx][ny] > DP[x][y]+1):
                break
            DP[nx][ny] = DP[x][y]+1
            que.append((nx,ny))

ans = DP[gx][gy]
if ans == inf:
    ans = -1
print(ans)

Fixed submission:
h,w,k = map(int, input().split())
sx,sy,gx,gy = map(lambda x: int(x)-1, input().split())
board = [list(map(lambda x: int(x=="."), input())) for i in range(h)]
inf = 1<<30
DP = [[inf for j in range(w)] for i in range(h)]
DP[sx][sy] = 0

from collections import deque
que = deque([(sx,sy)])

dxdy = [(0,1),(1,0),(0,-1),(-1,0)]
while que:
    x,y = que.popleft()
    for dx,dy in dxdy:
        nx,ny = x,y
        for i in range(k):
            nx += dx
            ny += dy
            if not(0<=nx<h and 0<=ny<w and board[nx][ny] and DP[nx][ny] >= DP[x][y]+1):
                break
            # コストが変わらない場合は無視か...
            if DP[nx][ny] == DP[x][y]+1:
                continue
            DP[nx][ny] = DP[x][y]+1
            que.append((nx,ny))
ans = DP[gx][gy]
if ans == inf:
    ans = -1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

h,w,k = map(int, input().split())
x1,y1,x2,y2 = map(int, input().split())
x1,y1,x2,y2 = x1-1,y1-1, x2-1,y2-1
board = [list(map(lambda x: int(x=="."), input())) for i in range(h)]

def make_array(dims, fill):
    if len(dims) == 1:
        return [fill]*dims[0]
    nxt = dims[1:]
    ret = [make_array(nxt,fill) for i in range(dims[0])]
    return ret

inf = 1<<30

DP = make_array((h,w,4), inf)

from collections import deque
que = deque()
for i in range(4):
    que.append((x1,y1,i,0))

dxdy = [(-1,0),(1,0),(0,1),(0,-1)]
while que:
    x,y,prev,cost = que.popleft()
    if DP[x][y][prev] <= cost:
        continue
    DP[x][y][prev] = cost
    for i in range(4):
        if i == prev:
            continue
        nprev = i^1
        ddx, ddy = dxdy[i]
        for j in range(1,k+1):
            nx, ny = x+j*ddx, y+j*ddy
            if not (0<=nx<h and 0<=ny<w and board[nx][ny] and DP[nx][ny][nprev] > cost+1):
                break
            que.append((nx,ny,nprev,cost+1))

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


Fixed submission:
h,w,k = map(int, input().split())
sx,sy,gx,gy = map(lambda x: int(x)-1, input().split())
board = [list(map(lambda x: int(x=="."), input())) for i in range(h)]
inf = 1<<30
DP = [[inf for j in range(w)] for i in range(h)]
DP[sx][sy] = 0

from collections import deque
que = deque([(sx,sy)])

dxdy = [(0,1),(1,0),(0,-1),(-1,0)]
while que:
    x,y = que.popleft()
    for dx,dy in dxdy:
        nx,ny = x,y
        for i in range(k):
            nx += dx
            ny += dy
            if not(0<=nx<h and 0<=ny<w and board[nx][ny] and DP[nx][ny] >= DP[x][y]+1):
                break
            # コストが変わらない場合は無視か...
            if DP[nx][ny] == DP[x][y]+1:
                continue
            DP[nx][ny] = DP[x][y]+1
            que.append((nx,ny))
ans = DP[gx][gy]
if ans == inf:
    ans = -1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

h,w,k = map(int, input().split())
x1,y1,x2,y2 = map(int, input().split())
x1,y1,x2,y2 = x1-1,y1-1, x2-1,y2-1
board = [list(map(lambda x: int(x=="."), input())) for i in range(h)]

def make_array(dims, fill):
    if len(dims) == 1:
        return [fill]*dims[0]
    nxt = dims[1:]
    ret = [make_array(nxt,fill) for i in range(dims[0])]
    return ret

inf = float("inf")

DP = make_array((h,w,4), inf)

from collections import deque
que = deque()
for i in range(4):
    que.append((x1,y1,i,0))

dxdy = [(-1,0),(1,0),(0,1),(0,-1)]
while que:
    x,y,prev,cost = que.popleft()
    if DP[x][y][prev] <= cost:
        continue
    DP[x][y][prev] = cost
    for i in range(4):
        if i == prev:
            continue
        nprev = i^1
        ddx, ddy = dxdy[i]
        for j in range(1,k+1):
            nx, ny = x+j*ddx, y+j*ddy
            if not (0<=nx<h and 0<=ny<w and board[nx][ny] and DP[nx][ny][nprev] > cost+1):
                break
            que.append((nx,ny,nprev,cost+1))

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


Fixed submission:
h,w,k = map(int, input().split())
sx,sy,gx,gy = map(lambda x: int(x)-1, input().split())
board = [list(map(lambda x: int(x=="."), input())) for i in range(h)]
inf = 1<<30
DP = [[inf for j in range(w)] for i in range(h)]
DP[sx][sy] = 0

from collections import deque
que = deque([(sx,sy)])

dxdy = [(0,1),(1,0),(0,-1),(-1,0)]
while que:
    x,y = que.popleft()
    for dx,dy in dxdy:
        nx,ny = x,y
        for i in range(k):
            nx += dx
            ny += dy
            if not(0<=nx<h and 0<=ny<w and board[nx][ny] and DP[nx][ny] >= DP[x][y]+1):
                break
            # コストが変わらない場合は無視か...
            if DP[nx][ny] == DP[x][y]+1:
                continue
            DP[nx][ny] = DP[x][y]+1
            que.append((nx,ny))
ans = DP[gx][gy]
if ans == inf:
    ans = -1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

h,w,k = map(int, input().split())
x1,y1,x2,y2 = map(int, input().split())
x1,y1,x2,y2 = x1-1,y1-1, x2-1,y2-1
board = [list(map(lambda x: int(x=="."), input())) for i in range(h)]

def make_array(dims, fill):
    if len(dims) == 1:
        return [fill]*dims[0]
    nxt = dims[1:]
    ret = [make_array(nxt,fill) for i in range(dims[0])]
    return ret

inf = float("inf")

DP = make_array((h,w,4), inf)

from collections import deque
que = deque()
for i in range(4):
    que.append((x1,y1,i,0))

dxdy = [(-1,0),(1,0),(0,1),(0,-1)]
while que:
    x,y,prev,cost = que.popleft()
    if DP[x][y][prev] <= cost:
        continue
    DP[x][y][prev] = cost
    for i in range(4):
        if i == prev:
            continue
        nprev = i^1
        ddx, ddy = dxdy[i]
        for j in range(1,k+1):
            nx, ny = x+j*ddx, y+j*ddy
            if not (0<=nx<h and 0<=ny<w and board[nx][ny]):
                break
            if DP[nx][ny][nprev] > cost+1:
                que.append((nx,ny,nprev,cost+1))

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


Fixed submission:
h,w,k = map(int, input().split())
sx,sy,gx,gy = map(lambda x: int(x)-1, input().split())
board = [list(map(lambda x: int(x=="."), input())) for i in range(h)]
inf = 1<<30
DP = [[inf for j in range(w)] for i in range(h)]
DP[sx][sy] = 0

from collections import deque
que = deque([(sx,sy)])

dxdy = [(0,1),(1,0),(0,-1),(-1,0)]
while que:
    x,y = que.popleft()
    for dx,dy in dxdy:
        nx,ny = x,y
        for i in range(k):
            nx += dx
            ny += dy
            if not(0<=nx<h and 0<=ny<w and board[nx][ny] and DP[nx][ny] >= DP[x][y]+1):
                break
            # コストが変わらない場合は無視か...
            if DP[nx][ny] == DP[x][y]+1:
                continue
            DP[nx][ny] = DP[x][y]+1
            que.append((nx,ny))
ans = DP[gx][gy]
if ans == inf:
    ans = -1
print(ans)