Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int board[8][8];
- int counter = 0;
- void print(){
- for(int i=0;i<8;i++){
- cout<<"\t\t\t";
- for(int j=0;j<8;j++)
- {
- cout<<board[i][j]<<" ";
- }
- cout<<"\n\n";
- }
- cout<<"\n\n";
- }
- void put(int x,int y){
- board[x][y]=1;
- }
- void rem(int x,int y){
- board[x][y]=0;
- }
- bool canput(int x,int y)
- {
- bool flag = true;
- for(int i=0;i<8;i++){
- for(int j=0; j<8;j++){
- if(x == i && board[i][j]==1){
- return false;
- }
- else if(y == j && board[i][j]==1){
- return false;
- }
- else if(x-y==i-j && board[i][j]==1){
- return false;
- }
- else if(y+x==i+j && board[i][j]==1){
- return false;
- }
- }
- if(i==7){
- return true;
- }
- }
- }
- void queen(int index =0)
- {
- if(index==8){
- print();
- counter++;
- return;
- }
- for(int y = 0;y < 8 ;y++){
- if(canput(index,y)){
- put(index,y);
- queen(index+1);
- rem(index,y);
- }
- }
- }
- int main()
- {
- for(int i=0;i<8;i++)
- {
- for(int j=0;j<8;j++)
- {
- board[i][j]=0;
- }
- }
- queen();
- cout<<counter<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement