Problem p03263 (python)

Fixed submission:
h, w = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(h)]
ls = [[bool(a % 2) for a in l] for l in ls]

res = []
started = False
steps = []
for i in range(h):
    odd = i % 2
    for j in range(w):
        if odd:
            j = w - j - 1
        if started:
            steps.append((i, j))
        if ls[i][j]:
            if not started:
                steps = [(i, j)]
                started = True
            else:
                for s, t in zip(steps, steps[1:]):
                    res.append((s[0], s[1], t[0], t[1]))
                started = False

print(len(res))
for row in res:
    row = map(lambda x: x + 1, row)
    print(*row)

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(h)]
ls = [[bool(a % 2) for a in l] for l in ls]

res = []
started = False
steps = []
for i in range(h):
    odd = i % 2
    for j in range(w):
        if odd:
            j = w - j - 1
        if started:
            steps.append((i, j))
        if ls[i][j]:
            if not started:
                steps = [(i, j)]
                started = True
            else:
                for s, t in zip(steps, steps[1:]):
                    res.append((*s, *t))
                started = False

print(len(res))
for row in res:
    row = map(lambda x: x + 1, row)
    print(*row)

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(h)]

INF = float('inf')
costs = [[0 for _ in range(w)] for _ in range(h)]


def dfs(i, j, paths, count):
    costs[i][j] = INF
    if ls[i][j] % 2 == 1 and count > 1:
        return paths
    if min([min(c) for c in costs]) == INF:
        return []
    if j > 0 and costs[i][j - 1] < INF:
        paths.append((i, j, i, j - 1))
        paths = dfs(i, j - 1, paths, count + 1)
    elif i < h - 1 and costs[i + 1][j] < INF:
        paths.append((i, j, i + 1, j))
        paths = dfs(i + 1, j, paths, count + 1)
    elif j < w - 1 and costs[i][j + 1] < INF:
        paths.append((i, j, i, j + 1))
        paths = dfs(i, j + 1, paths, count + 1)
    elif i > 0 and costs[i - 1][j] < INF:
        paths.append((i, j, i - 1, j))
        paths = dfs(i - 1, j, paths, count + 1)
    else:
        pi, pj, _, _ = paths.pop()
        paths = dfs(pi, pj, paths, count - 1)
    return paths


res = []
for i in range(h):
    for j in range(w):
        if costs[i][j] < INF and ls[i][j] % 2 == 1:
            paths = dfs(i, j, [], 1)
            if len(paths) > 0:
                res += paths
print(len(res))
for row in res:
    row = map(lambda x: x + 1, row)
    print(*row)

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(h)]

INF = float('inf')
costs = [[0 for _ in range(w)] for _ in range(h)]


def dfs(i, j, paths, count):
    costs[i][j] = INF
    if ls[i][j] % 2 == 1 and count > 1:
        return paths
    if min([min(c) for c in costs]) == INF:
        return []
    if j > 0 and costs[i][j - 1] < INF:
        paths.append((i, j, i, j - 1))
        paths = dfs(i, j - 1, paths, count + 1)
    elif i < h - 1 and costs[i + 1][j] < INF:
        paths.append((i, j, i + 1, j))
        paths = dfs(i + 1, j, paths, count + 1)
    elif j < w - 1 and costs[i][j + 1] < INF:
        paths.append((i, j, i, j + 1))
        paths = dfs(i, j + 1, paths, count + 1)
    elif i > 0 and costs[i - 1][j] < INF:
        paths.append((i, j, i - 1, j))
        paths = dfs(i - 1, j, paths, count + 1)
    else:
        pi, pj, _, _ = paths.pop()
        paths = dfs(pi, pj, paths, count - 1)
    return paths


res = []
for i in range(h):
    for j in range(w):
        if costs[i][j] < INF and ls[i][j] % 2 == 1:
            paths = dfs(i, j, [], 1)
            if len(paths) > 0:
                res += paths
print(len(res))
for row in res:
    row = map(lambda x: x + 1, row)
    print(*row)