Guest User

Untitled

a guest
Feb 16th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n, total = 0;
  6. bool *col, *diag1, *diag2;
  7.  
  8. // в этой функции тот самый алгоритм перебора с возвратом
  9. void search(int y){
  10. if (y == n){
  11. total++;
  12. return;
  13. }
  14. for (int x=0; x<n; x++){
  15. if (col[x] || diag1[x+y] || diag2[x-y+n-1]) continue;
  16. col[x] = diag1[x+y] = diag2[x-y+n-1] = 1;
  17. search(y+1);
  18. col[x] = diag1[x+y] = diag2[x-y+n-1] = 0;
  19. }
  20. }
  21.  
  22. int main(){
  23. ios::sync_with_stdio(0);
  24. cin.tie(0);
  25.  
  26. cin >> n;
  27. col = new bool[n];
  28. diag1 = new bool[n+(n-1)];
  29. diag2 = new bool[n+(n-1)];
  30. search(0);
  31. cout << total << endl;
  32. delete[] col;
  33. delete[] diag1;
  34. delete[] diag2;
  35.  
  36. system("pause");
  37. return 0;
  38. }
Add Comment
Please, Sign In to add comment