Problem p03263 (python)

Fixed submission:
H, W = map(int, input().split(" "))
A=[]
for i in range(H):
    A.append ([int(i) for i in input().split()])

ret=[]
temp=0
N=0

for y in range(H):
    for x in range(W-1):
        if A[y][x] % 2 != 0:
            A[y][x] -= 1
            A[y][x+1] += 1
            ret.append(str(y+1) + " " + str(x+1) + " " + str(y+1) + " " + str(x+1+1))
            N+=1

for y in range(H-1):
    if A[y][W-1] % 2 != 0:
        A[y][W-1] -= 1
        A[y+1][W-1] += 1
        ret.append(str(y+1) + " " + str(W) + " " + str(y+1+1) + " " + str(W))
        N+=1
        
print (N)
for r in ret:
    print (r)

Buggy submission (verdict: Runtime Error):
import random
import copy
 
H, W = map(int, input().split(" "))
A=[]
for i in range(H):
    A.append ([int(i) for i in input().split()])
 
B=np.zeros((H,W), int)
C=np.zeros((H,W), int)
B = copy.deepcopy(A)
N=0
 
cnt=0
ret=[]
def move_area(arr, cnt_odd):
    global A
    global B
    global C
    global N
    # print ("cnt_odd = " + str(cnt_odd))
    random.shuffle(arr)
    # print (arr)
    C=copy.deepcopy(A)
 
    i=int(cnt_odd/2)
    while i>0:
        a=random.randrange(len(arr))
        b=random.randrange(len(arr))
        if a != b:
            if abs(arr[a][0]-arr[b][0]) + abs(arr[a][1]-arr[b][1]) == 1:
                if C[arr[a][0]][arr[a][1]] % 2 != 0:
                    if C[arr[b][0]][arr[b][1]] % 2 != 0:
                        i -= 1
 
                    C[arr[a][0]][arr[a][1]] -= 1
                    C[arr[b][0]][arr[b][1]] += 1
                    # print (str(arr[a][0]) + " " + str(arr[a][1]) + " " + str(arr[b][0]) + " " + str(arr[b][1]))
                    ret.append(str(arr[a][0]+1) + " " + str(arr[a][1]+1) + " " + str(arr[b][0]+1) + " " + str(arr[b][1]+1))
                    N += 1
 
#    random.shuffle(arr)
#    print ("Move")
#    print (arr)
#    print ("")
def conn(arr, cnt, cnt_odd, depth):
    global A
    global B
    flg=0
    for y,x in arr:
        for yd, xd in [[0,0],[-1,0],[1,0],[0,-1],[0,1]]:
            if y+yd >= 0 and y+yd < H and x+xd >= 0 and x+xd < W:
                if B[y+yd][x+xd] != 0:
                    if [y+yd,x+xd] not in arr:
                        arr.append([y+yd,x+xd])
                    # print (str(y+yd) + " " + str(x+xd))
                    flg = 1
                    cnt += 1
                    if B[y+yd][x+xd] % 2 != 0:
                        cnt_odd += 1
                    B[y+yd][x+xd] = 0
    
    if flg > 0:
        conn(arr, cnt, cnt_odd, depth + 1)
    
    if depth ==0:
        move_area(arr, cnt_odd)
    
    return cnt % 2
    
y = random.randrange(H) 
x = random.randrange(W) 
tmp = 0
for yy in range(H):
    for xx in range(W):
        if A[yy][xx] != 0:
            cnt_odd=0
            tmp += conn([[yy,xx]], cnt, 0, 0)
            # print ("tmp = " + str(tmp))
 
# print (tmp)
 
# print (A[1][2])
# print (B[1][1])
 
print (N)
for r in ret:
    print (r)

Buggy submission (verdict: Runtime Error):
import random
import copy

H, W = map(int, input().split(" "))
A=[]
for i in range(H):
    A.append ([int(i) for i in input().split()])

B=np.zeros((H,W), int)
C=np.zeros((H,W), int)
B = copy.deepcopy(A)
N=0

cnt=0
ret=[]
def move_area(arr, cnt_odd):
    global A
    global B
    global C
    global N
    # print ("cnt_odd = " + str(cnt_odd))
    random.shuffle(arr)
    # print (arr)
    C=copy.deepcopy(A)

    i=int(cnt_odd/2)
    while i>0:
        a=random.randrange(len(arr))
        b=random.randrange(len(arr))
        if a != b:
            if abs(arr[a][0]-arr[b][0]) + abs(arr[a][1]-arr[b][1]) == 1:
                if C[arr[a][0]][arr[a][1]] % 2 != 0:
                    if C[arr[b][0]][arr[b][1]] % 2 != 0:
                        i -= 1

                    C[arr[a][0]][arr[a][1]] -= 1
                    C[arr[b][0]][arr[b][1]] += 1
                    # print (str(arr[a][0]) + " " + str(arr[a][1]) + " " + str(arr[b][0]) + " " + str(arr[b][1]))
                    ret.append(str(arr[a][0]) + " " + str(arr[a][1]) + " " + str(arr[b][0]) + " " + str(arr[b][1]))
                    N += 1

#    random.shuffle(arr)
#    print ("Move")
#    print (arr)
#    print ("")
def conn(arr, cnt, cnt_odd, depth):
    global A
    global B
    flg=0
    for y,x in arr:
        for yd, xd in [[0,0],[-1,0],[1,0],[0,-1],[0,1]]:
            if y+yd >= 0 and y+yd < H and x+xd >= 0 and x+xd < W:
                if B[y+yd][x+xd] != 0:
                    if [y+yd,x+xd] not in arr:
                        arr.append([y+yd,x+xd])
                    # print (str(y+yd) + " " + str(x+xd))
                    flg = 1
                    cnt += 1
                    if B[y+yd][x+xd] % 2 != 0:
                        cnt_odd += 1
                    B[y+yd][x+xd] = 0
    
    if flg > 0:
        conn(arr, cnt, cnt_odd, depth + 1)
    
    if depth ==0:
        move_area(arr, cnt_odd)
    
    return cnt % 2
    
y = random.randrange(H) 
x = random.randrange(W) 
tmp = 0
for yy in range(H):
    for xx in range(W):
        if A[yy][xx] != 0:
            cnt_odd=0
            tmp += conn([[yy,xx]], cnt, 0, 0)
            # print ("tmp = " + str(tmp))

# print (tmp)

# print (A[1][2])
# print (B[1][1])

print (N)
for r in ret:
    print (r)

Buggy submission (verdict: Runtime Error):
import random
import copy

H, W = map(int, input().split(" "))
A=[]
for i in range(H):
    A.append ([int(i) for i in input().split()])

B=np.zeros((H,W), int)
C=np.zeros((H,W), int)
B = copy.deepcopy(A)

cnt=0
def move_area(arr, cnt_odd):
    global A
    global B
    global C
    # print ("cnt_odd = " + str(cnt_odd))
    random.shuffle(arr)
    # print (arr)
    C=copy.deepcopy(A)

    i=int(cnt_odd/2)
    while i>0:
        a=random.randrange(len(arr))
        b=random.randrange(len(arr))
        if a != b:
            if abs(arr[a][0]-arr[b][0]) + abs(arr[a][1]-arr[b][1]) == 1:
                if C[arr[a][0]][arr[a][1]] % 2 != 0:
                    if C[arr[b][0]][arr[b][1]] % 2 != 0:
                        i -= 1

                    C[arr[a][0]][arr[a][1]] -= 1
                    C[arr[b][0]][arr[b][1]] += 1
                    print (str(arr[a][0]) + " " + str(arr[a][1]) + " " + str(arr[b][0]) + " " + str(arr[b][1]))

#    random.shuffle(arr)
#    print ("Move")
#    print (arr)
#    print ("")
def conn(arr, cnt, cnt_odd, depth):
    global A
    global B
    flg=0
    for y,x in arr:
        for yd, xd in [[0,0],[-1,0],[1,0],[0,-1],[0,1]]:
            if y+yd >= 0 and y+yd < H and x+xd >= 0 and x+xd < W:
                if B[y+yd][x+xd] != 0:
                    if [y+yd,x+xd] not in arr:
                        arr.append([y+yd,x+xd])
                    # print (str(y+yd) + " " + str(x+xd))
                    flg = 1
                    cnt += 1
                    if B[y+yd][x+xd] % 2 != 0:
                        cnt_odd += 1
                    B[y+yd][x+xd] = 0
    
    if flg > 0:
        conn(arr, cnt, cnt_odd, depth + 1)
    
    if depth ==0:
        move_area(arr, cnt_odd)
    
    return cnt % 2
    
y = random.randrange(H) 
x = random.randrange(W) 
tmp = 0
for yy in range(H):
    for xx in range(W):
        if A[yy][xx] != 0:
            cnt_odd=0
            tmp += conn([[yy,xx]], cnt, 0, 0)