Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n, l = 1;
- int mtr[11][11];
- int dx[] = {0, 1, 0, -1};
- int dy[] = {1, 0, -1, 0};
- void print() {
- for (int i = 1; i <= n; i++) {
- for (int j = 1; j <= n; j++) {
- cout << mtr[i][j] << " ";
- }
- cout << endl;
- }
- exit(0);
- }
- void spiral(int px, int py, int zn, int np, int v) {
- if (zn == n * n + 1) {
- print();
- }
- if (zn == v) {
- v += n - l;
- l += np % 2;
- np = (np + 1) % 4;
- }
- // cout << "px = " << px << " py = " << py << " v = " << v << " np = " << np << "\n";
- mtr[px][py] = zn;
- spiral(px + dx[np], py + dy[np], zn + 1, np, v);
- }
- int main() {
- cin >> n;
- spiral(1, 1, 1, 0, n);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement