CCC 2019 S3 Help

Link: CCC '19 S3 - Arithmetic Square - DMOJ: Modern Online Judge
Submission: Login - DMOJ: Modern Online Judge

My code ACs for all the test cases except the last test case. I have tried to generate cases where I think is edge case and my code still works for all the test cases I generated. Does anyone know under which scenario my code fails?

Hi, so sorry for the inconvenience, here is my code:

def check(matrix):
    for m in matrix:
        if m[2] - m[1] != m[1] - m[0]:
            return False
    cols = list(zip(*matrix))
    cols = list(map(list, cols))
    for c in cols:
        if c[2] - c[1] != c[1] - c[0]:
            return False
    return True

def two_X_one_row(row):
    for i in range(3):
        if i == 0:
            if row[i] != 'X':
                a = row[i]
            else:
                empty = i
        elif i == 1:
            if row[i] != 'X':
                b = row[i]
            else:
                empty = i
        else:
            if row[i] != 'X':
                c = row[i]
            else:
                empty = i
    if empty == 0:
        a = b - (c - b)
    elif empty == 1:
        b = (a * c) // 2
    else:
        c = b + (b - c)
    return a, b, c

def print_AAABBBCC(a, b, c):
    print(a, a, a)
    print(b, b, b)
    print(c, c, c)

def L_condition(matrix):
    if 'X' not in matrix[0] and ((matrix[1][0] != 'X' and matrix[2][0] != 'X') or (matrix[1][2] != 'X' and matrix[2][2] != 'X')):
        return True
    if 'X' not in matrix[2] and ((matrix[1][0] != 'X' and matrix[2][0] != 'X') or (matrix[1][2] != 'X' and matrix[0][2] != 'X')):
        return True
    return False

def T(matrix):
    if 'X' not in matrix[0] or 'X' not in matrix[2]:
        matrix[1][0], matrix[1][2] = matrix[1][1], matrix[1][1]
    elif 'X' not in matrix[1]:
        matrix[0][1], matrix[2][1] = matrix[1][1], matrix[1][1]
    return fill(matrix)

def T_condition(matrix):
    c = list(zip(*matrix))
    if ('X' not in c[0] and 'X' not in matrix[1]) or ('X' not in c[2] and 'X' not in matrix[1]) or ('X' not in matrix[0] and 'X' not in c[1]) or ('X' not in matrix[2] and 'X' not in c[1]):
        return True
    return False

def L(matrix):
    if matrix[0][0] == 'X':
        if matrix[0][2] % 2 != matrix[2][1] % 2:
            matrix[0][1] = matrix[0][2] + 1
        else:
            matrix[0][1] = matrix[0][2]
    elif matrix[0][2] == 'X':
        if matrix[0][0] % 2 != matrix[2][1] % 2:
            matrix[0][1] = matrix[0][0] + 1
        else:
            matrix[0][1] = matrix[0][0]
    elif matrix[2][0] == 'X':
        if matrix[2][2] % 2 != matrix[0][1] % 2:
            matrix[2][1] = matrix[2][2] + 1
        else:
            matrix[2][1] = matrix[2][2]
    elif matrix[2][2] == 'X':
        if matrix[2][0] % 2 != matrix[0][1] % 2:
            matrix[2][1] = matrix[2][0] + 1
        else:
            matrix[2][1] = matrix[2][0]
    return fill(matrix)

def X(matrix):
    matrix[0][0], matrix[0][2] = matrix[0][1], matrix[0][1]
    return fill(matrix)

def x(matrix):
    matrix[0][0], matrix[0][2] = matrix[0][1], matrix[0][1]
    return matrix


def X_condition(matrix):
    if 'X' not in matrix[1] and matrix[0][1] != 'X' and matrix[2][1] != 'X':
        return True
    return False

doneL, doneT, doneX = False, False, False
def fill(matrix):
    global doneL, doneT, doneX
    while True:
        # print(matrix)
        Xs = matrix[0].count('X') + matrix[1].count('X') + matrix[2].count('X')
        if L_condition(matrix) and not doneL and Xs == 4:
            # print(1)
            doneL = True
            return L(matrix)
        if T_condition(matrix) and not doneT and Xs == 4:
            # print(2)
            doneT = True
            return T(matrix)
        if X_condition(matrix) and not doneX and Xs == 4:
            # print(3)
            doneX = True
            return X(matrix)
        flag = False
        for m in matrix:
            if 'X' in m: flag = True
        if not flag: break
        for m in matrix:
            if m.count('X') == 1:
                for i in range(3):
                    if m[i] == 'X':
                        if i == 0:
                            m[0] = m[1] - (m[2] - m[1])
                            break
                        elif i == 1:
                            m[1] = (m[0] + m[2]) // 2
                            break
                        else:
                            m[2] = m[1] + (m[1] - m[0])
                            break
        cols = list(zip(*matrix))
        cols = list(map(list, cols))
        for i in range(3):
            if cols[i].count('X') == 1:
                for j in range(3):
                    if cols[i][j] == 'X':
                        if j == 0:
                            matrix[j][i] = cols[i][1] - (cols[i][2]-cols[i][1])
                            break
                        elif j == 1:
                            matrix[j][i] = (cols[i][0]+cols[i][2])//2
                            break
                        elif j == 2:
                            matrix[j][i] = cols[i][1] + (cols[i][1]-cols[i][0])
                            break
    return matrix


r1, r2, r3 = input().split(), input().split(), input().split()
for i in range(3):
    if r1[i] != 'X':
        r1[i] = int(r1[i])
    if r2[i] != 'X':
        r2[i] = int(r2[i])
    if r3[i] != 'X':
        r3[i] = int(r3[i])

comb = r1 + r2 + r3
cnt = 0
for R in comb:
    if R != 'X': cnt += 1

if cnt == 0:
    print(0, 0, 0)
    print(0, 0, 0)
    print(0, 0, 0)

elif cnt == 1:
    for R in comb:
        if R != 'X': num = R
    print(num, num, num)
    print(num, num, num)
    print(num, num, num)

elif cnt == 2:
    if r1.count('X') == 1:
        a, b, c = two_X_one_row(r1)
        print(a, b, c)
        print(a*2, b*2, c*2)
        print(a*3, b*3, c*3)
    elif r2.count('X') == 1:
        a, b, c = two_X_one_row(r2)
        print(a, b, c)
        print(a * 2, b * 2, c * 2)
        print(a * 3, b * 3, c * 3)
    elif r3.count('X') == 1:
        a, b, c = two_X_one_row(r3)
        print(a, b, c)
        print(a * 2, b * 2, c * 2)
        print(a * 3, b * 3, c * 3)
    else:
        if r1.count('X') == 3:
            none = 1
        elif r2.count('X') == 3:
            none = 2
        elif r3.count('X') == 3:
            none = 3
        if none == 1:
            for A in r2:
                if A != 'X':
                    b = A
            for A in r3:
                if A != 'X':
                    c = A
            a = int(b) + (int(b) - int(c))
            print_AAABBBCC(a, b, c)
        elif none == 2:
            for A in r1:
                if A != 'X':
                    a = A
            for A in r3:
                if A != 'X':
                    c = A
            b = (c+a) // 2
            if check([[a, a, a], [b, b, b], [c, c, c]]):
                print_AAABBBCC(a, b, c)
            else:
                c1, c2, c3 = [r1[0], r2[0], r3[0]], [r1[1], r2[1], r3[1]], [r1[2], r2[2], r3[2]]
                matrix = [c1, c2, c3]
                for k in range(3):
                    if matrix[k].count('X') == 2:
                        for s in matrix[k]:
                            if s != 'X':
                                number = s
                                break
                        matrix[k] = [s, s, s]
                a = fill(matrix)
                ans = list(zip(*a))
                ans = list(map(list, ans))
                for o in ans:
                    print(' '.join(list(map(str, o))))


        else:
            for A in r1:
                if A != 'X':
                    a = A
            for A in r2:
                if A != 'X':
                    b = A
            c = int(b) + (int(b) - int(a))
            print_AAABBBCC(a, b, c)

if cnt == 3:
    c1, c2, c3 = [r1[0], r2[0], r3[0]], [r1[1], r2[1], r3[1]], [r1[2], r2[2], r3[2]]
    if r1.count('X') == 2 and r2.count('X') == 2 and r3.count('X') == 2 and c1.count('X') == 2 and c2.count('X') == 2 and c3.count('X') == 2:
        matrix = [r1, r2, r3]
        for M in matrix[1]:
            if M != 'X':
                number = M
                break
        matrix[1] = [number, number, number]
        a = fill(matrix)
        for A in a:
            print(' '.join(list(map(str, A))))
    elif r1.count('X') == 0:
        for _ in range(3): print(' '.join(list(map(str, r1))))
    elif r2.count('X') == 0:
        for _ in range(3): print(' '.join(list(map(str, r2))))
    elif r3.count('X') == 0:
        for _ in range(3): print(' '.join(list(map(str, r3))))
    elif c1.count('X') == 0:
        for _ in range(3): print(' '.join(list(map(str, c1))))
    elif c2.count('X') == 0:
        for _ in range(3): print(' '.join(list(map(str, c2))))
    elif c3.count('X') == 0:
        for _ in range(3): print(' '.join(list(map(str, c3))))
    else:
        a = fill([r1, r2, r3])
        for A in a:
            print(' '.join(list(map(str, A))))

if cnt >= 4:
    a = fill([r1, r2, r3])
    for A in a:
        print(' '.join(list(map(str, A))))