Problem p02644 (python)

Fixed submission:
from collections import deque
import sys

sys.setrecursionlimit(500005)
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

def rights(bd):
    n = len(bd)
    m = len(bd[0])
    rs = [[0] * m for _ in range(n)]
    for i in range(n):
        for j in range(m-1, -1, -1):
            if bd[i][j] == "@":
                rs[i][j] = 0
            elif j == m-1:
                rs[i][j] = 1
            else:
                rs[i][j] = rs[i][j+1] + 1
    return rs

def rotc(bd):
    n = len(bd)
    m = len(bd[0])
    rs = [[0] * n for _ in range(m)]
    for i in range(n):
        for j in range(m):
            rs[j][n-1-i] = bd[i][j]
    return rs


h, w, k = na()
x1, y1, x2, y2 = na()
x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1

bd = [ns() for _ in range(h)]

right = rights(bd)
bd = rotc(bd)
up = rotc(rotc(rotc(rights(bd))))
bd = rotc(bd)
left = rotc(rotc(rights(bd)))
bd = rotc(bd)
down = rotc(rights(bd))
bd = rotc(bd)


q = deque()
rh = 1
while rh < w:
    rh *= 2
ch = 1
while ch < h:
    ch *= 2
ds = [9999999999] * (h*w+h*2*rh+w*2*ch)
q.append(x1*w+y1)
ds[x1*w+y1] = 0

# dr = (1, 0, -1, 0)
# dc = (0, 1, 0, -1)

# h*w
# h*2*rh
# w*2*ch
o0 = h*w
o1 = h*w + h*2*rh
while len(q) > 0:
    cur = q.popleft()
    curd = ds[cur]
    if cur < o0:
        r = cur // w
        c = cur % w

        rr = min(c + right[r][c] - 1, c + k) + 1
        ll = max(c - left[r][c] + 1, c - k)
        while ll != 0:
            f = ll & -ll
            if ll + f > rr:
                break
            tar = o0 + r*2*rh + (rh+ll)//f
            if ds[tar] > ds[cur] + 1:
                ds[tar] = ds[cur] + 1
                q.append(tar)
            ll += f
        while ll < rr:
            f = rr & -rr
            tar = o0 + r*2*rh + (rh+rr)//f-1
            if ds[tar] > ds[cur] + 1:
                ds[tar] = ds[cur] + 1
                q.append(tar)
            rr -= f

        rr = min(r + down[r][c] - 1, r + k) + 1
        ll = max(r - up[r][c] + 1, r - k)
        while ll != 0:
            f = ll & -ll
            if ll + f > rr:
                break
            tar = o1 + c * 2*ch + (ch + ll) // f
            if ds[tar] > ds[cur] + 1:
                ds[tar] = ds[cur] + 1
                q.append(tar)
            ll += f
        while ll < rr:
            f = rr & -rr
            tar = o1 + c * 2*ch + (ch + rr) // f - 1
            if ds[tar] > ds[cur] + 1:
                ds[tar] = ds[cur] + 1
                q.append(tar)
            rr -= f
    elif cur < o1:
        cur -= o0
        r = cur // (2*rh)
        v = cur % (2*rh)
        if v >= rh:
            tar = r*w + v-rh
            if ds[tar] > curd and bd[r][v-rh] == ".":
                ds[tar] = curd
                q.appendleft(tar)
        else:
            for j in range(2*v, 2*v+2):
                tar = o0 + r*2*rh + j
                if ds[tar] > curd:
                    ds[tar] = curd
                    q.appendleft(tar)
    else:
        cur -= o1
        c = cur // (2 * ch)
        v = cur % (2 * ch)
        if v >= ch:
            tar = (v-ch) * w + c
            if ds[tar] > curd and bd[v-ch][c] == ".":
                ds[tar] = curd
                q.appendleft(tar)
        else:
            for j in range(2 * v, 2 * v + 2):
                tar = o1 + c * 2 * ch + j
                if ds[tar] > curd:
                    ds[tar] = curd
                    q.appendleft(tar)

if ds[x2*w+y2] > 9999999:
    print(-1)
else:
    print(ds[x2*w+y2])

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(500005)
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

h, w, K = na()
x1, y1, x2, y2 = na()
x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1

bd = [ns() for _ in range(h)]

q = deque()
ds = [9999999999999999] * (h*w)

q.append((x1, y1))
ds[x1*w+y1] = 0
dr = (1, 0, -1, 0)
dc = (0, 1, 0, -1)
while len(q) > 0:
    r, c = q.popleft()
    for i in range(4):
        for j in range(1, K+1):
            nr, nc = r + dr[i] * j, c + dc[i] * j
            if 0 <= nr < h and 0 <= nc < w and bd[nr][nc] != "@":
                nd = ds[r*w+c] + 1
                if nd < ds[nr*w+nc]:
                    ds[nr*w+nc] = nd
                    q.append((nr, nc))
                elif nd <= ds[nr*w+nc]:
                    continue
                else:
                    break
            else:
                break
ans = ds[x2*w+y2]
if ans < 999999999999:
    print(ans)
else:
    print(-1)