Advertisement
Guest User

Untitled

a guest
Jan 11th, 2020
132
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct Solution {
  2. std::vector<Pnt> p;
  3. double currentMin;
  4.  
  5. double recurse(int num, double dist) {
  6.  
  7. if (num == 2) {
  8. return p[0].dist(p[1]) + dist;
  9. }
  10.  
  11.  
  12. for (int i = 0; i < num - 2; ++i) {
  13. Pnt t1;
  14. t1 = p[num - 2];
  15. p[num-2] = p[i];
  16. p[i] = t1;
  17.  
  18. for (int j = i + 1; j < num - 1; j++) {
  19. Pnt t2;
  20.  
  21. t2 = p[num - 1];
  22. p[num-1] = p[j];
  23. p[j] = t2;
  24.  
  25. double temp = recurse(num - 2, dist + p[num-2].dist(p[num-1]));
  26.  
  27. t2 = p[num - 1];
  28. p[num-1] = p[j];
  29. p[j] = t2;
  30.  
  31. if (temp < currentMin) {
  32. currentMin = temp;
  33. }
  34. }
  35.  
  36. t1 = p[num - 2];
  37. p[num-2] = p[i];
  38. p[i] = t1;
  39.  
  40. }
  41.  
  42. return currentMin;
  43. }
  44.  
  45. double solve(std::vector<Pnt> input) {
  46. p = input;
  47. currentMin = std::numeric_limits<double>::max();
  48.  
  49. return recurse(p.size(), 0.0);
  50. }
  51. };
Advertisement
RAW Paste Data Copied
Advertisement