Advertisement
Korotkodul

N4 v3

Dec 17th, 2022 (edited)
893
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int, int>
  11. #define pb(x) push_back(x)
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. using db = double;
  16. void cv(vector <int> &v) {
  17.     for (auto x : v) cout << x << ' ';
  18.     cout << "\n";
  19. }
  20.  
  21. void cvl(vector <ll> &v) {
  22.     for (auto x : v) cout << x << ' ';
  23.     cout << "\n";
  24. }
  25.  
  26.  
  27. void cvv(vector <vector <int> > &v) {
  28.     for (auto x : v) cv(x);
  29.     cout << "\n";
  30. }
  31.  
  32. void cvb(vector <bool> v) {
  33.     for (bool x : v) cout << x << ' ';
  34.     cout << "\n";
  35. }
  36.  
  37. void cvs(vector <string>  v) {
  38.     for (auto a : v) {
  39.         cout << a << "\n";
  40.     }
  41. }
  42.  
  43. void cvp(vector <pii> a) {
  44.     for (auto p : a) {
  45.         cout << p.first << ' ' << p.second << "\n";
  46.     }
  47.     cout << "\n";
  48. }
  49.  
  50. vector <int> dx, dy;
  51. int n;
  52. vector <vector <int> > m, id;
  53.  
  54. bool ok1(int i, int j) {
  55.     return i >= 0 && j >= 0 && i + j < n - 1;// && id[i][j] == 0;
  56. }
  57.  
  58. bool ok2(int i, int j) {
  59.     return i < n && j < n && i + j > n - 1;// && id[i][j] == 0;
  60. }
  61.  
  62. bool sh = 0;
  63.  
  64. int main() {
  65.     /*ios::sync_with_stdio(0);
  66.     cin.tie(0);
  67.     cout.tie(0);*/
  68.     cin >> n;
  69.     if (n == 1) {
  70.         int x; cin >> x;
  71.         cout << x;
  72.         exit(0);
  73.     }
  74.     dx = { 1, -1, 0, 1,};
  75.     dy = { 0, 1, 1, -1,};
  76.     int i = 0, j = 0;
  77.     m.assign(n, vector <int> (n, 0));
  78.     id.assign(n, vector <int> (n, 0));
  79.     if (sh) {
  80.         cout << "START\n";
  81.         cvv(id);
  82.     }
  83.     if (sh) {
  84.         cout << "PART1\n";
  85.     }
  86.     int cnt = 1;
  87.     id[0][0] = 1;
  88.     int tk = 1;
  89.     while (tk != (n - 1) * n / 2) {
  90.         if (sh) {
  91.             cout << "step1\n";
  92.             cout << "("<<i<<","<<j<<")\n";
  93.         }
  94.         if (ok1(i + dx[0], j + dy[0]) && tk != (n - 1) * n / 2) {
  95.             cnt+=2;
  96.             tk++;
  97.             i = i + dx[0];
  98.             j = j + dy[0];
  99.             id[i][j] = cnt;
  100.         }
  101.         if (sh) {
  102.             cout << "step2\n";
  103.             cout << "("<<i<<","<<j<<")\n";
  104.         }
  105.         while (ok1(i + dx[1], j + dy[1]) && tk != (n - 1) * n / 2) {
  106.             cnt+=2;
  107.             tk++;
  108.             i += dx[1];
  109.             j += dy[1];
  110.             id[i][j] = cnt;
  111.         }
  112.         if (sh) {
  113.             cout << "step3\n";
  114.             cout << "("<<i<<","<<j<<")\n";
  115.         }
  116.         if (ok1(i + dx[2], j + dy[2]) && tk != (n - 1) * n / 2) {
  117.             cnt+=2;
  118.             tk++;
  119.             i += dx[2];
  120.             j += dy[2];
  121.             id[i][j] = cnt;
  122.         }
  123.         if (sh) {
  124.             cout << "step4\n";
  125.             cout << "("<<i<<","<<j<<")\n";
  126.         }
  127.         while (ok1(i + dx[3], j + dy[3]) && tk != (n - 1) * n / 2 ) {
  128.             cnt+=2;
  129.             tk++;
  130.             i += dx[3];
  131.             j += dy[3];
  132.             id[i][j] = cnt;
  133.         }
  134.     }
  135.     if (sh) {
  136.         cvv(id);
  137.     }
  138.  
  139.     if (sh) {
  140.         cout << "PART 2\n";
  141.     }
  142.     dx = {0, -1, -1, 1};
  143.     dy = {-1, 1, 0, -1};
  144.     cnt = 2;
  145.     tk++;
  146.     id[n-1][n-1] = 2;
  147.     i = n - 1;
  148.     j = n - 1;
  149.     while (tk != (n - 1) * n) {
  150.         if (sh) {
  151.             cout << "step1\n";
  152.             cout << "("<<i<<","<<j<<")\n";
  153.         }
  154.         if (ok2(i + dx[0], j + dy[0]) && tk != (n - 1) * n ) {
  155.             cnt+=2;
  156.             tk++;
  157.             i = i + dx[0];
  158.             j = j + dy[0];
  159.             id[i][j] = cnt;
  160.         }
  161.  
  162.         if (sh) {
  163.             cout << "step2\n";
  164.             cout << "("<<i<<","<<j<<")\n";
  165.         }
  166.         while (ok2(i + dx[1], j + dy[1]) && tk != (n - 1) * n ) {
  167.             cnt+=2;
  168.             tk++;
  169.             i += dx[1];
  170.             j += dy[1];
  171.             id[i][j] = cnt;
  172.         }
  173.         if (sh) {
  174.             cout << "step3\n";
  175.             cout << "("<<i<<","<<j<<")\n";
  176.         }
  177.         if (ok2(i + dx[2], j + dy[2]) && tk != (n - 1) * n ) {
  178.             cnt+=2;
  179.             tk++;
  180.             i += dx[2];
  181.             j += dy[2];
  182.             id[i][j] = cnt;
  183.         }
  184.         if (sh) {
  185.             cout << "step4\n";
  186.             cout << "("<<i<<","<<j<<")\n";
  187.         }
  188.         while (ok2(i + dx[3], j + dy[3]) && tk != (n - 1) * n ) {
  189.             cnt+=2;
  190.             tk++;
  191.             i += dx[3];
  192.             j += dy[3];
  193.             id[i][j] = cnt;
  194.         }
  195.     }
  196.     if (sh) {
  197.         cvv(id);
  198.     }
  199.  
  200.     if (sh) {
  201.         cout << "PART3\n";
  202.     }
  203.     cnt++;
  204.     tk++;
  205.     i = 0;
  206.     j = n - 1;
  207.     id[i][j] = cnt;
  208.     while (tk != n * n) {
  209.         cnt++;
  210.         tk++;
  211.         i++;
  212.         j--;
  213.         id[i][j] = cnt;
  214.     }
  215.     if (sh) {
  216.         cvv(id);
  217.     }
  218.     for (int i = 0; i < n; ++i) {
  219.         for (int j = 0; j < n; ++j) {
  220.             cin >> m[i][j];
  221.         }
  222.     }
  223.     vector <int> ans(n*n,0);
  224.     for (int i = 0; i < n; ++i) {
  225.         for (int j = 0; j < n; ++j) {
  226.             ans[id[i][j] - 1] = m[i][j];
  227.         }
  228.     }
  229.     cv(ans);
  230. }
  231.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement