diff --git a/src/main.cc b/src/main.cc index 53f6f19..ab92e6c 100644 --- a/src/main.cc +++ b/src/main.cc @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -59,6 +60,7 @@ class Bot { asio::awaitable Protocol(); asio::awaitable Join(); asio::awaitable Move(Direction dir); + unsigned AStarHeuristic(std::pair position); unsigned ShortestPath(int x, int y); asio::awaitable ChooseMove(); @@ -177,8 +179,12 @@ asio::awaitable Bot::Move(Direction direction) { co_return true; } +unsigned Bot::AStarHeuristic(std::pair position) { + return std::abs(position.first - goal_x) + std::abs(position.second - goal_y); +} + unsigned Bot::ShortestPath(int x, int y) { - // Djikstra + // A* std::set> visited; std::multimap> queue; auto goal = std::make_pair(goal_x, goal_y); @@ -197,23 +203,23 @@ unsigned Bot::ShortestPath(int x, int y) { } if (!(known_map[position] & static_cast(Direction::LEFT)) && position.first > 0) { - queue.emplace(distance + 1, - std::make_pair(position.first - 1, position.second)); + auto pos = std::make_pair(position.first - 1, position.second); + queue.emplace(distance + 1 + AStarHeuristic(pos), pos); } if (!(known_map[position] & static_cast(Direction::RIGHT)) && position.first < field_width) { - queue.emplace(distance + 1, - std::make_pair(position.first + 1, position.second)); + auto pos = std::make_pair(position.first + 1, position.second); + queue.emplace(distance + 1 + AStarHeuristic(pos), pos); } if (!(known_map[position] & static_cast(Direction::UP)) && position.second > 0) { - queue.emplace(distance + 1, - std::make_pair(position.first, position.second - 1)); + auto pos = std::make_pair(position.first, position.second - 1); + queue.emplace(distance + 1 + AStarHeuristic(pos), pos); } if (!(known_map[position] & static_cast(Direction::DOWN)) && position.second < field_height) { - queue.emplace(distance + 1, - std::make_pair(position.first, position.second + 1)); + auto pos = std::make_pair(position.first, position.second + 1); + queue.emplace(distance + 1 + AStarHeuristic(pos), pos); } visited.emplace(position); }