bogdan_obukhovskii

РПР

Jun 13th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3.  
  4. using namespace std;
  5.  
  6. // точка пути
  7. struct Waypoint
  8. {
  9.     int leftPoint;
  10.     int rightPoint;
  11.  
  12.     Waypoint() {}
  13. };
  14.  
  15. int main()
  16. {
  17.     setlocale(0, "rus");
  18.  
  19.     const int STATUS_VISITED = 5; // вершина была посещена
  20.     const int STATUS_EXIST = 1;   // из вершины есть путь
  21.  
  22.     int nodes[4];         // вершины графа
  23.     stack<int> nodeStack; // стек с вершинами
  24.  
  25.     Waypoint point;                // временная точка пути
  26.     stack<Waypoint> WaypointStack; // стек точек пути
  27.  
  28.     // матрица смежности
  29.     int pathMatrix[4][4] = {
  30.         0, 0, 1, 1,
  31.         0, 0, 1, 0,
  32.         1, 1, 0, 1,
  33.         1, 0, 1, 0,
  34.     };
  35.  
  36.     for (int i = 0; i < 4; i++) // исходно все вершины равны 0
  37.         nodes[i] = 0;
  38.  
  39.     int beginpoint; // начальная точка пути
  40.     int endpoint;   // конечная точка пути
  41.  
  42.     cout << "Введите начальную и конечную точку пути: ";
  43.     cin >> beginpoint >> endpoint;
  44.  
  45.     // уменьшаем индексы на 1
  46.     beginpoint--;
  47.     endpoint--;
  48.  
  49.     nodeStack.push(beginpoint);
  50.  
  51.     // пока есть непосещённые вершины
  52.     while (!nodeStack.empty())
  53.     {
  54.         auto temp = nodeStack.top();
  55.         nodeStack.pop();
  56.  
  57.         if (nodes[temp] == STATUS_VISITED)
  58.         {
  59.             continue;
  60.         }
  61.         else
  62.         {
  63.             nodes[temp] = STATUS_VISITED;
  64.         }
  65.  
  66.         for (int j = 0; j <= 3; j++)
  67.         {
  68.             if (pathMatrix[temp][j] == STATUS_EXIST && nodes[j] != STATUS_VISITED)
  69.             {
  70.                 nodeStack.push(j);
  71.  
  72.                 nodes[j] = STATUS_EXIST;
  73.  
  74.                 point.leftPoint = temp;
  75.                 point.rightPoint = j;
  76.  
  77.                 WaypointStack.push(point);
  78.  
  79.                 if (temp == endpoint)
  80.                 {
  81.                     break;
  82.                 }
  83.             }
  84.         }
  85.     }
  86.  
  87.     cout << "ПУТЬ ОТ " << beginpoint + 1 << " ДО " << endpoint + 1;
  88.     cout << endl;
  89.  
  90.     cout << endpoint + 1;
  91.  
  92.     while (!WaypointStack.empty())
  93.     {
  94.         point = WaypointStack.top();
  95.         WaypointStack.pop();
  96.  
  97.         if (point.rightPoint == endpoint)
  98.         {
  99.             endpoint = point.leftPoint;
  100.  
  101.             cout << " __/ ";
  102.             cout << endpoint + 1;
  103.         }
  104.     }
  105.    
  106.     system("pause");
  107.     return 0;
  108. }
Add Comment
Please, Sign In to add comment