Maxim_Leo

Untitled

May 19th, 2022
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <vector>
  4. #include <stack>
  5. using namespace std;
  6.  
  7. #define n 6
  8.  
  9. int G[6][6]{
  10. {0, 1, 1, 0, 0, 0},
  11. {1, 0, 1, 1, 0, 0},
  12. {1, 1, 0, 1, 1, 0},
  13. {0, 1, 1, 0, 1, 1},
  14. {0, 0, 1, 1, 0, 1},
  15. {0, 0, 0, 1, 1, 0}
  16. };
  17.  
  18. void Lee(int S, int F) {
  19. S--;
  20. F--;
  21. int length, wawe[n];
  22. stack<int> way;
  23. vector<int> NewFront, OldFront;
  24. for (int i = 0; i < n; i++)
  25. wawe[i] = -1;
  26.  
  27. wawe[S] = 0;
  28. cout << "Wawe: " << endl;
  29. for (int k = 0; k < n; k++) {
  30. cout << wawe[k] << " ";
  31. }
  32. cout << endl;
  33. OldFront.push_back(S);
  34. cout << "NewFront: {} " << endl;
  35. cout << "OldFront: " << S + 1 << endl;
  36. length = 0;
  37. cout << "Length " << length << endl << endl;
  38. while (true) {
  39. for (int i = 0; i < OldFront.size(); i++) {
  40. for (int j = 0; j < n; j++) {
  41. if (G[OldFront[i]][j] == 1 && wawe[j]) {
  42. if (wawe[j] == -1) {
  43. if (j == F) {
  44. wawe[j] = length + 1;
  45. NewFront.push_back(j);
  46. break;
  47. }
  48.  
  49. wawe[j] = length + 1;
  50. }
  51.  
  52. NewFront.push_back(j);
  53. }
  54. }
  55. }
  56. cout << "Wawe: " << endl;
  57. for (int k = 0; k < n; k++) {
  58. cout << wawe[k] << " ";
  59. }
  60. cout << endl;
  61. cout << "NewFront: " << endl;
  62. for (int k = 0; k < NewFront.size(); k++) {
  63. cout << NewFront[k] + 1 << " ";
  64. }
  65. cout << endl;
  66. cout << "OldFront: " << endl;
  67. for (int k = 0; k < OldFront.size(); k++) {
  68. cout << OldFront[k] + 1 << " ";
  69. }
  70. cout << endl;
  71. cout << "length " << length + 1 << endl;
  72. cout << endl;
  73. if (NewFront.empty()) {
  74. cout << "Нет пути";
  75. break;
  76. }
  77. for (int m = 0; m < NewFront.size(); m++) {
  78. if (NewFront[m] == F) {
  79. // cout << F + 1 << " ";
  80. way.push(F+1);
  81. for (int d = F; d != S; ) {
  82.  
  83. for (int j = 0; j < n; j++) {
  84. if (G[d][j] == 1 && wawe[d] - wawe[j] == 1) {
  85. d = j;
  86. way.push(d + 1);
  87. // cout << d + 1 << " ";
  88. break;
  89. }
  90. }
  91. }
  92. cout << endl << "Путь: "<<endl;
  93. for (int i = 0; i< way.size()+3; i++) {
  94. cout << way.top() << " ";
  95. way.pop();
  96. }
  97. cout << endl;
  98. cout << "Длина пути: " << length + 1;
  99. return;
  100. }
  101. }
  102. OldFront = NewFront;
  103. NewFront = {};
  104. length++;
  105. }
  106.  
  107. }
  108.  
  109. int main()
  110. {
  111. setlocale(LC_ALL, "Russian");
  112. Lee(1, 6);
  113.  
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment