Sanlover

Untitled

Mar 24th, 2022
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <Windows.h>
  4. using namespace std;
  5.  
  6. using double_vector_t = vector<vector<int>>;
  7. using coordinate_t = pair<int, int>;
  8. using coordinate_with_flag_t = pair<coordinate_t, bool>;
  9.  
  10. bool isChangedMaxWayLength = false;
  11.  
  12. //done
  13. void print(vector<coordinate_t>& resultWay)
  14. {
  15. cout << "Result way:" << endl;
  16. for (int j = 0; j < resultWay.size(); j++)
  17. {
  18. cout << "(" << resultWay[j].first << ":" << resultWay[j].second << ")" << ", ";
  19. }
  20. }
  21.  
  22. // done
  23. int getCrossAmount(double_vector_t& matrix, const int& N, const int& node)
  24. {
  25. int amount = 0;
  26. for (int j = 0; j < N; j++)
  27. {
  28. if (matrix[node][j] != 0)
  29. {
  30. amount++;
  31. }
  32. }
  33. return amount;
  34. }
  35.  
  36. // done
  37. int getMaxWayLength(double_vector_t& matrix, const int& N)
  38. {
  39. int summary = 0;
  40. for (int i = 0; i < N; i++)
  41. {
  42. for (int j = 0; j < N; j++)
  43. {
  44. if (matrix[i][j] != 0)
  45. {
  46. summary += matrix[i][j];
  47. }
  48. }
  49. }
  50. int response;
  51. for (int i = 0; i < N; i++)
  52. {
  53. response = getCrossAmount(matrix, N, i);
  54. if (response != 0)
  55. {
  56. summary += pow(response, response);
  57. }
  58. }
  59. return summary;
  60. }
  61.  
  62. // done
  63. int isAlreadyVisited(vector<coordinate_with_flag_t>& visited, const coordinate_t& node)
  64. {
  65. for (int k = 0; k < visited.size(); k++)
  66. {
  67. if (visited[k].first.first == node.first && visited[k].first.second == node.second ||
  68. visited[k].first.second == node.first && visited[k].first.first == node.second)
  69. {
  70. return visited[k].second == true;
  71. }
  72. }
  73. return false;
  74. }
  75.  
  76.  
  77. // done
  78. void markVisited(vector<coordinate_with_flag_t>& visited, const coordinate_t& node)
  79. {
  80. const int visitedSize = visited.size();
  81. for (int k = 0; k < visitedSize; k++)
  82. {
  83. if (visited[k].first.first == node.first && visited[k].first.second == node.second ||
  84. visited[k].first.second == node.first && visited[k].first.first == node.second)
  85. {
  86. visited[k].second = true;
  87. }
  88. }
  89. }
  90.  
  91. //done
  92. void markUnvisited(vector<coordinate_with_flag_t>& visited, const coordinate_t& node)
  93. {
  94. for (int k = 0; k < visited.size(); k++)
  95. {
  96. if (visited[k].first.first == node.first && visited[k].first.second == node.second ||
  97. visited[k].first.second == node.first && visited[k].first.first == node.second)
  98. {
  99. visited[k].second = false;
  100. }
  101. }
  102. }
  103.  
  104. // done
  105. bool isAllVisited(vector<coordinate_with_flag_t>& visited)
  106. {
  107. for (int i = 0; i < visited.size(); i++)
  108. {
  109. if (visited[i].second == false)
  110. {
  111. return false;
  112. }
  113. }
  114. return true;
  115. }
  116.  
  117. void getOptimizedWay(double_vector_t& matrix,
  118. int& maxWayLength,
  119. int& wayLength,
  120. vector<coordinate_t>& currentWay,
  121. vector<coordinate_t>& resultWay,
  122. vector<coordinate_with_flag_t>& visited,
  123. const int& i)
  124. {
  125. currentWay.push_back(coordinate_t(i, 0));
  126. for (int j = 0; j < matrix.size(); j++)
  127. {
  128. coordinate_t currentNode(i, j);
  129.  
  130. if (matrix[i][j] != 0 && !isAlreadyVisited(visited, currentNode) && wayLength + matrix[i][j] < maxWayLength)
  131. {
  132. wayLength += matrix[i][j];
  133. markVisited(visited, currentNode);
  134. if (isAllVisited(visited))
  135. {
  136. if (isChangedMaxWayLength == false || wayLength < maxWayLength)
  137. {
  138. currentWay.push_back(coordinate_t(i, j));
  139.  
  140. isChangedMaxWayLength = true;
  141. resultWay = currentWay;
  142. maxWayLength = wayLength;
  143.  
  144. currentWay.pop_back();
  145. }
  146. }
  147. else
  148. {
  149. getOptimizedWay(matrix, maxWayLength, wayLength, currentWay, resultWay, visited, j);
  150. }
  151. wayLength -= matrix[i][j];
  152. markUnvisited(visited, currentNode);
  153. }
  154. }
  155. currentWay.pop_back();
  156. }
  157.  
  158.  
  159. int main()
  160. {
  161. SetConsoleCP(1251);
  162. SetConsoleOutputCP(1251);
  163.  
  164. double_vector_t matrix
  165. {
  166. {0, 10, 20},
  167. {10, 0, 0},
  168. {20, 0, 0}
  169. };
  170.  
  171. int N = matrix.size();
  172. int wayLength = 0, maxWayLength = getMaxWayLength(matrix, N);
  173. vector<coordinate_with_flag_t> visited;
  174. vector<coordinate_t> currentWay, resultWay;
  175.  
  176. cout << "Наши данные: " << endl;
  177. for (int i = 0; i < N; i++)
  178. {
  179. for (int j = 0; j < N; j++)
  180. {
  181. cout << matrix[i][j] << " ";
  182. }
  183. cout << endl;
  184. }
  185. for (int i = 0; i < N; i++)
  186. {
  187. for (int j = 0; j < N; j++)
  188. {
  189. if (matrix[i][j] != 0)
  190. {
  191. visited.push_back(coordinate_with_flag_t(coordinate_t(i, j), false));
  192. }
  193. }
  194. }
  195.  
  196. for (int i = 0; i < N; i++)
  197. {
  198. coordinate_t currentNode(i, 0);
  199. visited.push_back(coordinate_with_flag_t(currentNode, true));
  200. visited.push_back(coordinate_with_flag_t(coordinate_t(0, i), true));
  201. getOptimizedWay(matrix, maxWayLength, wayLength, currentWay, resultWay, visited, i);
  202. visited.pop_back();
  203. visited.pop_back();
  204. }
  205.  
  206. cout << endl << "Length: " << maxWayLength << endl;
  207. print(resultWay);
  208. }
  209.  
Advertisement
Add Comment
Please, Sign In to add comment