Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int totalQueens,possibleSolution=1;
- int validQueenColumns_x[100];
- void input();
- int place(int,int);
- void N_QUEENS(int,int);
- int main(void)
- {
- input();
- N_QUEENS(1,totalQueens);
- return 0;
- }
- void input()
- {
- cout<<"How many queens : ";
- cin>>totalQueens;
- }
- int place(int queenRow_k,int queenColumn_i)
- {
- int j;
- for(j=1; j<=queenRow_k-1; j++)
- {
- if( (queenColumn_i==validQueenColumns_x[j]) || (abs(queenRow_k-j)==abs(queenColumn_i-validQueenColumns_x[j])) )
- {
- return 0;
- }
- }
- return 1;
- }
- void N_QUEENS(int whichQueen_k,int totalQueens_n)
- {
- int i,j;
- for(i=1; i<=totalQueens; i++)
- {
- if(place(whichQueen_k,i)==1)
- {
- validQueenColumns_x[whichQueen_k]=i;
- if(whichQueen_k==totalQueens_n)
- {
- cout<<"Possible solution "<<possibleSolution<<" : "<<endl;
- for(j=1; j<=totalQueens_n; j++)
- {
- cout<<validQueenColumns_x[j]<<" ";
- }
- cout<<endl;
- possibleSolution++;
- }
- else
- {
- N_QUEENS(whichQueen_k+1,totalQueens_n);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement