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

Envío 6514

Problema 0xe1 - Cuadrado mágico

  • Autor: danidiaztech
  • Fecha: 2022-07-11 04:35:39 UTC (Hace más de 2 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.002 s 0 KBi
#2
Correcto
0.004 s 41 KBi
#3
Correcto
0.002 s 0 KBi
#4
Correcto
0.005 s 25 KBi
#5
Correcto
0.002 s 0 KBi
#6
Correcto
0.002 s 0 KBi
#7
Correcto
0.001 s 0 KBi
#8
Correcto
0.002 s 0 KBi
#9
Correcto
0.003 s 0 KBi
#10
Correcto
0.002 s 0 KBi
#11
Correcto
0.002 s 0 KBi
#12
Correcto
0.005 s 11 KBi
#13
Correcto
0.002 s 0 KBi
#14
Correcto
0.002 s 0 KBi
#15
Correcto
0.002 s 0 KBi
#16
Correcto
0.004 s 1 KBi
#17
Correcto
0.007 s 1 KBi
#18
Correcto
0.002 s 0 KBi
#19
Correcto
0.003 s 0 KBi
#20
Correcto
0.003 s 0 KBi
Puntos totales: 100 / 100

Código

// Find if a matrix n x n is a magic square
#include <bits/stdc++.h>

using namespace std;

int main(){
    int n, h, d, d2, v, horizontal, vertical, LIM; 
    cin >> n;
    
    int arr[n][n];

    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            cin >> arr[i][j];
        }
    }
    // -1000 <= Ai, j <=  1000
    LIM = 1000 * n + 1;
    
    horizontal = vertical = LIM;

    for (int i = 0; i < n; i++){
        h = 0;
        v = 0;
        // horizontal / rows 
        for (int j = 0; j < n; j++){
            h += arr[i][j];
            v += arr[j][i];
        }
        if (horizontal == LIM){
            horizontal = h;
        }
        else if (h != horizontal){
                cout << "No";
                return 0;
        }
        // Vertical / columns
        if (vertical == LIM){
            vertical = v;
        }
        else if (v != vertical){
                cout << "No";
                return 0;
        }
    }
    d = 0;
    d2 = 0;

    // diagonal
    for (int j = 0; j < n; j++){
        d += arr[j][j];
    }
    if (d != horizontal){
            cout << "No";
            return 0;
    }

    // diagonal 2
    int rowindex = 0;
    for (int j = n - 1; j > -1; j--){
        d2 += arr[rowindex][j];
        rowindex += 1; 
    }
    if (d2 != horizontal){
            cout << "No";
            return 0;
    }
    // cout << horizontal << vertical << d << d2;

    cout << "Yes";
    return 0;
}