Advertisement
mickypinata

PROG-T1017: Magic Square

Sep 18th, 2021
739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 10;
  5. const int N2 = N * N;
  6.  
  7. int board[N + 10][N + 10];
  8. bool found[N2 + 10];
  9.  
  10. int main(){
  11.  
  12.     int n;
  13.     scanf("%d", &n);
  14.  
  15.     int crossSumL = 0;
  16.     int crossSumR = 0;
  17.     int lastSum;
  18.     for(int i = 1; i <= n; ++i){
  19.         int rowSum = 0;
  20.         for(int j = 1; j <= n; ++j){
  21.             scanf("%d", &board[i][j]);
  22.             found[board[i][j]] = true;
  23.             rowSum += board[i][j];
  24.             if(i == j){
  25.                 crossSumR += board[i][j];
  26.             }
  27.             if(i + j == n + 1){
  28.                 crossSumL += board[i][j];
  29.             }
  30.         }
  31.         if(i == 1){
  32.             lastSum = rowSum;
  33.         } else if(rowSum != lastSum){
  34.             cout << "No";
  35.             return 0;
  36.         }
  37.     }
  38.     if(!(crossSumL == crossSumR && crossSumL == lastSum)){
  39.         cout << "No";
  40.         return 0;
  41.     }
  42.  
  43.     for(int i = 1; i <= n; ++i){
  44.         int colSum = 0;
  45.         for(int j = 1; j <= n; ++j){
  46.             colSum += board[j][i];
  47.         }
  48.         if(colSum != lastSum){
  49.             cout << "No";
  50.             return 0;
  51.         }
  52.     }
  53.  
  54.     int n2 = n * n;
  55.     for(int i = 1; i <= n2; ++i){
  56.         if(!found[i]){
  57.             cout << "No";
  58.             return 0;
  59.         }
  60.     }
  61.     cout << "Yes";
  62.  
  63.     return 0;
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement