1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| using Point = pair<int, int>; #define x first #define y second
vector<Point> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; void solve() { int n, m; cin >> n >> m; Point start, end; vector<vector<char>> vec(n, vector<char>(m)); vector<vector<bool>> vis(n, vector<bool>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> vec[i][j]; if (vec[i][j] == 'S') start = {i, j}; if (vec[i][j] == 'E') end = {i, j}; } } priority_queue<pair<int, Point>, vector<pair<int, Point>>, greater<>> que; int ans = INT_MAX; que.push({0, start}); vis[start.x][start.y] = true; while (!que.empty()) { auto [step, cur] = que.top(); que.pop(); if (cur == end) { ans = min(ans, step); continue; } for (auto dir : directions) { Point nxt = {dir.x + cur.x, dir.y + cur.y}; if (nxt.x >= 0 && nxt.x < n && nxt.y >= 0 && nxt.y < m && !vis[nxt.x][nxt.y] && vec[nxt.x][nxt.y] != '#') que.push({vec[nxt.x][nxt.y] == '@' ? step + 3 : step + 1, nxt}), vis[nxt.x][nxt.y] = true; } } if (ans == INT_MAX) { cout << "The End!!!" << endl; return; } cout << ans << endl; }
|