Problem p02839 (python)

Fixed submission:
import numpy as np

H, W = list(map(int, input().split()))

A = []
B = []

max_bias = 80
bias = max_bias * (H + W - 1) + 1

for i in range(H):
    A.append(list(map(int, input().split())))

for i in range(H):
    B.append(list(map(int, input().split())))

A = np.array(A, dtype=np.int)
B = np.array(B, dtype=np.int)
D = np.abs(A - B)

dp = np.zeros([H, W, bias * 2 + 1], dtype=bool)
dp[0][0][bias - D[0][0]] = True
dp[0][0][bias + D[0][0]] = True

for i in range(H):
    for j in range(W):
        diff = D[i][j]
        
        if i > 0:
            dp[i, j, diff:] |= dp[i - 1, j, :bias * 2 + 1 - diff]
            dp[i, j, : bias * 2 + 1 - diff] |= dp[i - 1, j, diff:]

        if j > 0:
            dp[i, j, diff:] |= dp[i, j - 1, :bias * 2 + 1 - diff]
            dp[i, j, : bias * 2 + 1 - diff] |= dp[i, j - 1, diff:]

idx = dp[H - 1][W - 1][:].nonzero()[0]
print(np.abs(idx - bias).min())

Buggy submission (verdict: Runtime Error):
import numpy as np

H, W = list(map(int, input().split()))

A = []
B = []

for i in range(H):
    A.append(list(map(int, input().split())))

for i in range(H):
    B.append(list(map(int, input().split())))

A = np.array(A, dtype=np.int)
B = np.array(B, dtype=np.int)

dp = np.zeros([H, W, 80**2+1], dtype=bool)
dp[0][0][abs(A[0][0] - B[0][0])] = True

for i in range(H):
    for j in range(W):
        diff = A[i][j] - B[i][j]
        if i > 0:
            idx = np.where(dp[i - 1][j][:])[0]
            dp[i][j][np.clip(np.abs(idx - diff), 0, 80**2)] = True
            dp[i][j][np.clip(np.abs(idx + diff), 0, 80**2)] = True

        if j > 0:
            idx = np.where(dp[i][j - 1][:])[0]
            dp[i][j][np.clip(np.abs(idx - diff), 0, 80**2)] = True
            dp[i][j][np.clip(np.abs(idx + diff), 0, 80**2)] = True

print(dp[H - 1][W - 1][:].nonzero()[0].min())

Buggy submission (verdict: Runtime Error):
import numpy as np

H, W = list(map(int, input().split()))

A = []
B = []

for i in range(H):
    A.append(list(map(int, input().split())))

for i in range(H):
    B.append(list(map(int, input().split())))

A = np.array(A, dtype=np.uint8)
B = np.array(B, dtype=np.uint8)

dp = np.zeros([H, W, 80**2+1], dtype=bool)
dp[0][0][abs(A[0][0] - B[0][0])] = True

for i in range(H):
    for j in range(W):
        diff = A[i][j] - B[i][j]
        if i > 0:
            idx = np.where(dp[i - 1][j][:])[0]
            dp[i][j][np.clip(np.abs(idx - diff), 0, 80**2)] = True
            dp[i][j][np.clip(np.abs(idx + diff), 0, 80**2)] = True

        if j > 0:
            idx = np.where(dp[i][j - 1][:])[0]
            dp[i][j][np.clip(np.abs(idx - diff), 0, 80**2)] = True
            dp[i][j][np.clip(np.abs(idx + diff), 0, 80**2)] = True

print(dp[H - 1][W - 1][:].nonzero()[0].min())

Buggy submission (verdict: Runtime Error):
import numpy as np

H, W = list(map(int, input().split()))

A = []
B = []

for i in range(H):
    A.append(list(map(int, input().split())))

for i in range(H):
    B.append(list(map(int, input().split())))

dp = False * np.ones([H, W, 80**3], dtype=bool)
dp[0][0][abs(A[0][0] - B[0][0])] = True

for i in range(H):
    for j in range(W):
        diff = A[i][j] - B[i][j]
        if i > 0:
            idx = np.where(dp[i - 1][j][:])[0]
            dp[i][j][np.abs(idx - diff)] = True
            dp[i][j][np.abs(idx + diff)] = True

        if j > 0:
            idx = np.where(dp[i][j - 1][:])[0]
            dp[i][j][np.abs(idx - diff)] = True
            dp[i][j][np.abs(idx + diff)] = True

print(dp[H - 1][W - 1][:].nonzero()[0].min())

Buggy submission (verdict: Runtime Error):
import numpy as np

H, W = list(map(int, input().split()))

A = []
B = []

for i in range(H):
    A.append(list(map(int, input().split())))

for i in range(H):
    B.append(list(map(int, input().split())))

dp = False * np.ones([H, W, 80**3], dtype=bool)
dp[0][0][abs(A[0][0] - B[0][0])] = True

for i in range(H):
    for j in range(W):
        diff = A[i][j] - B[i][j]
        if i > 0:
            idx = np.where(dp[i - 1][j][:])[0]
            idx1 = np.clip(np.abs(idx - diff), 0, 80**3-1)
            idx2 = np.clip(np.abs(idx + diff), 0, 80**3-1)
            dp[i][j][idx1] = True
            dp[i][j][idx2] = True

        if j > 0:
            idx = np.where(dp[i][j - 1][:])[0]
            idx1 = np.clip(np.abs(idx - diff), 0, 80**3-1)
            idx2 = np.clip(np.abs(idx + diff), 0, 80**3-1)
            dp[i][j][idx1] = True
            dp[i][j][idx2] = True

print(dp[H - 1][W - 1][:].nonzero()[0].min())