Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- using namespace std;
- struct Edge
- {
- int begin;
- int end;
- };
- int mas[10][10] =
- {
- { 0, 1, 1, 0, 0, 0, 0, 0, 0, 1 },
- { 0, 0, 0, 0, 0, 1, 1, 0, 1, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 1, 0, 1 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
- { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 1, 0, 0, 1, 0, 0, 0 },
- };
- int nodes[10];
- stack <int> Stack;
- stack <Edge> Edges;
- Edge e;
- void VGLrec(int Start, int End)
- {
- int r;
- cout << Start + 1 << " ";
- nodes[Start] = 1;
- for (r = 0; r < End; r++)
- if ((mas[Start][r] != 0) && (nodes[r] == 0))
- VGLrec(r, End);
- }
- void VGL(int Start, int End)
- {
- int n=0;
- Stack.push(Start);
- cout << "Путь обхода:" << endl;
- while (!Stack.empty())
- { //пока стек не пуст
- int node = Stack.top(); // извлекаем вершину
- Stack.pop();
- if (nodes[node] == 2) continue;
- nodes[node] = 2; // отмечаем ее как посещенную
- for (int j = 6; j >= 0; j--)
- { //проверяем для нее все смежные варианты
- if (mas[node][j] == 1 && nodes[j] != 2)
- { // если вершина смежная и не обнаружена
- Stack.push(j); // добавляем ее в стек
- nodes[j] = 1; // отмечаем вершину как обнаруженную
- e.begin = node; e.end = j;
- Edges.push(e);
- if (node == End) break;
- }
- }
- cout << node + 1 << " ";
- }
- cout << endl;
- cout << "Путь до вершины " << End + 1 << endl;
- cout << End + 1;
- while (!Edges.empty())
- {
- e = Edges.top();
- Edges.pop();
- if (e.end == End)
- {
- End = e.begin;
- cout << " <- " << End + 1;
- n++;
- }
- }
- cout << endl;
- cout << "Количество шагов: " << n << endl;
- }
- void VSH(int Start, int End)
- {
- Stack.push(Start);
- int n=0;
- cout << "Путь обхода:" << endl;
- while (!Stack.empty())
- {
- int node = Stack.top(); // извлекаем вершину
- Stack.pop();
- nodes[node] = 2; // отмечаем ее как посещенную
- for (int j = 0; j < 7; j++)
- {
- if (mas[node][j] == 1 && nodes[j] == 0)
- { // если вершина смежная и не обнаружена
- Stack.push(j); // добавляем ее в стек
- nodes[j] = 1; // отмечаем вершину как обнаруженную
- e.begin = node; e.end = j;
- Edges.push(e);
- if (node == End) break;
- n++;
- }
- }
- cout << node + 1 << " ";
- }
- cout << endl;
- cout << "Путь до вершины " << End + 1 << endl;
- cout << End + 1;
- while (!Edges.empty())
- {
- e = Edges.top();
- Edges.pop();
- if (e.end == End)
- {
- End = e.begin;
- cout << " <- " << End + 1;
- }
- }
- cout << endl;
- cout << "Количество шагов: " << n << endl;
- }
- int main()
- {
- setlocale(LC_ALL, "rus");
- for (int i = 0; i < 7; i++) // исходно все вершины равны 0
- nodes[i] = 0;
- int F;
- int Start, End;
- cout << "Введите начальную вершину: ";
- cin >> Start; Start--;
- cout << "Введите целевую вершину: ";
- cin >> End; End--;
- cout << "МЕНЮ:\n";
- cout << "1) В ширину\n";
- cout << "2) В глубиину\n";
- cout << "3) В глубину с рекурсией\n";
- cout << "4.Выход\n\n";
- cin >> F;
- cout << endl;
- switch (F)
- {
- case 1:
- VSH(Start, End);
- break;
- case 2:
- VGL(Start, End);
- break;
- case 3:
- cout << "Путь обхода:" << endl;
- VGLrec(Start, End);
- cout << End + 1 << endl;
- break;
- case 4:
- exit(0);
- break;
- default:
- cout << "Неверный пункт меню!\n";
- return F;
- break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement