Problem p03263 (python)

Fixed submission:
H,W = map(int,input().split())
A = [[0 for _ in range(W+1)]]
sumA = 0
for _ in range(H):
  row = [0] + list(map(int,input().split())) 
  A.append(row)
  sumA = sum(row)



c_num = 0
pros = []
for j in range(1,H+1):
  for i in range(1,W):
    if A[j][i]%2==1:
      pros.append([j,i, j,i+1])
      c_num += 1
      A[j][i] -= 1
      A[j][i+1] += 1
 
for j in range(H,1,-1):
  if A[j][W]%2==1:
    pros.append([j,W,j-1,W])
    c_num += 1
    A[j][W] -= 1
    A[j-1][W] += 1

print(c_num)
[print(*pro) for pro in pros]

Buggy submission (verdict: Runtime Error):
def move_one_coine(target_i, target_j):
    while grids[target_j][target_i] < 2:
        for j in range(H):
            for i in range(W):
                if grids[j][i] >= 3:
                    grids[j][i] -= 1
                    grids[target_j][target_i] += 1
                    append_steps_to_answer(target_j, target_i, j, i)
        if j == H - 1 and i == W - 1:
            return


def append_steps_to_answer(j1, i1, j2, i2):
    prv_i = i1
    for i in range(i1 + 1, i2 + 1):
        ans.append([j1, prv_i, j2, i])
        prv_i = i

    prv_j = j1
    for j in range(j1 + 1, j2 + 1):
        ans.append([prv_j, i1, j2, i2])
        prv_j = j


H, W = map(int, input().split())
grids = [list(map(int, input().split())) for _ in range(H)]
ans = []
for j in range(H):
    for i in range(W):
        if grids[j][i] in [0, 1]:
            move_one_coine(i, j)

girds_to_move = None
for j in range(H):
    for i in range(W):
        if grids[j][i] % 2 == 1:
            if girds_to_move is None:
                girds_to_move = [j, i]
            else:
                grids[girds_to_move[0]][girds_to_move[1]] += 1
                grids[j][i] -= 1
                append_steps_to_answer(*girds_to_move, j, i)


print(len(ans))
[print(*map(lambda x: x + 1, process)) for process in ans]

Buggy submission (verdict: Runtime Error):
from itertools import permutations
import copy

def dis(po1,po2):
  return(abs(po1[0]-po2[0])+abs(po1[1]-po2[1]))

def print_process(min_prm):
  for i in range(0,len(min_prm)//2+1,2):
    while prm[i][0] != prm[i+1][0]:
      prv_prm = copy.deepcopy(prm[i])
      if prm[i][0] < prm[i+1][0]: prm[i][0] += 1
      else: prm[i][0] -= 1
      print(prv_prm[1], prv_prm[0], prm[1], prm[0])
   
    while prm[i][1] != prm[i+1][1]:
      prv_prm = copy.deepcopy(prm[i])
      if prm[i][1] < prm[i+1][1]: prm[i][1] += 1
      else: prm[i][1] -= 1
      print(prv_prm[1], prv_prm[0], prm[1], prm[0])

H,W = map(int,input().split())
odds = []
for j in range(H):
  for i, a in enumerate(map(int,input().split())):
    if a%2 == 1:
      odds.append([i+1,j+1])

min_prm = None
min_dis = float('inf')
for prm in permutations(odds):
  tmp_dis = 0
  for i in range(0,len(prm)//2+1,2):
    tmp_dis += dis(prm[i],prm[i+1])
  
  if min_dis > tmp_dis:
    min_prm = prm
    min_dis = tmp_dis
print(min_dis)
print_process(min_prm)

Buggy submission (verdict: Runtime Error):
from itertools import permutations
import copy

def dis(po1,po2):
  return(abs(po1[0]-po2[0])+abs(po1[1]-po2[1]))

def print_process(min_prm):
  for i in range(0,len(min_prm)//2+1,2):
    while prm[i][0] != prm[i+1][0]:
      prv_prm = copy.deepcopy(prm[i])
      if prm[i][0] < prm[i+1][0]: prm[i][0] += 1
      else: prm[i][0] -= 1
      print(*prv_prm + prm[i])
   
    while prm[i][1] != prm[i+1][1]:
      prv_prm = copy.deepcopy(prm[i])
      if prm[i][1] < prm[i+1][1]: prm[i][1] += 1
      else: prm[i][1] -= 1
      print(*prv_prm + prm[i])

H,W = map(int,input().split())
odds = []
for j in range(H):
  for i, a in enumerate(map(int,input().split())):
    if a%2 == 1:
      odds.append([i+1,j+1])

min_prm = None
min_dis = float('inf')
for prm in permutations(odds):
  tmp_dis = 0
  for i in range(0,len(prm)//2+1,2):
    tmp_dis += dis(prm[i],prm[i+1])
  
  if min_dis > tmp_dis:
    min_prm = prm
    min_dis = tmp_dis
print(min_dis)
print_process(min_prm)