Advertisement
kimo12

Untitled

Apr 12th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.    int board[8][8];
  5.    int counter = 0;
  6. void print(){
  7.            for(int i=0;i<8;i++){
  8.                 cout<<"\t\t\t";
  9.                 for(int j=0;j<8;j++)
  10.             {
  11.                 cout<<board[i][j]<<"   ";
  12.             }
  13.             cout<<"\n\n";
  14.                             }
  15.                             cout<<"\n\n";
  16.  
  17. }
  18.  
  19.  
  20. void put(int x,int y){
  21.     board[x][y]=1;
  22. }
  23.  
  24. void rem(int x,int y){
  25.  
  26. board[x][y]=0;
  27. }
  28. bool canput(int x,int y)
  29. {
  30.     bool   flag = true;
  31.         for(int i=0;i<8;i++){
  32.                 for(int j=0; j<8;j++){
  33.                                 if(x == i && board[i][j]==1){
  34.                                     return false;
  35.                                 }
  36.                                 else if(y == j && board[i][j]==1){
  37.                                     return false;
  38.                                 }
  39.                                 else if(x-y==i-j && board[i][j]==1){
  40.                                     return false;
  41.                                 }
  42.                                 else if(y+x==i+j && board[i][j]==1){
  43.                                     return false;
  44.                                 }
  45.                                 }
  46.                     if(i==7){
  47.                         return true;
  48.                     }
  49.                     }
  50. }
  51.  
  52. void queen(int index =0)
  53. {
  54.     if(index==8){
  55.         print();
  56.         counter++;
  57.         return;
  58.     }
  59.     for(int y = 0;y < 8 ;y++){
  60.         if(canput(index,y)){
  61.         put(index,y);
  62.         queen(index+1);
  63.         rem(index,y);
  64.         }
  65.     }
  66. }
  67.  
  68.  
  69. int main()
  70. {
  71.  
  72.     for(int i=0;i<8;i++)
  73. {
  74.         for(int j=0;j<8;j++)
  75.         {
  76.           board[i][j]=0;
  77.         }
  78. }
  79.     queen();
  80.     cout<<counter<<endl;
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement