Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define NMAX 15
- int n;
- int st[NMAX];
- bool vis[NMAX];
- int sols;
- inline bool valid(int top, int x)
- {
- if (vis[x])
- return false;
- for (int i = 1; i < top; i++)
- {
- if (abs(top - i) == abs(x - st[i]))//damaCurr(coord:top, x;) altele:linia i si col Ast[i]
- return false;
- }
- return true;
- }
- inline void display()
- {
- for (int i = 1; i <= n; i++)
- cout << st[i] << ' ';
- cout << '\n';
- }
- void backtracking(int top)
- {
- if (top == n + 1)
- {
- sols++;
- if (sols == 1)
- display();
- return;
- }
- for (int i = 1; i <= n; i++)
- {
- if (valid(top, i))
- {
- vis[i] = true;
- st[top] = i;
- backtracking(top + 1);
- vis[i] = false;
- }
- }
- }
- signed main()
- {
- cin >> n;
- backtracking(1);
- cout << sols;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement