Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. int path_between_two_nodes(const std::string& first, const std::string& second)
  2.     {
  3.         // first find both nodes
  4.         planet* first_p = nullptr;
  5.         planet* second_p = nullptr;
  6.  
  7.         std::stack<planet*> stack;
  8.         for (auto& i : branches)
  9.             stack.push(i.get());
  10.  
  11.         while (!stack.empty())
  12.         {
  13.             auto current_branch = stack.top();
  14.             stack.pop();
  15.  
  16.             for (auto& i : current_branch->get_childs())
  17.             {
  18.                 stack.push(i.get());
  19.             }
  20.  
  21.             if (current_branch->name == first)
  22.             {
  23.                 first_p = current_branch;
  24.             }
  25.             if (current_branch->name == second)
  26.             {
  27.                 second_p = current_branch;
  28.             }
  29.         }
  30.         // done
  31.         // now find find the path to COM from both
  32.  
  33.         std::vector<planet*> first_path;
  34.         auto temp = first_p;
  35.         while (temp != nullptr)
  36.         {
  37.             first_path.push_back(temp);
  38.             temp = temp->parent;
  39.         }
  40.  
  41.         std::vector<planet*> second_path;
  42.         temp = second_p;
  43.         while (temp != nullptr)
  44.         {
  45.             second_path.push_back(temp);
  46.             temp = temp->parent;
  47.         }
  48.  
  49.  
  50.         // done
  51.         // now find the first same node
  52.  
  53.         planet* first_intersection = nullptr;
  54.  
  55.         std::reverse(first_path.begin(), first_path.end());
  56.         std::reverse(second_path.begin(), second_path.end());
  57.  
  58.  
  59.         for (auto i = first_path.begin(); i < first_path.end(); ++i)
  60.         {
  61.             for (auto j = second_path.begin(); j < second_path.end(); ++j)
  62.             {
  63.                 if (*i == *j)
  64.                     first_intersection = *i;
  65.             }
  66.         }
  67.  
  68.         std::reverse(first_path.begin(), first_path.end());
  69.         std::reverse(second_path.begin(), second_path.end());
  70.  
  71.         int ctr1 = 0;
  72.         for (auto& e : first_path)
  73.         {
  74.             if (e == first_intersection)
  75.             {
  76.                 ++ctr1;
  77.                 break;
  78.             }
  79.             ++ctr1;
  80.         }
  81.  
  82.         int ctr2 = 0;
  83.         for (auto& e : second_path)
  84.         {
  85.             if (e == first_intersection)
  86.             {
  87.                 ++ctr2;
  88.                 break;
  89.             }
  90.             ++ctr2;
  91.         }
  92.        
  93.         std::cout << ctr1 << "  " << ctr2 << std::endl;
  94.         return ctr1 + ctr2 - 4;
  95.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement