Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. #include <iomanip.h>
  2. #include <iostream.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <algorithm>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. int constraintsViolated(vector<int> &Q) {
  10. int a, b, c = 0, i, j, n;
  11.  
  12. n = Q.size();
  13. for (i = 0; i < n; i++) {
  14. a = Q[i];
  15. for (j = 0; j < n; j++) {
  16. b = Q[j];
  17. if (i != j && a != - 1 && b != - 1) {
  18. if (a == b) c++;
  19. if (i - j == a - b || i - j == b - a) c++;
  20. }
  21. }
  22. }
  23. return c;
  24. }
  25.  
  26. const int MaxQueens = 100000;
  27. int LevelOf[MaxQueens];
  28. int backtrackTo[MaxQueens] = {- 1};
  29.  
  30. int AnalyseBTLevel(int L, int x, vector<int> compoundLabel, vector<int> Dx) {
  31. bool NoConflict;
  32. int Level = - 1, Temp, a, b, i, j;
  33.  
  34. for (i = 0; i < Dx.size(); i++) {
  35. a = Dx[i];
  36. Temp = L - 1;
  37. NoConflict = true;
  38. for (j = 0; j < compoundLabel.size(); j++) {
  39. if (compoundLabel[j] != - 1) {
  40. b = compoundLabel[j];
  41. if (i != j && a != - 1 && b != - 1) {
  42. if (a == b) NoConflict = false;
  43. if (i - j == a - b || i - j == b - a) NoConflict = false;
  44. if (!NoConflict)
  45. Temp = Temp < LevelOf[j] ? Temp : LevelOf[j];
  46. }
  47. }
  48. }
  49. if (NoConflict) return LevelOf[x] - 1;
  50. Level = (Level > Temp) ? Level : Temp;
  51. }
  52. return Level;
  53. }
  54.  
  55. int Backjump(int L, vector<int> unlabelled, vector<int> compoundLabel,
  56. vector<int> &solution, vector<int> *D) {
  57. int Level = L, Result, i, j, v, x;
  58.  
  59. if (unlabelled.empty()) {
  60. for (i = 0; i < compoundLabel.size(); i++)
  61. solution[i] = compoundLabel[i];
  62. return true;
  63. }
  64. // pick a random variable from unlabelled array
  65. i = rand() % unlabelled.size();
  66. x = unlabelled[i];
  67. vector<int> TDx(D[x].size());
  68. copy(D[x].begin(), D[x].end(), TDx.begin());
  69. LevelOf[x] = L;
  70. do {
  71. // pick a random value from domain of x
  72. j = rand() % TDx.size();
  73. v = TDx[j];
  74. vector<int>::iterator vIterator = find(TDx.begin(), TDx.end(), v);
  75. TDx.erase(vIterator);
  76. compoundLabel[x] = v;
  77. if (constraintsViolated(compoundLabel) == 0) {
  78. vIterator = find(unlabelled.begin(), unlabelled.end(), x);
  79. unlabelled.erase(vIterator);
  80. Result = Backjump(L + 1, unlabelled, compoundLabel, solution, D);
  81. if (Result != backtrackTo[Level]) return Result;
  82. unlabelled.push_back(x);
  83. }
  84. else
  85. compoundLabel[x] = - 1;
  86. } while (!TDx.empty() && (Result != backtrackTo[Level] || Level >= L));
  87. if (Result == backtrackTo[Level] && Level < L) return backtrackTo[Level];
  88. Level = AnalyseBTLevel(L, x, compoundLabel, D[x]);
  89. backtrackTo[Level] = Level;
  90. return Level;
  91. }
  92.  
  93. void printSolution(vector<int> &solution) {
  94. char hyphen[256];
  95. int column, i, i4, n = solution.size(), row;
  96.  
  97. if (n <= 10) {
  98. for (i = 0; i < n; i++) {
  99. i4 = i * 4;
  100. hyphen[i4 + 0] = '-';
  101. hyphen[i4 + 1] = '-';
  102. hyphen[i4 + 2] = '-';
  103. hyphen[i4 + 3] = '-';
  104. }
  105. i4 = i * 4;
  106. hyphen[i4 + 0] = '-';
  107. hyphen[i4 + 1] = 'n';
  108. hyphen[i4 + 2] = '';
  109. for (row = 0; row < n; row++) {
  110. column = solution[row];
  111. cout << hyphen;
  112. for (i = 0; i < column; i++)
  113. cout << "| ";
  114. cout << "| Q ";
  115. for (i = column + 1; i < n; i++)
  116. cout << "| ";
  117. cout << '|' << endl;
  118. }
  119. cout << hyphen;
  120. }
  121. else
  122. for (row = 0; row < n; row++)
  123. cout << row << ' ' << solution[row] << endl;
  124. }
  125.  
  126. int main(int argc, char *argv[]) {
  127. if (argc != 2) {
  128. cout << "usage: " << argv[0] << " numberOfQueens" << endl;
  129. exit(1);
  130. }
  131. int i, j, n = atoi(argv[1]);
  132. if (n > MaxQueens) {
  133. cout << "error: too many queens maximum = " << MaxQueens << endl;
  134. exit(1);
  135. }
  136. vector<int> compoundLabel(n, - 1), solution(n, - 1), unlabelled(n);
  137. vector<int> *D = new vector<int>[n];
  138. if (D == NULL) {
  139. cout << "error: can't allocate memory in main" << endl;
  140. exit(1);
  141. }
  142. for (i = 0; i < n; i++) {
  143. unlabelled[i] = i;
  144. for (j = 0; j < n; j++)
  145. D[i].push_back(j);
  146. }
  147. for (i = 0; i < n; i++)
  148. unlabelled[i] = i;
  149. srand(time(NULL));
  150. Backjump(1, unlabelled, compoundLabel, solution, D);
  151. printSolution(solution);
  152. return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement