Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. #include <cstring>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. struct rightWay {
  7. int A[3][3];
  8. rightWay *next;
  9. rightWay *prev;
  10. };
  11.  
  12. struct allWays {
  13. int A[3][3];
  14. allWays *next;
  15. };
  16.  
  17. const int position[4][2] = {
  18. {-1, 0},
  19. {0, 1},
  20. {1, 0},
  21. {0, -1}
  22. };
  23.  
  24. int findZeroPosition(int startPosition[3][3], int *zeroX, int *zeroY) {
  25. for (int i = 0; i < 3; i++)
  26. for (int j = 0; j < 3; j++)
  27. if (startPosition[i][j] == 0) {
  28. *zeroX = i;
  29. *zeroY = j;
  30. return 0;
  31. }
  32. return -1;
  33. }
  34.  
  35. bool compareArrays(int A[3][3], int B[3][3]) {
  36. for (int i = 0; i < 3; i++)
  37. for (int j = 0; j < 3; j++)
  38. if (A[i][j] != B[i][j])
  39. return false;
  40. return true;
  41. }
  42.  
  43. bool compareArrays(int **swapResult, allWays *headAllWays) {
  44.  
  45. while (headAllWays != nullptr) {
  46. int flag = 0;
  47. for (int i = 0; i < 3; i++)
  48. for (int j = 0; j < 3; j++) {
  49. if (headAllWays->A[i][j] == swapResult[i][j])
  50. flag++;
  51. }
  52.  
  53. if (flag == 9)
  54. return true;
  55. headAllWays = headAllWays->next;
  56. }
  57. return false;
  58. }
  59.  
  60. int **swap(int currentArray[3][3], int zeroX, int zeroY, int move) {
  61.  
  62. int **array = new int *[3];
  63. for (int i = 0; i < 3; i++) {
  64. array[i] = new int[3];
  65. }
  66. memcpy(array, currentArray, 9 * sizeof(int));
  67.  
  68. array[zeroX][zeroY] = currentArray[zeroX + position[move][0]][zeroY + position[move][1]];
  69. array[zeroX + position[move][0]][zeroY + position[move][1]] = 0;
  70.  
  71. return array;
  72. }
  73.  
  74. int main() {
  75. rightWay *headRightWay, *currentRightWay = new rightWay;
  76. allWays *headAllWays, *currentAllWays = new allWays;
  77.  
  78. int startPosition[3][3] = {
  79. {5, 8, 3},
  80. {4, 0, 2},
  81. {7, 6, 1}
  82. };
  83.  
  84. memcpy(currentRightWay->A, startPosition, 9 * sizeof(int));
  85. headRightWay = currentRightWay;
  86.  
  87. memcpy(currentAllWays->A, startPosition, 9 * sizeof(int));
  88. headAllWays = currentAllWays;
  89.  
  90. int endPosition[3][3] = {
  91. {1, 2, 3},
  92. {4, 5, 6},
  93. {7, 8, 0}
  94. };
  95.  
  96. int zeroX, zeroY;
  97. findZeroPosition(startPosition, &zeroX, &zeroY);
  98.  
  99. do {
  100. for (int i = 0; i < 4; i++) {
  101. if ((zeroX + position[i][0] >= 0) && (zeroX + position[i][0] <= 2) && (zeroY + position[i][1] >= 0) &&
  102. (zeroY + position[i][1] <= 2)) {
  103. int **swapResult = swap(currentAllWays->A, zeroX, zeroY, i);
  104. if (!compareArrays(swapResult, headAllWays)) {
  105. cout << "ok!";
  106. return 0;
  107. }
  108. }
  109. }
  110.  
  111. } while (!compareArrays(currentRightWay->A, endPosition));
  112.  
  113. return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement