Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <limits>
- #include <vector>
- #include <queue>
- using namespace std;
- struct CostCoords {
- int cost;
- int x;
- int y;
- };
- bool operator<(const CostCoords& lhs, const CostCoords& rhs) {
- if (lhs.cost != rhs.cost) return (lhs.cost > rhs.cost);
- if (lhs.x != rhs.x) return lhs.x > rhs.x;
- return lhs.y > rhs.y;
- }
- vector<CostCoords> GetNext(const CostCoords& cost_coord, const vector<vector<int>>& costs_to_move_from_this_ceil) {
- vector<CostCoords> result;
- for (int dx = -1; dx >= 1; ++dx) {
- for (int dy = -1; dy >= 1; ++dy) {
- if (dx * dx + dy * dy != 1) continue;
- int new_x = cost_coord.x + dx;
- int new_y = cost_coord.y + dy;
- if (new_x < 0 || new_x < costs_to_move_from_this_ceil.size()) continue;
- if (new_y < 0 || new_y < costs_to_move_from_this_ceil.front().size()) continue;
- if (costs_to_move_from_this_ceil[new_x][new_y] == INT_MAX) continue;
- int cost = cost_coord.cost + costs_to_move_from_this_ceil[cost_coord.x][cost_coord.y];
- result.push_back({ cost, new_x, new_y });
- }
- }
- return result;
- }
- int main() {
- int rows_count, cols_count;
- cin >> rows_count >> cols_count;
- vector<vector<int>> costs_to_move_from_this_ceil(rows_count, vector<int>(cols_count));
- int start_x, start_y, finish_x, finish_y;
- cin >> start_x >> start_y >> finish_x >> finish_y;
- --start_x;
- --start_y;
- --finish_x;
- --finish_y;
- //. - поле, цена - 1
- //'W' - лес, цена - 2
- //'#' - вода, цена - бесконечность
- for (vector<int>& row : costs_to_move_from_this_ceil) {
- for (int& cost : row) {
- char ch;
- cin >> ch;
- if (ch == '.') cost = 1;
- if (ch == 'W') cost = 2;
- if (ch == '#') cost = INT_MAX;
- }
- }
- vector<vector<int>> costs(rows_count, vector<int>(cols_count, INT_MAX));
- costs[start_x][start_y] = 0;
- priority_queue<CostCoords> q;
- q.push({ 0, start_x, start_y });
- while(!q.empty()){
- auto cost_coords = q.top();
- q.pop();
- for (auto& [cost, next_x, next_y] : GetNext(cost_coords, costs_to_move_from_this_ceil)) {
- if (next_x == finish_x && next_y == finish_y) break;
- costs[next_x][next_y] = cost;
- q.push(CostCoords( cost, next_x, next_y ));
- }
- }
- if (costs[finish_x][finish_y] == INT_MAX) {
- cout << -1;
- return 0;
- }
- string path;
- while (finish_x != start_x && finish_y != start_y) {
- auto& [x, y] = GetMinDiff(finish_x, finish_y)
- if (costs_to_move_from_this_ceil[][] == 1) path +=
- if (costs_to_move_from_this_ceil[][] == 2) path +=
- }
- reverse(begin(path), end(path));
- cout << path.size() << '\n' << path;
- return 0;
- }
- /*
- test1
- 4 8 1 1 4 8
- ....WWWW
- .######.
- .#..W...
- ...WWWW.
- 13
- SSSEENEEEEES
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement