Advertisement
MiinaMagdy

11221 - Magic square palindromes.

Sep 6th, 2022
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define ll long long
  6. #define endl '\n'
  7. #define sz(x) int(x.size())
  8. #define all(x) x.begin(), x.end()
  9.  
  10. int main() {
  11.     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  12.     int testcases;
  13.     cin >> testcases;
  14.     for (int test = 1; test <= testcases; test++) {
  15.         cout << "Case #" << test << ":\n";
  16.         string tmp, s;
  17.         getline(cin >> ws, tmp);
  18.         for (int i = 0; i < sz(tmp); i++) if (isalpha(tmp[i])) s += tmp[i];
  19.         int q = sqrt(sz(s));
  20.         if (q * q == sz(s)) {
  21.             string t[4];
  22.             for (int i = 0; i < q; i++)
  23.                 for (int j = 0; j < q; j++)
  24.                     t[0] += s[q * i + j];
  25.                    
  26.            
  27.             for (int i = 0; i < q; i++)
  28.                 for (int j = 0; j < q; j++)
  29.                     t[1] += s[i + q * j];
  30.                    
  31.             for (int i = q - 1; i >= 0; i--)
  32.                 for (int j = q - 1; j >= 0; j--)
  33.                     t[2] += s[q * i + j];
  34.                    
  35.             for (int i = q - 1; i >= 0; i--)
  36.                 for (int j = q - 1; j >= 0; j--)
  37.                     t[3] += s[i + q * j];
  38.                    
  39.             if (t[0] == t[1] && t[1] == t[2] && t[2] == t[3]) {
  40.                 cout << q << endl;
  41.             }
  42.             else {
  43.                 cout << "No magic :(" << endl;
  44.             }
  45.         }
  46.         else {
  47.             cout << "No magic :(" << endl;
  48.         }
  49.     }
  50. }
  51.  
Tags: UVA CP3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement