Advertisement
Guest User

Labirinth

a guest
Feb 17th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #include <fstream>
  2. #include <queue>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. fstream fin("maze.in");
  10. ofstream fout("maze.out");
  11. short int M, N;
  12. fin >> M >> N;
  13. pair<short int, short int> start, end;
  14. fin >> start.first >> start.second >> end.first >> end.second;
  15. queue<pair<short int, short int>> Maze;
  16. bool** arr = new bool*[M + 2];
  17. for (size_t i = 0; i < M + 2; i++)
  18. {
  19. arr[i] = new bool[N + 2];
  20. }
  21.  
  22. for (size_t i = 0; i < M + 2; i++)
  23. {
  24. for (size_t j = 0; j < N + 2; j++)
  25. {
  26. if (i == 0 || j == 0 || i == M + 1 || j == N + 1)
  27. arr[i][j] = 0;
  28. else
  29. fin >> arr[i][j];
  30. }
  31. }
  32. Maze.push(start);
  33. /*vector<vector<char>> p;
  34. p.assign(M + 2, vector<char>(N + 2));*/
  35. char** p = new char*[M + 2];
  36. for (size_t i = 0; i < M + 2; i++)
  37. {
  38. p[i] = new char[N + 2];
  39. }
  40. p[start.first][start.second] = -1;
  41. while (!Maze.empty())
  42. {
  43. pair<short int, short int> tmp = Maze.front();
  44. short int i = tmp.first, j = tmp.second;
  45. if (arr[i + 1][j] == 1)
  46. {
  47. Maze.push(pair<short int, short int>(i + 1, j));
  48. p[i + 1][j] = '1';
  49. }
  50. if (arr[i - 1][j] == 1)
  51. {
  52. Maze.push(pair<short int, short int>(i - 1, j));
  53. p[i - 1][j] = '2';
  54. }
  55. if (arr[i][j + 1] == 1)
  56. {
  57. Maze.push(pair<short int, short int>(i, j + 1));
  58.  
  59. p[i][j + 1] = '3';
  60. }
  61. if (arr[i][j - 1] == 1)
  62. {
  63. Maze.push(pair<short int, short int>(i, j - 1));
  64. p[i][j - 1] = '4';
  65. }
  66. arr[i][j] = 0;
  67. Maze.pop();
  68. }
  69. if (arr[end.first][end.second] != 0)
  70. fout << -1 << endl;
  71. else
  72. {
  73. vector<pair<short int, short int>> path;
  74. int count = 0;
  75. char tmp = p[end.first][end.second];
  76. for (pair<short int, short int> i = end;;)
  77. {
  78. //path.push_back(i);
  79. p[i.first][i.second] = tmp;
  80. if (i == start)
  81. break;
  82. if (p[i.first][i.second] == '1')
  83. {
  84. i = pair<short int, short int>(i.first - 1, i.second);
  85. tmp = 2;
  86. }
  87. else if (p[i.first][i.second] == '2')
  88. {
  89. i = pair<short int, short int>(i.first + 1, i.second);
  90. tmp = '1';
  91. }
  92. else if (p[i.first][i.second] == '3')
  93. {
  94. i = pair<short int, short int>(i.first, i.second - 1);
  95. tmp = '4';
  96. }
  97. else if (p[i.first][i.second] == '4')
  98. {
  99. i = pair<short int, short int>(i.first, i.second + 1);
  100. tmp = '3';
  101. }
  102. ++count;
  103. }
  104. for (pair<short int, short int> i = start;;)
  105. {
  106. //path.push_back(i);
  107. fout<<i.first<<" "<<i.second<<endl;
  108. if (i == end)
  109. break;
  110. if (p[i.first][i.second] == '1')
  111. i = pair<short int, short int>(i.first - 1, i.second);
  112. else if (p[i.first][i.second] == '2')
  113. i = pair<short int, short int>(i.first + 1, i.second);
  114. else if (p[i.first][i.second] == '3')
  115. i = pair<short int, short int>(i.first, i.second - 1);
  116. else if (p[i.first][i.second] == '4')
  117. i = pair<short int, short int>(i.first, i.second + 1);
  118.  
  119. }
  120. reverse(path.begin(), path.end());
  121. fout << count << endl;
  122. for (size_t i = 0; i < path.size(); i++)
  123. {
  124. fout << path[i].first << " " << path[i].second << endl;
  125. }
  126. }
  127.  
  128.  
  129. for (size_t i = 0; i < M + 2; i++)
  130. {
  131. delete[] arr[i];
  132. delete[] p[i];
  133. }
  134. delete[] p;
  135. delete[] arr;
  136. return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement