Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <string>
  6. #include <set>
  7. #include <queue>
  8. #include <utility>
  9. #include <iomanip>
  10. #include <stack>
  11. #include <map>
  12. #include <time.h>
  13. using namespace std;
  14. const int inf = 1000000000 + 7;
  15. vector<vector<char>> CreateTruePytnaska(string s, const int& n) {
  16. vector<vector<char>> answer(n, vector<char>(n));
  17. int count = 0;
  18. s += "_";
  19. for (int i = 0; i < n; i++)
  20. for (int j = 0; j < n; j++)
  21. answer[i][j] = s[count++];
  22. return answer;
  23. }
  24. void PrintPytnaska(const vector<vector<char>>& v, const int& n) {
  25. for (int i = 0; i < n; i++) {
  26. for (int j = 0; j < n; j++)
  27. {
  28. cout << v[i][j] << " ";
  29. }
  30. cout << endl;
  31. }
  32. cout << endl;
  33. }
  34. void PrintMinWay(map<vector<vector<char>>, int>& d, const vector<vector<char>>& final, int steps, const int& n){
  35. int dx[4] = { 1, -1, 0, 0 };
  36. int dy[4] = { 0, 0, 1, -1 };
  37. vector<vector<char>> temp = final;
  38. int Steps = steps;
  39. vector<vector<vector<char>>> VectorToPrint = {final};
  40. while(steps > 0) {
  41. steps--;
  42. int x = 0, y = 0;
  43. for (int i = 0; i < n; i++)
  44. for (int j = 0; j < n; j++)
  45. if (temp[i][j] == '_') {
  46. x = i;
  47. y = j;
  48. }
  49. for (int i = 0; i < 4; i++) {
  50. int a = x + dx[i];
  51. int b = y + dy[i];
  52. if (a < 0 || b < 0 || a >= n || b >= n)
  53. continue;
  54. vector<vector<char>> f = temp;
  55. swap(f[x][y], f[a][b]);
  56. if(d.count(f) && d[f] == steps)
  57. {
  58. temp = f;
  59. VectorToPrint.push_back(f);
  60. break;
  61. }
  62. }
  63. }
  64. reverse(VectorToPrint.begin(),VectorToPrint.end());
  65. for(const auto& x : VectorToPrint)
  66. PrintPytnaska(x,n);
  67. cout << endl << "Done in " << Steps <<" steps" << endl;
  68. }
  69. void bfs(const int& n, const vector<vector<char>>& m, const string& s) {
  70. int dx[4] = { 1, -1, 0, 0 };
  71. int dy[4] = { 0, 0, 1, -1 };
  72. vector<vector<char>> final = CreateTruePytnaska(s, n);
  73. if (m == final) {
  74. cout << "Pytnaska already done" << endl;
  75. return;
  76. }
  77. queue <vector<vector<char>>> q;
  78. map <vector<vector<char>>, int> was;
  79. map<vector<vector<char>>, int> d;
  80. d[m] = 0;
  81. q.push(m);
  82. was[m] = 1;
  83. while (!q.empty()) {
  84. vector<vector<char>> temp = q.front();
  85. q.pop();
  86. int x = 0, y = 0;
  87. for (int i = 0; i < n; i++)
  88. for (int j = 0; j < n; j++)
  89. if (temp[i][j] == '_') {
  90. x = i;
  91. y = j;
  92. }
  93. for (int i = 0; i < 4; i++)
  94. {
  95. int a = x + dx[i];
  96. int b = y + dy[i];
  97. if (a < 0 || b < 0 || a >= n || b >= n)
  98. continue;
  99. vector<vector<char>> f = temp;
  100. swap(f[x][y], f[a][b]);
  101. if (f == final) {
  102. //PrintPytnaska(f, n);
  103.  
  104. PrintMinWay(d,final,d[temp] + 1,n);
  105. return;
  106.  
  107. }
  108. if (!was[f])
  109. {
  110. q.push(f);
  111. was[f] = 1;
  112. d[f] = d[temp] + 1;
  113. //PrintPytnaska(f, n);
  114. }
  115.  
  116. }
  117.  
  118. }
  119. cout << "Can't be solved" << endl;
  120. }
  121. signed main() {
  122. int n = 3;
  123. string s;
  124. cin >> s;
  125. vector<vector<char>> m(n, vector<char>(n));
  126. for (int i = 0; i < n; i++)
  127. for (int j = 0; j < n; j++)
  128. cin >> m[i][j];
  129. clock_t start = clock();
  130. bfs(n, m, s);
  131. clock_t end = clock();
  132. double seconds = (double)(end - start) / CLOCKS_PER_SEC;
  133. cout << "time: " << seconds << endl << endl;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement