█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 4823

Problema 0xe1 - Cuadrado mágico

  • Autor: Dlopez94
  • Fecha: 2021-08-25 01:44:13 UTC (Hace más de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.02 s 3 KBi
#2
Correcto
0.021 s 3 KBi
#3
Correcto
0.023 s 3 KBi
#4
Correcto
0.021 s 3 KBi
#5
Correcto
0.02 s 3 KBi
#6
Correcto
0.022 s 3 KBi
#7
Correcto
0.021 s 3 KBi
#8
Correcto
0.021 s 3 KBi
#9
Correcto
0.038 s 3 KBi
#10
Correcto
0.023 s 3 KBi
#11
Correcto
0.024 s 3 KBi
#12
Correcto
0.023 s 3 KBi
#13
Correcto
0.025 s 3 KBi
#14
Correcto
0.03 s 3 KBi
#15
Correcto
0.028 s 3 KBi
#16
Correcto
0.035 s 3 KBi
#17
Correcto
0.03 s 3 KBi
#18
Correcto
0.034 s 3 KBi
#19
Correcto
0.042 s 3 KBi
#20
Correcto
0.027 s 3 KBi
Puntos totales: 100 / 100

Código

N = 0
a = []
matrix = []
sum_total = 0
magic_sum = 0
answer = ''

N = int(input())

for index in range(N):
    a = []
    for num in input().split(' '):
        x = int(num)
        sum_total +=x
        a.append(x)
    matrix.append(a)

magic_sum =sum_total/N

def rows_check(matrix, magic_sum, answer, N):
    for row in range(N):
        if sum(matrix[row]) != magic_sum:
            answer = 'No'
            break
        else:
            answer = 'Yes'
    return(answer)


def column_check(matrix, magic_sum, answer, N):
    for column in range(N):
        if sum([rows[column] for rows in matrix]) != magic_sum:
            answer = 'No'
            break
        else:
            answer = 'Yes'
    return(answer)


def top_diagonal_check(matrix, magic_sum, answer, N):
    top_diagonal = 0
    for row_index in range(N):
        for column_index in range(N):
            if row_index == column_index:
                top_diagonal += matrix[row_index][column_index]
            else:
                continue

    if top_diagonal != magic_sum:
        answer = 'No'
    else:
        answer = 'Yes'

    return(answer)

def lower_diagonal_check(matrix, magic_sum, answer, N):
    lower_diagonal = 0
    diagonal_index = N-1
    for row_index in range(N):
        for column_index in range(N):
            if column_index == diagonal_index:
                lower_diagonal += matrix[row_index][column_index]
                diagonal_index += -1
            else:
                continue

    if lower_diagonal != magic_sum:
        answer = 'No'
    else:
        answer = 'Yes'

    return(answer)

answer = rows_check(matrix, magic_sum, answer, N)

if answer == 'Yes':
    answer = column_check(matrix, magic_sum, answer, N)

if answer == 'Yes':
    answer = top_diagonal_check(matrix, magic_sum, answer, N)

if answer == 'Yes':
    answer = lower_diagonal_check(matrix, magic_sum, answer, N)


print(answer)