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

Envío 871

Problema 0xe1 - Cuadrado mágico

  • Autor: d4vsanchez
  • Fecha: 2020-09-26 19:49:40 UTC (Hace alrededor de 4 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.024 s 3 KBi
#2
Correcto
0.022 s 3 KBi
#3
Correcto
0.023 s 3 KBi
#4
Correcto
0.031 s 7 KBi
#5
Correcto
0.023 s 3 KBi
#6
Correcto
0.023 s 3 KBi
#7
Correcto
0.024 s 3 KBi
#8
Correcto
0.024 s 3 KBi
#9
Incorrecto
0.024 s 3 KBi
#10
Incorrecto
0.023 s 3 KBi
#11
Correcto
0.026 s 7 KBi
#12
Correcto
0.024 s 3 KBi
#13
Correcto
0.022 s 3 KBi
#14
Correcto
0.026 s 3 KBi
#15
Correcto
0.028 s 3 KBi
#16
Correcto
0.028 s 3 KBi
#17
Correcto
0.027 s 3 KBi
#18
Correcto
0.026 s 3 KBi
#19
Correcto
0.028 s 3 KBi
#20
Correcto
0.028 s 3 KBi
Puntos totales: 90 / 100

Código

def get_main_diagonal_sum(matrix, n):
  return sum([matrix[i][i] for i in range(n)])

def get_secondary_diagonal_sum(matrix, n):
  return sum([matrix[i][n - i - 1] for i in range(n)])

def get_rows_sum(matrix, n):
  row_sum = [sum(row) for row in matrix]
  return sum(row_sum)

def get_columns_sum(matrix, n):
  col_sum = [sum(row) for row in zip(*matrix)]
  return sum(col_sum)

def is_magic_square(matrix):
  n = len(matrix)

  current_sum = get_main_diagonal_sum(matrix, n)
  current_sum = current_sum - get_secondary_diagonal_sum(matrix, n)

  if current_sum != 0:
    return False
  
  current_sum = get_rows_sum(matrix, n)
  current_sum = current_sum - get_columns_sum(matrix, n)

  return current_sum == 0

def main():
  n = int(input())
  matrix = []

  for cols in range(n):
    matrix.append([int(value) for value in input().split()])

  if is_magic_square(matrix):
    print("Yes")
  else:
    print("No")

if __name__ == "__main__":
  main()