Problem p03426 (python)

Fixed submission:
import numpy as np

H, W, D = [int(x) for x in input().strip().split(' ')]
stage = {}
for i in range(H):
    nums = [int(x) for x in input().strip().split(' ')]
    for j in range(W):
        stage[nums[j]] = [i + 1, j + 1]
Q = int(input())
tests = []
for _ in range(Q):
    tests.append([int(x) for x in input().strip().split(' ')])

scores = np.zeros([H * W + 1]).astype(np.int)
for i in range(H * W, 0, -1):
    if i + D > H * W:
        scores[i] = 0
    else:
        x, y = stage[i]
        ox, oy = stage[i + D]
        scores[i] = scores[i + D] + abs(x - ox) + abs(y - oy)

for i in range(Q):
    L, R = tests[i]
    print(scores[L] - scores[R])

Buggy submission (verdict: Runtime Error):

H, W, D = [int(x) for x in input().strip().split(' ')]
stage = {}
for i in range(H):
    nums = [int(x) for x in input().strip().split(' ')]
    for j in range(W):
        stage[nums[j]] = [i + 1, j + 1]
Q = int(input())
tests = []
for _ in range(Q):
    tests.append([int(x) for x in input().strip().split(' ')])

scores = []
done = {}
for i in range(Q):
    L, R = tests[i]
    if (L, R) in done:
        scores.append(done[(L, R)])
    elif L == R:
        scores.append(0)
    else:
        x, y = stage[L]
        score = 0
        nowL = L.copy()
        while nowL < R:
            if (nowL, R) in done:
                score += done[(nowL, R)]
                break
            new_x, new_y = stage[nowL + D]
            score += abs(new_x - x) + abs(new_y - y)
            x = new_x
            y = new_y
            nowL += D
        scores.append(score)
        done[(L, R)] = score

for score in scores:
    print(score)

Buggy submission (verdict: Runtime Error):

H, W, D = [int(x) for x in input().strip().split(' ')]
stage = {}
for i in range(H):
    nums = [int(x) for x in input().strip().split(' ')]
    for j in range(W):
        stage[nums[j]] = [i + 1, j + 1]
Q = int(input())
tests = []
for _ in range(Q):
    tests.append([int(x) for x in input().strip().split(' ')])

scores = []
done = {}
for i in range(Q):
    L, R = tests[i]
    if (L, R) in done:
        scores.append(done[(L, R)])
    elif L == R:
        scores.append(0)
    else:
        x, y = stage[L]
        score = 0
        while L < R:
            if (L, R) in done:
                score += done[(L, R)]
                break
            new_x, new_y = stage[L + D]
            del stage[L + D]
            score += abs(new_x - x) + abs(new_y - y)
            x = new_x
            y = new_y
            L += D
        scores.append(score)
        done[(L, R)] = score

for score in scores:
    print(score)

Buggy submission (verdict: Runtime Error):

H, W, D = [int(x) for x in input().strip().split(' ')]
stage = {}
for i in range(H):
    nums = [int(x) for x in input().strip().split(' ')]
    for j in range(W):
        stage[nums[j]] = [i + 1, j + 1]
Q = int(input())
tests = []
for _ in range(Q):
    tests.append([int(x) for x in input().strip().split(' ')])

scores = []
done = {}
for i in range(Q):
    L, R = tests[i]
    if (L, R) in done:
        scores.append(done[[L, R]])
    elif L == R:
        scores.append(0)
    else:
        x, y = stage[L]
        score = 0
        while L < R:
            if (L, R) in done:
                score += done[(L, R)]
                break
            new_x, new_y = stage[L + D]
            del stage[L + D]
            score += abs(new_x - x) + abs(new_y - y)
            x = new_x
            y = new_y
            L += D
        scores.append(score)
        done[(L, R)] = score

for score in scores:
    print(score)

Buggy submission (verdict: Runtime Error):

H, W, D = [int(x) for x in input().strip().split(' ')]
stage = {}
for i in range(H):
    nums = [int(x) for x in input().strip().split(' ')]
    for j in range(W):
        stage[nums[j]] = [i + 1, j + 1]
Q = int(input())
tests = []
for _ in range(Q):
    tests.append([int(x) for x in input().strip().split(' ')])

scores = []
done = {}
for i in range(Q):
    L, R = tests[i]
    if (L, R) in done:
        scores.append(done[[L, R]])
    elif L == R:
        scores.append(0)
    else:
        x, y = stage[L]
        score = 0
        while L < R:
            new_x, new_y = stage[L + D]
            score += abs(new_x - x) + abs(new_y - y)
            x = new_x
            y = new_y
            L += D
        scores.append(score)
        done[(L, R)] = score

for score in scores:
    print(score)