Advertisement
rootUser

N-Queen (own)

Aug 14th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4.  
  5. int totalQueens,possibleSolution=1;
  6. int validQueenColumns_x[100];
  7. void input();
  8. int place(int,int);
  9. void N_QUEENS(int,int);
  10.  
  11. int main(void)
  12. {
  13.     input();
  14.     N_QUEENS(1,totalQueens);
  15.     return 0;
  16. }
  17.  
  18. void input()
  19. {
  20.     cout<<"How many queens : ";
  21.     cin>>totalQueens;
  22. }
  23.  
  24. int place(int queenRow_k,int queenColumn_i)
  25. {
  26.     int j;
  27.     for(j=1; j<=queenRow_k-1; j++)
  28.     {
  29.         if( (queenColumn_i==validQueenColumns_x[j]) || (abs(queenRow_k-j)==abs(queenColumn_i-validQueenColumns_x[j])) )
  30.         {
  31.             return 0;
  32.         }
  33.     }
  34.     return 1;
  35. }
  36. void N_QUEENS(int whichQueen_k,int totalQueens_n)
  37. {
  38.     int i,j;
  39.     for(i=1; i<=totalQueens; i++)
  40.     {
  41.         if(place(whichQueen_k,i)==1)
  42.         {
  43.             validQueenColumns_x[whichQueen_k]=i;
  44.             if(whichQueen_k==totalQueens_n)
  45.             {
  46.                 cout<<"Possible solution "<<possibleSolution<<" : "<<endl;
  47.                 for(j=1; j<=totalQueens_n; j++)
  48.                 {
  49.                     cout<<validQueenColumns_x[j]<<" ";
  50.                 }
  51.                 cout<<endl;
  52.                 possibleSolution++;
  53.             }
  54.             else
  55.             {
  56.                 N_QUEENS(whichQueen_k+1,totalQueens_n);
  57.             }
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement