Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <climits>
  5. #include <queue>
  6. #include <cmath>
  7. #define N 26
  8. using namespace std;
  9. typedef struct Priority{
  10. vector<int> resultat;
  11. double prior;
  12. int var_evristic;
  13. } Priority;
  14.  
  15. bool operator < (const Priority &com_ver_1, const Priority &com_ver_2){
  16. return com_ver_1.prior > com_ver_2.prior;
  17. }
  18.  
  19. class Graf{
  20. private:
  21. double **graf;
  22. vector<int> str;
  23. double weight_from[N];
  24. double finish_way;
  25. public:
  26. Graf(int start){
  27. str.push_back(start);
  28. graf = new double *[N];
  29. for(int i = 0; i < N; i++)
  30. graf[i] = new double[N];
  31. for(int i = 0; i < N; i++){
  32. for(int j = 0; j < N; j++)
  33. graf[i][j] = 0;
  34. }
  35. }
  36. ~Graf(){
  37. for (int i = 0; i < N; i++)
  38. delete[] graf[i];
  39. delete[] graf;
  40. str.clear();
  41. }
  42. void matrix(char i, char j, double weight){
  43. graf[i][j] = weight;
  44. }
  45. void Display(){
  46. for(int i = 0; i < N; i++){
  47. for(int j = 0; j < N; j++){
  48. cout << graf[i][j] << " ";
  49. }
  50. cout << endl;
  51. }
  52. }
  53. void print_path(int first, int finish){
  54. bool admissible = 1;
  55. bool monotonic = 1;
  56. int i = 0;
  57. while(str[i] != finish){
  58. admissible = is_admissible(finish, str[i]) && admissible ? 1 : 0;
  59. i++;
  60. monotonic = is_monotonic(finish, str[i-1], str[i]) && monotonic ? 1 : 0;
  61. }
  62. for(i = 0; i < str.size() ; i++)
  63. cout << char(str[i] + 'a');
  64. if(admissible)
  65. cout<<endl<<"Эвристика допустима"<<endl;
  66. else
  67. cout<<endl<<"Эвристика не допустима"<<endl;
  68. if(monotonic)
  69. cout<<"Эвристика монотонна"<<endl;
  70. else
  71. cout<<"Эвристика не монотонна"<<endl;
  72. }
  73. double evristic(int i, int j){
  74. return abs(i - j);
  75. }
  76. bool is_admissible( int finish, int ver){
  77. return finish_way - weight_from[ver] >= evristic(finish, ver);
  78. }
  79. bool is_monotonic(int finish, int ver1, int ver2){
  80. return graf[ver1][ver2] >= evristic(finish, ver1) - evristic(finish, ver2);
  81. }
  82. void min_way_A_star(int first, int finish, priority_queue <Priority> &queue, double way){
  83. vector<int> str1;
  84. weight_from[first] = 0;
  85. int first_prior = evristic(finish, first);
  86. while(true){
  87. for(int j = 0; j < N; j++)
  88. if(graf[first][j] != 0){
  89. Priority elem;
  90. elem.var_evristic = evristic(finish, j);
  91. elem.prior = graf[first][j] + way + elem.var_evristic;
  92. for(auto i: str1)
  93. elem.resultat.push_back(i);
  94. elem.resultat.push_back(j);
  95. queue.push(elem);
  96. }
  97. if(queue.empty())
  98. break;
  99. if(!queue.empty()){
  100. Priority popped;
  101. popped = queue.top();
  102. queue.pop();
  103. first = popped.resultat[popped.resultat.size()-1];
  104. str1 = popped.resultat;
  105. way = popped.prior - popped.var_evristic;
  106. first_prior = popped.var_evristic + first_prior;
  107. weight_from[first] = way + first_prior;
  108. }
  109. if(str1[str1.size() - 1] == finish){
  110. if(str.size()){
  111. for(auto i: str1)
  112. str.push_back(i);
  113. finish_way = weight_from[first] ;
  114. print_path(str[0], str[str.size() - 1]);
  115. break;
  116. } else cout << "нет пути";
  117. }
  118. }
  119. }
  120. };
  121.  
  122. int main() {
  123. char start, the_end, from, to;
  124. double weight;
  125. cin>> start >> the_end;
  126. priority_queue <Priority> queue;
  127. Graf matr(start - 'a');
  128. while(cin >> from >> to >> weight){
  129. matr.matrix(from - 'a', to - 'a', weight);
  130. }
  131. // matr.Display();
  132. matr.min_way_A_star(start - 'a', the_end - 'a', queue, 0);
  133. return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement