Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <map>
- #include <string>
- #include <queue>
- using namespace std;
- int main() {
- int n, m;
- pair <int, int> s, f;
- cin >> n >> m;
- //map<pair<int, int>, vector<char>> g;
- vector <vector<char>> graph(n);
- vector <pair<pair<int, int>, string>> step;
- step.push_back(make_pair(make_pair(-1, 0), "N"));
- step.push_back(make_pair(make_pair(-1, -1), "NW"));
- step.push_back(make_pair(make_pair(-1, 1), "NE"));
- step.push_back(make_pair(make_pair(0, -1), "W"));
- step.push_back(make_pair(make_pair(0, 1), "E"));
- step.push_back(make_pair(make_pair(1, 0), "S"));
- step.push_back(make_pair(make_pair(1, -1), "SW"));
- step.push_back(make_pair(make_pair(1, 1), "SE"));
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++) {
- char x;
- cin >> x;
- if (x == 'S') {
- s = make_pair(i, j);
- graph[i].push_back('S');
- }
- if (x == 'F') {
- f = make_pair(i, j);
- graph[i].push_back('F');
- }
- if (x == '.')
- graph[i].push_back('.');
- if (x == '#')
- graph[i].push_back('#');
- }
- queue<pair<int, int>> q;
- map<pair<int, int>, bool> used;
- map<pair<int, int>, pair<pair<int, int>, string>> p;
- map<pair<int, int>, string> compas;
- map<pair<int, int>, int> d;
- used[s] = true;
- used[f] = false;
- p[s] = make_pair(make_pair(-1, -1), "NULL");
- d[s] = 0;
- q.push(s);
- while (!q.empty()) {
- pair<int, int> v = q.front();
- q.pop();
- for (int i = 0; i < step.size(); i++) {
- pair<int, int> pair;
- pair = make_pair(v.first + step[i].first.first, v.second + step[i].first.second);
- if (pair.first >= 0
- && pair.first < n
- && pair.second >= 0
- && pair.second < m
- && graph[pair.first][pair.second] != '#'
- && !used[pair])
- {
- q.push(pair);
- used[pair] = true;
- d[pair] = d[v] + 1;
- p[pair].first = v;
- p[pair].second = step[i].second;
- }
- }
- }
- if (!used[f])
- cout << -1;
- else {
- cout << d[f] << endl;
- vector <string> path;
- for (pair <int, int> v = f; v.first != -1 && v.second!= -1; v = p[v].first) { // если v != s - работает
- path.push_back(p[v].second);
- }
- reverse(path.begin(), path.end());
- for (int i = 0; i < path.size(); i++)
- cout << path[i] << endl;
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement