Advertisement
alsiva

ForMaxCivilization

May 23rd, 2022
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <queue>
  5. using namespace std;
  6.  
  7.  
  8. void dijsktra(int xst, int yst, vector<vector<int>> &map,vector<vector<int>> &weights, vector<vector<bool>> &visited) {
  9.  
  10. weights[xst][yst] = 0;
  11. priority_queue<pair<int, pair<int, int>>> dots;
  12. dots.push(make_pair(0, make_pair(xst, yst)));
  13.  
  14. while (!dots.empty()) {
  15. int x = dots.top().second.first;
  16. int y = dots.top().second.second;
  17. dots.pop();
  18.  
  19. if (visited[x][y]) {
  20. continue;
  21. }
  22.  
  23. visited[x][y] = true;
  24.  
  25. if (x > 0 && !visited[x-1][y] && map[x-1][y] != -1) {
  26. if (weights[x][y] + map[x-1][y] < weights[x-1][y]) {
  27. weights[x-1][y] = weights[x][y] + map[x-1][y];
  28. }
  29. dots.push(make_pair(-weights[x-1][y], make_pair(x-1, y)));
  30. }
  31.  
  32. if (x < map.size() - 1 && !visited[x+1][y] && map[x+1][y] != -1) {
  33. if (weights[x][y] + map[x+1][y] < weights[x+1][y]) {
  34. weights[x+1][y] = weights[x][y] + map[x+1][y];
  35. }
  36. dots.push(make_pair(-weights[x+1][y], make_pair(x+1, y)));
  37. }
  38.  
  39. if(y > 0 && !visited[x][y-1] && map[x][y-1] != - 1) {
  40. if (weights[x][y] + map[x][y-1] < weights[x][y-1]) {
  41. weights[x][y-1] = weights[x][y] + map[x][y-1];
  42. }
  43. dots.push(make_pair(-weights[x][y-1], make_pair(x, y-1)));
  44. }
  45.  
  46. if (y < map[0].size() - 1 && !visited[x][y+1] && map[x][y+1] != -1) {
  47. if (weights[x][y] + map[x][y+1] < weights[x][y+1]) {
  48. weights[x][y+1] = weights[x][y] + map[x][y+1];
  49. }
  50. dots.push(make_pair(-weights[x][y+1], make_pair(x, y+1)));
  51. }
  52. }
  53. }
  54.  
  55. void printDirection(int x, int y, vector<vector<int>> &weights) {
  56.  
  57. int minWeight;
  58. if (weights[x][y] == 0) {
  59. return;
  60. } else {
  61. minWeight = weights[x][y];
  62. }
  63.  
  64. char dir;
  65. if (x > 0 && minWeight > weights[x-1][y]) {
  66. minWeight = weights[x-1][y];
  67. dir = 'S';
  68. }
  69. if (x < weights.size() - 1 && minWeight > weights[x+1][y]) {
  70. minWeight = weights[x+1][y];
  71. dir = 'N';
  72. }
  73. if (y > 0 && minWeight > weights[x][y-1]) {
  74. minWeight = weights[x][y-1];
  75. dir = 'E';
  76. }
  77. if (y < weights[0].size() - 1 && minWeight > weights[x][y+1]) {
  78. minWeight = weights[x][y+1];
  79. dir = 'W';
  80. }
  81.  
  82. if (dir == 'S') {
  83. printDirection(x-1, y, weights);
  84. } else if (dir == 'N') {
  85. printDirection(x+1, y, weights);
  86. } else if (dir == 'E') {
  87. printDirection(x, y-1, weights);
  88. } else if (dir == 'W') {
  89. printDirection(x, y+1, weights);
  90. }
  91.  
  92. cout << dir;
  93. }
  94.  
  95. int main() {
  96. ifstream file("input.txt");
  97.  
  98. int n, m, xbeg, ybeg, xfin, yfin;
  99. file >> n >> m >> xbeg >> ybeg >> xfin >> yfin;
  100.  
  101. vector<vector<int>> map(n, vector<int>(m, -1));
  102. vector<vector<int>> weights(n, vector<int>(m, INT32_MAX));
  103. vector<vector<bool>> visited(n, vector<bool>(m, false));
  104.  
  105.  
  106. char s;
  107. for (int i = 0; i < n; i++) {
  108. for (int j = 0; j < m; j++) {
  109. file >> s;
  110. if (s == '.') {
  111. map[i][j] = 1;
  112. } else if (s == 'W') {
  113. map[i][j] = 2;
  114. } else if (s == '#') {
  115. map[i][j] = -1;
  116. }
  117. }
  118. }
  119.  
  120. dijsktra(xbeg - 1, ybeg - 1, map, weights, visited);
  121.  
  122. if (weights[xfin-1][yfin-1] == INT32_MAX) {
  123. cout << -1;
  124. } else {
  125. cout << weights[xfin-1][yfin-1] << endl;
  126. printDirection(xfin-1, yfin-1, weights);
  127. }
  128.  
  129. file.close();
  130. return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement