Advertisement
maxim_shlyahtin

A*

May 2nd, 2024
746
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <queue>
  5. #include <cmath>
  6. #include <climits>
  7.  
  8. using namespace std;
  9.  
  10. // Структура для хранения информации о вершине
  11. struct Vertex {
  12.     double g = numeric_limits<double>::max(); // Стоимость пути от начальной вершины до данной
  13.     double h; // Эвристическая оценка стоимости пути от данной вершины до конечной
  14.     double f; // Общая стоимость пути (g + h)
  15.     char parent; // Родительская вершина в пути
  16. };
  17.  
  18. // Структура для сравнения вершин в очереди
  19. struct Compare {
  20.     bool operator()(const pair<char, Vertex>& a, const pair<char, Vertex>& b) {
  21.         return a.second.f > b.second.f;
  22.     }
  23. };
  24.  
  25. // Функция для вычисления эвристической оценки
  26. double heuristic(char a, char b) {
  27.     return abs(a - b);
  28. }
  29.  
  30. string aStar(map<char, vector<pair<char, double>>>& graph, char start, char goal) {
  31.     map<char, Vertex> vertices;
  32.     priority_queue<pair<char, Vertex>, vector<pair<char, Vertex>>, Compare> pq;
  33.  
  34.     // Инициализация начальной вершины
  35.     vertices[start].g = 0;
  36.     vertices[start].h = heuristic(start, goal);
  37.     vertices[start].f = vertices[start].g + vertices[start].h;
  38.     vertices[start].parent = ' ';
  39.     pq.push({ start, vertices[start] });
  40.  
  41.     while (!pq.empty()) {
  42.         char current = pq.top().first;
  43.         pq.pop();
  44.  
  45.         if (current == goal) {
  46.             string path;
  47.             while (current != ' ') {
  48.                 path = current + path;
  49.                 current = vertices[current].parent;
  50.             }
  51.             return path;
  52.         }
  53.  
  54.         for (auto& neighbor : graph[current]) {
  55.             char next = neighbor.first;
  56.             double tentative_g = vertices[current].g + neighbor.second;
  57.  
  58.             //cout << next << ' ' << tentative_g << ' ' << vertices[next].g << '\n';
  59.  
  60.             if (tentative_g < vertices[next].g) {
  61.                 vertices[next].g = tentative_g;
  62.                 vertices[next].h = heuristic(next, goal);
  63.                 vertices[next].f = vertices[next].g + vertices[next].h;
  64.                 vertices[next].parent = current;
  65.                 pq.push({ next, vertices[next] });
  66.             }
  67.         }
  68.  
  69.         //cout << current << '\n';
  70.     }
  71.  
  72.     return "No path found";
  73. }
  74.  
  75. int main() {
  76.     map<char, vector<pair<char, double>>> graph;
  77.     char start, goal;
  78.     cin >> start >> goal;
  79.  
  80.     char from, to;
  81.     double weight;
  82.     while (cin >> from >> to >> weight) {
  83.         graph[from].insert(graph[from].end(), {make_pair(to, weight)});
  84.     }
  85.  
  86.     /*for (const auto& vert : graph) {
  87.         std::cout << "vert = " << vert.first << '\n';
  88.         for (const auto& pair : vert.second) {
  89.             std::cout << " end vert = " << pair.first << " weight = " << pair.second;
  90.         }
  91.         std::cout << "\n";
  92.     }*/
  93.  
  94.     string path = aStar(graph, start, goal);
  95.     cout << path << endl;
  96.  
  97.     return 0;
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement