Problem p03263 (python)

Fixed submission:
from collections import deque
H,W = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(H)]
B = [[0 if A[i][j]%2==0 else 1 for j in range(W)] for i in range(H)]
C = [deque([]) for _ in range(H)]
for i in range(H):
    for j in range(W):
        if B[i][j]==1:
            C[i].append(j)
cnt = 0
for i in range(H):
    cnt += sum(B[i])
if cnt%2==1:
    for i in range(H-1,-1,-1):
        if len(C[i])>0:
            C[i].pop()
            break
D = []
flag = 0
i = 0
while i<H:
    while len(C[i])>=2:
        if flag==0:
            j1 = C[i].popleft()
            i1 = i
            j2 = C[i].popleft()
            i = i
            for j in range(j1,j2):
                D.append((i,j,i,j+1))
        else:
            j1 = C[i].pop()
            i1 = i
            j2 = C[i].pop()
            i2 = i
            for j in range(j1,j2,-1):
                D.append((i,j,i,j-1))
    if len(C[i])==0:
        i += 1
    else:
        j = i+1
        while j<H:
            if len(C[j])>0:
                break
        j1 = C[i].pop()
        i1 = i
        if flag==0:
            j2 = C[j].pop()
            i2 = j
            for l in range(j1,W-1):
                D.append((i1,l,i1,l+1))
            for k in range(i1,i2):
                D.append((k,W-1,k+1,W-1))
            for l in range(W-1,j2,-1):
                D.append((i2,l,i2,l-1))
        else:
            j2 = C[j].popleft()
            i2 = j
            for l in range(j1,0,-1):
                D.append((i1,l,i1,l-1))
            for k in range(i1,i2):
                D.append((k,0,k+1,0))
            for l in range(j2):
                D.append((i2,l,i2,l+1))
        flag = 1-flag
        i = j
print(len(D))
for d in D:
    print(d[0]+1,d[1]+1,d[2]+1,d[3]+1)

Buggy submission (verdict: Runtime Error):
H,W = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(H)]
for i in range(H):
    for j in range(W):
        A[i][j] = A[i][j]%2
flag=0
for i in range(H):
    if i%2==0:
        for j in range(W):
            if A[i][j]==1:
                cur = (i,j)
                flag=1
                break
        if flag==1:break
    else:
        for j in range(W-1,-1,-1):
            if A[i][j]==1:
                cur = (i,j)
                flag = 1
                break
        if flag==1:break
B = []
if flag==0:
    N=0
else:
    i,j = cur
    ind = 1
    while i<H:
        if i%2==0:
            while True:
                if j==W-1:
                    if ind==1:
                        B.append([(i,j),(i+1,j)])
                    i += 1
                    if A[i][j]==1:
                        ind = 1-ind
                    break
                else:
                    if ind==1:
                        B.append([(i,j),(i,j+1)])
                    j += 1
                    if A[i][j]==1:
                        ind = 1-ind
        else:
            while j>=0:
                if j==0:
                    if ind==1:
                        B.append([(i,j),(i+1,j)])
                    i += 1
                    break
                else:
                    if ind==1:
                        B.append([(i,j),(i,j-1)])
                    j -= 1
                    if A[i][j]==0:continue
                    else:
                        ind = 1-ind
    N = len(B)
print(N)
for i in range(N):
    (i1,j1),(i2,j2)=B[i]
    print(i1+1,j1+1,i2+1,j2+1)

Buggy submission (verdict: Runtime Error):
def manh(x,y):
    i,j = x[0],x[1]
    k,m = y[0],y[1]
    return abs(i-k)+abs(m-j)
H,W = map(int,input().split())
G = {i:[] for i in range(1,H+1)}
for i in range(1,H+1):
    row = list(map(int,input().split()))
    row.insert(0,0)
    for j in range(1,W+1):
        row[j] = row[j]%2
        if row[j]==1:
            G[i].append(j)
B = []
C = []
for i in range(1,H+1):
    for j in range(1,len(G[i]),2):
        B.append(((i,G[i][j-1]),(i,G[i][j])))
    if len(G)%2==1:
        C.append((i,G[i][-1]))
D = []
for i in range(1,len(C),2):
    x = C[i-1]
    y = C[i]
    D.append((x,y))
N = 0
for b in B:
    x,y = b[0],b[1]
    N += manh(x,y)
for d in D:
    x,y = d[0],d[1]
    N += manh(x,y)
print(N)
for b in B:
    x,y = b[0],b[1]
    i,j = x[0],x[1]
    k,m = y[0],y[1]
    for t in range(j,m):
        print(i,t,i,t+1)
for d in D:
    x,y = d[0],d[1]
    i,j = x[0],x[1]
    k,m = y[0],y[1]
    for t in range(i,k):
        print(t,j,t+1,j)
    if j>=m:
        for t in range(j,m,-1):
            print(k,t,k,t-1)
    else:
        for t in range(j,m):
            print(k,t,k,t+1)