Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <map>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. vector<vector<int>> neighbours(vector<int> curr){ // туда пускаем вершину, а получаем вектор из соседей
  10. int zp = 0;
  11. for (int i = 0; i < 9; i++)
  12. if (curr[i] == 0)
  13. zp = i;
  14. vector<vector<int>> ans;
  15.  
  16. int zi = zp / 3;
  17. int zj = zp % 3;
  18.  
  19. if (zi > 0){
  20. auto tmp = curr;
  21. swap(tmp[zp],tmp[zp-3]);
  22. ans.push_back(tmp);
  23. }
  24.  
  25. if (zi < 2){
  26. auto tmp = curr;
  27. swap(tmp[zp],tmp[zp+3]);
  28. ans.push_back(tmp);
  29. }
  30.  
  31. if (zj > 0){
  32. auto tmp = curr;
  33. swap(tmp[zp],tmp[zp-1]);
  34. ans.push_back(tmp);
  35. }
  36.  
  37. if (zj < 2){
  38. auto tmp = curr;
  39. swap(tmp[zp],tmp[zp+1]);
  40. ans.push_back(tmp);
  41. }
  42.  
  43. return ans;
  44. }
  45.  
  46. bool solve( vector<int> v ){ // смотрим, можно ли решить
  47. int inv = 0;
  48. for (int i=0; i<9; ++i)
  49. if (v[i])
  50. for (int j=0; j<i; ++j)
  51. if (v[j] > v[i])
  52. ++inv;
  53. if (inv % 2 == 0)
  54. return true;
  55. else
  56. return false;
  57. }
  58.  
  59. char direction( vector<int> s, vector<int> f){ // определяем как попасть из s в f
  60. int zp1 = 0, zp2 = 0;
  61. for (int i = 0; i < 9; i++){
  62. if (s[i] == 0)
  63. zp1 = i;
  64. if (f[i] == 0)
  65. zp2 = i;
  66. }
  67. int d = zp2 - zp1;
  68. if (d == 3)
  69. return 'D';
  70. if (d == -3)
  71. return 'U';
  72. if (d == 1)
  73. return 'R';
  74. if (d == -1)
  75. return 'L';
  76. return 0; // default value
  77. }
  78.  
  79. void print_vec(vector<int> v){
  80. cout << v[0] << " " << v[1] << " " << v[2] << endl;
  81. cout << v[3] << " " << v[4] << " " << v[5] << endl;
  82. cout << v[6] << " " << v[7] << " " << v[8] << endl;
  83. cout << endl;
  84. }
  85. int main(){
  86. queue<vector<int>> q;
  87. vector<int> s(9);
  88. vector<int> f(9);
  89.  
  90. for (int i = 0; i < 9; i++){
  91. cin >> s[i];
  92. f[i] = i+1;
  93. }
  94. f[8] = 0;
  95.  
  96. if (!solve(s)){
  97. cout << -1 << endl;
  98. return 0;
  99. }
  100.  
  101. q.push(s);
  102. map<vector<int>, int> d; // расстояние
  103. map<vector<int>, vector<int>> p; // предки
  104. d[s] = 0;
  105. auto curr = s;
  106.  
  107.  
  108. while (curr != f){
  109. curr = q.front();
  110. q.pop();
  111. auto nb = neighbours(curr);
  112. for (auto elem : nb)
  113. if (d.find(elem) == d.end()){
  114. d[elem] = d[curr] + 1;
  115. q.push(elem);
  116. p[elem] = curr;
  117. }
  118. }
  119.  
  120. cout << d[f] << endl;
  121.  
  122. string path;
  123. curr = f;
  124. while (curr != s){
  125. path.push_back(direction(p[curr], curr));
  126. curr = p[curr];
  127. }
  128. reverse(path.begin(), path.end());
  129. cout << path << endl;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement