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

Envío 5093

Problema 0xe1 - Cuadrado mágico

  • Autor: jarangolp
  • Fecha: 2021-10-14 00:02:04 UTC (Hace alrededor de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.003 s 8 KBi
#2
Correcto
0.004 s 1 KBi
#3
Correcto
0.004 s 0 KBi
#4
Correcto
0.004 s 0 KBi
#5
Correcto
0.004 s 0 KBi
#6
Correcto
0.004 s 0 KBi
#7
Correcto
0.004 s 0 KBi
#8
Correcto
0.005 s 0 KBi
#9
Correcto
0.006 s 0 KBi
#10
Correcto
0.004 s 0 KBi
#11
Correcto
0.004 s 0 KBi
#12
Correcto
0.004 s 2 KBi
#13
Correcto
0.004 s 27 KBi
#14
Correcto
0.004 s 0 KBi
#15
Correcto
0.006 s 0 KBi
#16
Correcto
0.009 s 0 KBi
#17
Correcto
0.006 s 0 KBi
#18
Correcto
0.007 s 0 KBi
#19
Correcto
0.009 s 0 KBi
#20
Correcto
0.006 s 1 KBi
Puntos totales: 100 / 100

Código

#include <iostream>
#include <vector>

using namespace std;

int main () {
  int N;
  cin >> N;

  vector<vector<int> > matriz(N, vector<int>(N));
  int verticalResults[N];
  int horizontalResults[N];
  int diagonalResults[2] = {0, 0};

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> matriz[i][j];
    }
  }

  //vertical-horizontal check
  int verticalSum, horizontalSum;
  for (int i = 0; i < N; i++) {
    verticalSum = 0;
    horizontalSum = 0;
    for (int j = 0; j < N; j++) {
      horizontalSum += matriz[i][j];
      verticalSum += matriz[j][i];
    }
    verticalResults[i] = verticalSum;
    horizontalResults[i] = horizontalSum;
  }

  // diagonal check
  for (int i = 0, j = N-1; i < N; i++, j--) {
    diagonalResults[0] += matriz[i][i];
    diagonalResults[1] += matriz[i][j];
  }

  bool isMagic = true;
  int aux = horizontalResults[0];
  for (int i = 0; i < N; i++) {
    isMagic = isMagic && horizontalResults[i] == aux;
    isMagic = isMagic && verticalResults[i] == aux;
  }
  isMagic = isMagic && diagonalResults[0] == aux;
  isMagic = isMagic && diagonalResults[1] == aux;

  if (isMagic) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}