Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <string>
  5. #include <queue>
  6. using namespace std;
  7.  
  8. int main() {
  9. int n, m;
  10. pair <int, int> s, f;
  11. cin >> n >> m;
  12. //map<pair<int, int>, vector<char>> g;
  13. vector <vector<char>> graph(n);
  14. vector <pair<pair<int, int>, string>> step;
  15. step.push_back(make_pair(make_pair(-1, 0), "N"));
  16. step.push_back(make_pair(make_pair(-1, -1), "NW"));
  17. step.push_back(make_pair(make_pair(-1, 1), "NE"));
  18. step.push_back(make_pair(make_pair(0, -1), "W"));
  19. step.push_back(make_pair(make_pair(0, 1), "E"));
  20. step.push_back(make_pair(make_pair(1, 0), "S"));
  21. step.push_back(make_pair(make_pair(1, -1), "SW"));
  22. step.push_back(make_pair(make_pair(1, 1), "SE"));
  23. for (int i = 0; i < n; i++)
  24. for (int j = 0; j < m; j++) {
  25. char x;
  26. cin >> x;
  27. if (x == 'S') {
  28. s = make_pair(i, j);
  29. graph[i].push_back('S');
  30. }
  31. if (x == 'F') {
  32. f = make_pair(i, j);
  33. graph[i].push_back('F');
  34. }
  35. if (x == '.')
  36. graph[i].push_back('.');
  37. if (x == '#')
  38. graph[i].push_back('#');
  39. }
  40. queue<pair<int, int>> q;
  41. map<pair<int, int>, bool> used;
  42.  
  43. map<pair<int, int>, pair<pair<int, int>, string>> p;
  44. map<pair<int, int>, string> compas;
  45.  
  46. map<pair<int, int>, int> d;
  47. used[s] = true;
  48. used[f] = false;
  49. p[s] = make_pair(make_pair(-1, -1), "NULL");
  50. d[s] = 0;
  51. q.push(s);
  52. while (!q.empty()) {
  53.  
  54. pair<int, int> v = q.front();
  55. q.pop();
  56.  
  57. for (int i = 0; i < step.size(); i++) {
  58. pair<int, int> pair;
  59. pair = make_pair(v.first + step[i].first.first, v.second + step[i].first.second);
  60. if (pair.first >= 0
  61. && pair.first < n
  62. && pair.second >= 0
  63. && pair.second < m
  64. && graph[pair.first][pair.second] != '#'
  65. && !used[pair])
  66. {
  67. q.push(pair);
  68. used[pair] = true;
  69. d[pair] = d[v] + 1;
  70. p[pair].first = v;
  71. p[pair].second = step[i].second;
  72. }
  73. }
  74. }
  75. if (!used[f])
  76. cout << -1;
  77. else {
  78. cout << d[f] << endl;
  79. vector <string> path;
  80. for (pair <int, int> v = f; v.first != -1 && v.second!= -1; v = p[v].first) { // если v != s - работает
  81. path.push_back(p[v].second);
  82. }
  83. reverse(path.begin(), path.end());
  84. for (int i = 0; i < path.size(); i++)
  85. cout << path[i] << endl;
  86. }
  87.  
  88. system("pause");
  89. return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement