Advertisement
jaiswalsahil844

bug in code

Aug 18th, 2020 (edited)
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. bool Possible(char chess[][10], int row, int col, int n)
  6. {
  7.     //checking in left diagonal
  8.  
  9.     int i,j;
  10.     i=row-1;
  11.     j=col-1;
  12.            
  13.     while(i>=0 && j>=0)
  14.     {
  15.         if(chess[i][j]=='Q')
  16.             return false;
  17.         i--;
  18.         j--;
  19.     }
  20.  
  21.     i=row-1;
  22.     j=col+1; // see in ur code
  23.    
  24.     while(i>=0 && j<n)
  25.     {
  26.         if(chess[i][j]=='Q')
  27.             return false;
  28.         i--;
  29.         j++;
  30.     }
  31.  
  32.     i = row-1;
  33.     j = col;
  34.  
  35.     while(i>=0)
  36.     {
  37.         if(chess[i][j]=='Q')
  38.             return false;
  39.  
  40.         i--;
  41.     }
  42.  
  43.     return true;
  44. }
  45.  
  46. void placeNQueens(char chess[][10], int row,int n)
  47. {
  48.     if(row==n)
  49.     {
  50.         for(int i=0 ; i<n ; i++)
  51.         {
  52.             for(int j=0 ; j<n ; j++)
  53.                 cout<<chess[i][j]<<"\t";
  54.             cout<<"\n";
  55.         }
  56.         cout<<"\n";
  57.         return;
  58.     }
  59.  
  60.     for(int col=0 ; col<n ; col++)
  61.     {
  62.         // first check is it empty place or not
  63.         if(chess[row][col]=='-' and Possible(chess,row,col,n)) // see in ur code
  64.         {
  65.             chess[row][col] = 'Q';
  66.             placeNQueens(chess,row+1,n);
  67.             chess[row][col] = '-';
  68.         }
  69.     }
  70. }
  71.  
  72. void placeNQueens(int n)
  73. {
  74.     char chess[10][10];
  75.     memset(chess,'-',sizeof chess);
  76.     placeNQueens(chess,0,n);
  77. }
  78.  
  79. int main()
  80. {
  81.     int n;
  82.     // may be multiple testcase(question setter issue)
  83.     while(cin>>n){
  84.  
  85.     if(n<=3 && n!=1)
  86.         cout<<"Not Possible"<<"\n";
  87.     else
  88.         placeNQueens(n);
  89.     }
  90.    
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement