Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 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. double 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. // delete[] weight_from;
  41. str.clear();
  42. }
  43. void matrix(char i, char j, double weight){
  44. graf[i][j] = weight;
  45. }
  46. void Display(){
  47. for(int i = 0; i < N; i++){
  48. for(int j = 0; j < N; j++){
  49. cout << graf[i][j] << " ";
  50. }
  51. cout << endl;
  52. }
  53. }
  54. void print_path(int first, int finish){
  55. bool admissible = 1;
  56. bool monotonic = 1;
  57. int i = 0;
  58. if(str.size() > 1){
  59. while(str[i] != finish)
  60. {
  61. admissible = is_admissible(finish, str[i]) && admissible ? 1 : 0;
  62. i++;
  63. monotonic = is_monotonic(finish, str[i-1], str[i]) && monotonic ? 1 : 0;
  64. }
  65. admissible = is_admissible(finish, str[i]) && admissible ? 1 : 0;
  66. for(i = 0; i < str.size() ; i++){
  67. cout << char(str[i] + 'a');
  68. cout<<" "<<weight_from[i];}
  69. if(admissible)
  70. cout<<endl<<"Heuristic is admissible"<<endl;
  71. else
  72. cout<<endl<<"Heuristic is not admissible"<<endl;
  73. if(monotonic)
  74. cout<<"Heuristic is monotonic"<<endl;
  75. else
  76. cout<<"Heuristic is not monotonic"<<endl;
  77. }
  78. else cout << "нет пути";
  79. }
  80. double evristic(int i, int j){
  81. return abs(i - j);
  82. }
  83. bool is_admissible( int finish, int ver){
  84. return finish_way - weight_from[ver] >= evristic(finish, ver);
  85. }
  86. bool is_monotonic(int finish, int ver1, int ver2){
  87. return graf[ver1][ver2] >= evristic(finish, ver1) - evristic(finish, ver2);
  88. }
  89. void min_way_A_star(int first, int finish, priority_queue <Priority> &queue, double way){
  90. vector<int> str1;
  91. weight_from[0] = 0;
  92. while(true){
  93. for(int j = 0; j < N; j++)
  94. if(graf[first][j] != 0){
  95. Priority elem;
  96. elem.var_evristic = evristic(finish, j);
  97. elem.prior = graf[first][j] + way + elem.var_evristic;
  98. for(auto i: str1)
  99. elem.resultat.push_back(i);
  100. elem.resultat.push_back(j);
  101. queue.push(elem);
  102. }
  103. if(queue.empty())
  104. break;
  105. if(!queue.empty()){
  106. Priority popped;
  107. popped = queue.top();
  108. queue.pop();
  109. first = popped.resultat[popped.resultat.size()-1];
  110. str1 = popped.resultat;
  111. way = popped.prior - popped.var_evristic;
  112. weight_from[first] = popped.prior;
  113. }
  114. if(str1[str1.size()-1] == finish){
  115. for(auto i: str1)
  116. str.push_back(i);
  117. break;
  118. }
  119. }
  120. str1.clear();
  121. finish_way = weight_from[first] + evristic(finish, str.size() - 1);
  122. }
  123. };
  124.  
  125. int main() {
  126. char start, the_end, from, to;
  127. double weight;
  128. cin>> start >> the_end;
  129. priority_queue <Priority> queue;
  130. Graf matr(start - 'a');
  131. while(cin >> from >> to >> weight){
  132. matr.matrix(from - 'a', to - 'a', weight);
  133. }
  134. // matr.Display();
  135. matr.min_way_A_star(start - 'a', the_end - 'a', queue, 0);
  136. matr.print_path(start - 'a', the_end - 'a');
  137. return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement