Problem p03263 (python)

Fixed submission:
#!/mnt/c/Users/moiki/bash/env/bin/python
H,W = map(int, input().split())
a = [ list(map(int, input().split())) for _ in range(H)]

diff = lambda a,b: abs(a[0]-b[0]) + abs(a[1]-b[1])

trainsition_list = []
for h in range(H):
    step = 1 if h%2==0 else -1
    for w in range(W)[::step]:
        trainsition_list.append( (h,w) )

ans_tuple = []
# ans_n = 0
n = 0
for pos,next_pos in zip(trainsition_list, trainsition_list[1:]):
    h,w = pos
    if a[h][w] % 2 == 1:
        nh,nw = next_pos
        a[nh][nw] += 1
        # print(h, w, nh, nw)
        ans_tuple.append( (h,w,nh,nw) )
        n += 1
print(n)
for atp in ans_tuple:
    print( *(map(lambda x: x+1, atp)))
    

# print(trainsition_list)
# exit(0)
        # if a[h][w] !=
        
    
    

# odd_pos = []
# for h in range(H):
#     for w in range(W):
#         if a[h][w] % 2 == 1:
#             odd_pos.append( (h+1,w+1) )
# 
# odd_pos = sorted(odd_pos, lambda x: x[0])
# odd_pos = sorted(odd_pos, lambda x: x[1])
# 
# N = 0
# for pair in zip( odd_pos[0::2], odd_pos[1::2]):
#     N += diff(pair[0], pair[1])
# 
# if N <= H*W:
#     print(N)
# else:
#     print(H*W)
# 
# cnt = 0
# for pair in zip( odd_pos[0::2], odd_pos[1::2]):
#     pos = pair[0]
#     target = pair[1]
#     while pos[0] != target[0] and cnt <= H*W:
#         cnt += 1
#         decrease = 1 if pos[0] > target[0] else -1
#         print(pos[0], pos[1], pos[0] - decrease, pos[1])
#         pos = (pos[0] - decrease, pos[1])
#     while pos[1] != target[1] and cnt <= H*W:
#         cnt += 1
#         decrease = 1 if pos[1] > target[1] else -1
#         print(pos[0], pos[1], pos[0], pos[1]-decrease)
#         pos = (pos[0] , pos[1] - decrease)
#

Buggy submission (verdict: Runtime Error):
#!/mnt/c/Users/moiki/bash/env/bin/python
H,W = map(int, input().split())
a = [ list(map(int, input().split())) for _ in range(H)]

diff = lambda a,b: abs(a[0]-b[0]) + abs(a[1]-b[1])

trainsition_list = []
for h in range(H):
    step = 1 if h%2==0 else -1
    for w in range(W)[::step]:
        trainsition_list.append( (h,w) )

ans_tuple = []
ans_n = 0
for pos,next_pos in zip(trainsition_list, trainsition_list[1:]):
    h,w = pos
    if a[h][w] % 2 == 1:
        nh,nw = next_pos
        a[nh][nw] += 1
        # print(h, w, nh, nw)
        ans_tuple.append( (h,w,nh,nw) )
        n += 1
print(n)
for atp in ans_tuple:
    print(atp)
    

# print(trainsition_list)
# exit(0)
        # if a[h][w] !=
        
    
    

# odd_pos = []
# for h in range(H):
#     for w in range(W):
#         if a[h][w] % 2 == 1:
#             odd_pos.append( (h+1,w+1) )
# 
# odd_pos = sorted(odd_pos, lambda x: x[0])
# odd_pos = sorted(odd_pos, lambda x: x[1])
# 
# N = 0
# for pair in zip( odd_pos[0::2], odd_pos[1::2]):
#     N += diff(pair[0], pair[1])
# 
# if N <= H*W:
#     print(N)
# else:
#     print(H*W)
# 
# cnt = 0
# for pair in zip( odd_pos[0::2], odd_pos[1::2]):
#     pos = pair[0]
#     target = pair[1]
#     while pos[0] != target[0] and cnt <= H*W:
#         cnt += 1
#         decrease = 1 if pos[0] > target[0] else -1
#         print(pos[0], pos[1], pos[0] - decrease, pos[1])
#         pos = (pos[0] - decrease, pos[1])
#     while pos[1] != target[1] and cnt <= H*W:
#         cnt += 1
#         decrease = 1 if pos[1] > target[1] else -1
#         print(pos[0], pos[1], pos[0], pos[1]-decrease)
#         pos = (pos[0] , pos[1] - decrease)
#