Advertisement
FNSY

Untitled

Apr 12th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. char trial[8][8] = {{'0', '0', '0', '0', '0', '0', '0', '0'},
  5. {
  6. '0', '0', '0', '0', '0', '0', '0', '0'},
  7. {
  8. '0', '0', '0', '0', '0', '0', '0', '0'},
  9. {
  10. '0', '0', '0', '0', '0', '0', '0', '0'},
  11. {'0', '0', '0', '0', '0', '0', '0', '0'},
  12. {
  13. '0', '0', '0', '0', '0', '0', '0', '0'},
  14. {
  15. '0', '0', '0', '0', '0', '0', '0', '0'},
  16. {
  17. '0', '0', '0', '0', '0', '0', '0', '0'}};
  18.  
  19. int counter = 0;
  20.  
  21. void putQueen(int rowIndex, int columnIndex) {
  22. trial[rowIndex][columnIndex] = '1';
  23. }
  24.  
  25. void removeQueen(int rowIndex, int columnIndex) {
  26. trial[rowIndex][columnIndex] = '0';
  27. }
  28.  
  29. bool canPut(int r, int c) {
  30. int rowIndex = r;
  31. int columnIndex = c;
  32. while (rowIndex < sizeof(trial[0]) / sizeof(trial[0][0]) && columnIndex < sizeof(trial[0]) / sizeof(trial[0][0]))
  33. if (trial[rowIndex++][columnIndex++] == '1')
  34. return false;
  35.  
  36. rowIndex = r;
  37. columnIndex = c;
  38. while (rowIndex >= 0 && columnIndex >= 0)
  39. if (trial[rowIndex--][columnIndex--] == '1')
  40. return false;
  41.  
  42. rowIndex = r;
  43. columnIndex = c;
  44. while (rowIndex >= 0 && columnIndex < sizeof(trial[0]) / sizeof(trial[0][0]))
  45. if (trial[rowIndex--][columnIndex++] == '1')
  46. return false;
  47.  
  48.  
  49. rowIndex = r;
  50. columnIndex = c;
  51. while (rowIndex < sizeof(trial[0]) / sizeof(trial[0][0]) && columnIndex >= 0)
  52. if (trial[rowIndex++][columnIndex--] == '1')
  53. return false;
  54.  
  55.  
  56. for (int i = r; i < sizeof(trial[0]) / sizeof(trial[0][0]); i++)
  57. if (trial[i][c] == '1')
  58. return false;
  59.  
  60. for (int i = c; i < sizeof(trial[0]) / sizeof(trial[0][0]); i++)
  61. if (trial[r][i] == '1')
  62. return false;
  63.  
  64.  
  65. for (int i = r; i >= 0; i--)
  66. if (trial[i][c] == '1')
  67. return false;
  68.  
  69. for (int i = c; i >= 0; i--)
  70. if (trial[r][i] == '1')
  71. return false;
  72. return true;
  73.  
  74.  
  75. }
  76.  
  77.  
  78. void queen(int rowIndex) {
  79. if (rowIndex == 8)
  80. counter++;
  81.  
  82. for (int columnIndex = 0; columnIndex < 8; columnIndex++)
  83. if (canPut(rowIndex, columnIndex)) {
  84. putQueen(rowIndex, columnIndex);
  85. queen(rowIndex + 1);
  86. removeQueen(rowIndex, columnIndex);
  87. // لو اللي بعدي ملقاش مكان يتحط فيه، شيلني وغير مكاني وكمل
  88. //او لو لقيت طريقة ناجحة وعايز ارجع عشان اشوف بقيت الطرق
  89. }
  90. }
  91.  
  92. int main() {
  93. queen(0);
  94. cout << counter << endl;
  95. return 0;
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement