Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- using namespace std;
- // точка пути
- struct Waypoint
- {
- int leftPoint;
- int rightPoint;
- Waypoint() {}
- };
- int main()
- {
- setlocale(0, "rus");
- const int STATUS_VISITED = 5; // вершина была посещена
- const int STATUS_EXIST = 1; // из вершины есть путь
- int nodes[4]; // вершины графа
- stack<int> nodeStack; // стек с вершинами
- Waypoint point; // временная точка пути
- stack<Waypoint> WaypointStack; // стек точек пути
- // матрица смежности
- int pathMatrix[4][4] = {
- 0, 0, 1, 1,
- 0, 0, 1, 0,
- 1, 1, 0, 1,
- 1, 0, 1, 0,
- };
- for (int i = 0; i < 4; i++) // исходно все вершины равны 0
- nodes[i] = 0;
- int beginpoint; // начальная точка пути
- int endpoint; // конечная точка пути
- cout << "Введите начальную и конечную точку пути: ";
- cin >> beginpoint >> endpoint;
- // уменьшаем индексы на 1
- beginpoint--;
- endpoint--;
- nodeStack.push(beginpoint);
- // пока есть непосещённые вершины
- while (!nodeStack.empty())
- {
- auto temp = nodeStack.top();
- nodeStack.pop();
- if (nodes[temp] == STATUS_VISITED)
- {
- continue;
- }
- else
- {
- nodes[temp] = STATUS_VISITED;
- }
- for (int j = 0; j <= 3; j++)
- {
- if (pathMatrix[temp][j] == STATUS_EXIST && nodes[j] != STATUS_VISITED)
- {
- nodeStack.push(j);
- nodes[j] = STATUS_EXIST;
- point.leftPoint = temp;
- point.rightPoint = j;
- WaypointStack.push(point);
- if (temp == endpoint)
- {
- break;
- }
- }
- }
- }
- cout << "ПУТЬ ОТ " << beginpoint + 1 << " ДО " << endpoint + 1;
- cout << endl;
- cout << endpoint + 1;
- while (!WaypointStack.empty())
- {
- point = WaypointStack.top();
- WaypointStack.pop();
- if (point.rightPoint == endpoint)
- {
- endpoint = point.leftPoint;
- cout << " __/ ";
- cout << endpoint + 1;
- }
- }
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment