Advertisement
Guest User

NQueens - Gerarld Merales - SIP Intern

a guest
Aug 18th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include<iostream>
  2. #include<math.h>
  3. using namespace std;
  4.  
  5. bool end = false;
  6. int temp;
  7. int board[20];
  8. int level = 1;
  9. bool valid;
  10. int n;
  11. foo(){
  12. if(!end){
  13. // cout<<"-----strt func-----"<<endl;
  14. valid = true;
  15. for(int i = 1; i <= n ; i++){
  16. //check if valid
  17. valid = true;
  18. {
  19. for(int ctr1=0; ctr1<level-1; ctr1++){
  20. if(board[ctr1]==i){
  21. valid = false;
  22. // cout<<board[ctr1]<<" "<<i<<endl<<"hori"<<endl;
  23. ctr1 = level;
  24. }
  25. if(abs(level-ctr1-1)==abs(i-board[ctr1])){
  26. valid = false;
  27. ctr1 = level;
  28. // cout<<"diag"<<endl;
  29. }
  30. }
  31. }
  32. if(valid){
  33. board[level-1]=i;
  34. if(level==n){
  35. for(int ctr1=0 ; ctr1<n ; ctr1++){
  36. cout<<board[ctr1]<<" ";
  37. }
  38. end = true;
  39. }
  40. level++;
  41. // cout<<"----- so far -----"<<endl;
  42. // for(int ctr1=0 ; ctr1<4 ; ctr1++){
  43. // cout<<board[ctr1];
  44. // }
  45. // cout<<endl<<"----- valid -----"<<endl;
  46. foo();
  47. level--;
  48. }
  49. if(!end){
  50. // cout<<"level = " << level <<endl;
  51. // cout<<"i = "<<i<<endl;
  52. // cout<<"-----end func-----"<<endl<<endl;
  53. }
  54. }
  55. }
  56. }
  57.  
  58. int main(){
  59. cin >> n;
  60.  
  61. foo();
  62.  
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement