Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n, l = 1;
  6. int mtr[11][11];
  7. int dx[] = {0, 1, 0, -1};
  8. int dy[] = {1, 0, -1, 0};
  9.  
  10. void print() {
  11.     for (int i = 1; i <= n; i++) {
  12.         for (int j = 1; j <= n; j++) {
  13.             cout << mtr[i][j] << " ";
  14.         }
  15.         cout << endl;
  16.     }
  17.     exit(0);
  18. }
  19.  
  20. void spiral(int px, int py, int zn, int np, int v) {
  21.     if (zn == n * n + 1) {
  22.         print();
  23.     }
  24.     if (zn == v) {
  25.         v += n - l;
  26.         l += np % 2;
  27.         np = (np + 1) % 4;
  28.     }
  29. //    cout << "px = " << px << " py = " << py << " v = " << v << " np = " << np << "\n";
  30.     mtr[px][py] = zn;
  31.     spiral(px + dx[np], py + dy[np], zn + 1, np, v);
  32.  
  33. }
  34.  
  35. int main() {
  36.     cin >> n;
  37.     spiral(1, 1, 1, 0, n);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement