Advertisement
Farjana_akter

Untitled

Apr 1st, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int chess[10][10],row,col,cnt=0;
  5.  
  6.  
  7. bool check(int ro,int column)
  8. {
  9. for(int i=0;i<8;i++){
  10. if(chess[i][column])
  11. return false;
  12. }
  13. for(int j=0;j<8;j++)
  14. {
  15. if(chess[ro][j])
  16. return false;
  17. }
  18. for(int i=0;i<8;i++)
  19. {
  20. for(int j=0;j<8;j++)
  21. {
  22. if(chess[i][j] && abs(ro-i)==abs(column-j))
  23. return false;
  24. }
  25. }
  26. return true;
  27. }
  28.  
  29.  
  30.  
  31. void queen(int queencnt,int column)
  32. {
  33. if(queencnt==0 && column==8 && chess[row][col]==1)
  34. {
  35. cnt++;
  36. printf("%2d ",cnt);//6ta space
  37. for(int i=0;i<8;i++)
  38. {
  39. for(int j=0;j<8;j++)
  40. {
  41. if(chess[j][i]==1 && i==0)
  42. printf("%d",j+1);
  43. else if(chess[j][i]==1)
  44. printf(" %d",j+1);
  45. }
  46. }
  47. cout<<endl;
  48. }
  49. for(int x=0;x<8;x++)
  50. {
  51. if(check(x,column)==true)
  52. {
  53. chess[x][column]=1;
  54. queen(queencnt-1,column+1);
  55. chess[x][column]=0;
  56. }
  57. }
  58.  
  59. }
  60.  
  61.  
  62. int main()
  63. {
  64. int t,cas,i,j,k;
  65. cin>>t;
  66. for(cas=1;cas<=t;cas++)
  67. {
  68. cin>>row>>col;
  69. row--,col--;
  70. cnt=0;
  71. memset(chess,0,sizeof(chess));
  72. printf("SOLN COLUMN\n");//7ta space;
  73. printf(" # 1 2 3 4 5 6 7 8\n\n");//prothome 1 ta pore 8ta
  74. queen(8,0);
  75. if(cas!=t)
  76. cout<<endl;
  77. }
  78. return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement