Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n, total = 0;
- bool *col, *diag1, *diag2;
- // в этой функции тот самый алгоритм перебора с возвратом
- void search(int y){
- if (y == n){
- total++;
- return;
- }
- for (int x=0; x<n; x++){
- if (col[x] || diag1[x+y] || diag2[x-y+n-1]) continue;
- col[x] = diag1[x+y] = diag2[x-y+n-1] = 1;
- search(y+1);
- col[x] = diag1[x+y] = diag2[x-y+n-1] = 0;
- }
- }
- int main(){
- ios::sync_with_stdio(0);
- cin.tie(0);
- cin >> n;
- col = new bool[n];
- diag1 = new bool[n+(n-1)];
- diag2 = new bool[n+(n-1)];
- search(0);
- cout << total << endl;
- delete[] col;
- delete[] diag1;
- delete[] diag2;
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment