Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <set>
- using namespace std;
- #define n 6
- int G[6][6]{
- {0, 1, 1, 0, 0, 0},
- {1, 0, 1, 1, 0, 0},
- {1, 1, 0, 1, 1, 0},
- {0, 1, 1, 0, 1, 1},
- {0, 0, 1, 1, 0, 1},
- {0, 0, 0, 1, 1, 0}
- };
- void Lee(int S, int F) {
- S--;
- F--;
- int length, wawe[n];
- set<int> way;
- set<int> way1;
- set<int> way2;
- vector<int> NewFront, OldFront;
- for (int i = 0; i < n; i++)
- wawe[i] = -1;
- int mark=1;
- wawe[S] = mark;
- cout << "Wawe: " << endl;
- for (int k = 0; k < n; k++) {
- cout << wawe[k] << " ";
- }
- cout << endl;
- OldFront.push_back(S);
- cout << "NewFront: {} " << endl;
- cout << "OldFront: " <<S+1<< endl;
- way.insert(way.begin(), S);
- way1.insert(way1.begin(), S);
- way2.insert(way2.begin(), S);
- length = 0;
- cout << "Length " << length<<endl<<endl;
- while (true) {
- mark++;
- for (int i = 0; i < OldFront.size(); i++) {
- for (int j = 0; j < n; j++) {
- if (G[OldFront[i]][j] == 1&&wawe[j]) {
- if (wawe[j] == -1) {
- if (j == F) {
- wawe[j]=mark;
- NewFront.push_back(j);
- way.insert(way.end(), j);
- way1.insert(way1.end(), j);
- way2.insert(way2.end(), j);
- break;
- }
- wawe[j]=mark;
- }
- NewFront.push_back(j);
- if (NewFront.size()==1) way.insert(way.end(), NewFront[0]);
- if(NewFront.size() == 2) way1.insert(way1.end(), NewFront[1]);
- if (NewFront.size() == 2&&length!=1) way2.insert(way2.end(), NewFront[1]);
- if (NewFront.size() == 2 && length == 1) way2.insert(way2.end(), NewFront[0]);
- }
- }
- }
- cout << "Wawe: " << endl;
- for (int k = 0;k<n; k++) {
- cout <<wawe[k]<<" ";
- }
- cout << endl;
- cout << "NewFront: " << endl;
- for (int k = 0; k < NewFront.size(); k++) {
- cout << NewFront[k]+1 << " ";
- }
- cout << endl;
- cout << "OldFront: " << endl;
- for (int k = 0; k < OldFront.size(); k++) {
- cout << OldFront[k]+1 << " ";
- }
- cout << endl;
- cout << "length " << length+1 << endl;
- cout << endl;
- if (NewFront.empty()) {
- cout << "Нет пути";
- break;
- }
- for (int m = 0; m < NewFront.size(); m++) {
- if (NewFront[m] == F) {
- cout <<endl<< "Путь найден" << endl;
- cout << endl;
- cout << "Путь 1";
- for (auto it:way) cout <<endl<< it+1<< " ";
- cout << endl;
- cout << "Длина 1 пути: " << length + 1;
- cout << endl;
- cout << "Путь 2";
- for (auto it1 : way1) cout << endl << it1 + 1 << " ";
- cout << endl;
- cout << "Длина 2 пути: " << length + 1;
- cout << endl;
- cout << "Путь 3";
- for (auto it2 : way2) cout << endl << it2 + 1 << " ";
- cout << endl;
- cout << "Длина 3 пути: " << length + 1 << endl;
- cout << "Путь: ";
- for (int d = F; d >= 0; d--) {
- for (int j = 0; j < n; j++) {
- if (G[d][j] && wawe[d] - wawe[d - 1] == 1) cout << d << " ";
- }
- }
- cout <<"Длина пути: "<<length+1;
- return;
- }
- }
- OldFront = NewFront;
- NewFront = {};
- length++;
- }
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- Lee(1, 6);
- }
Advertisement
Add Comment
Please, Sign In to add comment