Advertisement
Guest User

gabi e sladka

a guest
Feb 28th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <stack>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. bool allArePresent(list<string> &base, list<string> &elements)
  9. {
  10.     for (auto &el : elements)
  11.     {
  12.         bool found = false;
  13.         for (auto &el2 : base)
  14.         {
  15.             if (el2 == el)
  16.             {
  17.                 found = true;
  18.             }
  19.         }
  20.         if (!found)
  21.         {
  22.             return false;
  23.         }
  24.     }
  25.     return true;
  26. }
  27.  
  28. void addIfAbsent(list<string> &l, list<string> &elements)
  29. {
  30.     for (auto el : elements)
  31.     {
  32.         bool found = false;
  33.         for (auto &res_el : l)
  34.         {
  35.             if (res_el == el)
  36.             {
  37.                 found = true;
  38.                 break;
  39.             }
  40.         }
  41.         if (!found)
  42.         {
  43.             l.push_back(el);
  44.         }
  45.     }
  46. }
  47.  
  48. bool addIfAbsent(list<string> &list, string el)
  49. {
  50.     bool found = false;
  51.     for (auto &res_el : list)
  52.     {
  53.         if (res_el == el)
  54.         {
  55.             found = true;
  56.             break;
  57.         }
  58.     }
  59.     if (!found)
  60.     {
  61.         list.push_back(el);
  62.         return true;
  63.     }
  64.     return false;
  65. }
  66.  
  67. list<string> getSurroundingStations(const list<list<string>> &lines, string station)
  68. {
  69.     list<string> result;
  70.     for (const list<string> &el : lines)
  71.     {
  72.         string prev = "";
  73.         auto it = el.begin();
  74.         while (it != el.end())
  75.         {
  76.             if (*it == station)
  77.             {
  78.                 if (!prev.empty())
  79.                 {
  80.                     addIfAbsent(result, prev);
  81.                 }
  82.                 it++;
  83.                 if (it != el.end())
  84.                 {
  85.                     addIfAbsent(result, *it);
  86.                 }
  87.                 break;
  88.             }
  89.             else
  90.             {
  91.                 prev = *it;
  92.             }
  93.             it++;
  94.         }
  95.     }
  96.  
  97.     return result;
  98. }
  99.  
  100. void printStack(stack<string> st)
  101. {
  102.     while (!st.empty())
  103.     {
  104.         cout << st.top() << ' ';
  105.         st.pop();
  106.     }
  107.     cout << '\n';
  108. }
  109.  
  110. void printList(list<string> l)
  111. {
  112.     for (auto &el : l)
  113.     {
  114.         cout << el << ' ';
  115.     }
  116.     cout << endl;
  117. }
  118.  
  119. list<string> getPath(const list<list<string>> &lines, const string &origin, const string &destination)
  120. {
  121.     list<string> visited;
  122.     list<string> path;
  123.     stack<string> st;
  124.     path.push_back(origin);
  125.     visited.push_back(origin);
  126.     st.push(origin);
  127.  
  128.     while (!st.empty() && path.back() != destination)
  129.     {
  130.         cout << "List: ";
  131.         printList(path);
  132.         cout << "Stack: ";
  133.         printStack(st);
  134.         cout << endl;
  135.  
  136.         list<string> current = getSurroundingStations(lines, st.top());
  137.         if (allArePresent(visited, current))
  138.         {
  139.             if (!st.empty())
  140.             {
  141.                 st.pop();
  142.             }
  143.             if (!path.empty())
  144.             {
  145.                 path.pop_back();
  146.             }
  147.         }
  148.         else
  149.         {
  150.             if (path.back() != st.top())
  151.             {
  152.                 path.push_back(st.top());
  153.             }
  154.  
  155.             for (auto &el : current)
  156.             {
  157.                 if (addIfAbsent(visited, el))
  158.                 {
  159.                     st.push(el);
  160.                 }
  161.                 if (st.top() == destination)
  162.                 {
  163.                     path.push_back(destination);
  164.                     break;
  165.                 }
  166.             }
  167.  
  168.             if (path.back() != st.top())
  169.             {
  170.                 path.push_back(st.top());
  171.             }
  172.         }
  173.     }
  174.  
  175.     return path;
  176. }
  177.  
  178. int main()
  179. {
  180.     list<list<string>> lines = {{"Stranraer", "Glasgow Central", "Falkirk High", "Edinburgh"},
  181.                                 {"Fort William", "Glasgow Central", "Edinburgh", "Newcastle", "York", "Doncaster", "Peterborough", "London KingsX"},
  182.                                 {"Bangor", "Chester", "Crewe", "Nottingham", "Peterborough", "Norwich"},
  183.                                 {"Norwich", "Cambridge", "Hitchin", "London KingsX"},
  184.                                 {"Penzance", "Plymouth", "Exeter", "Swindon", "London Victoria"}};
  185.  
  186.     list<string> path = getPath(lines, "Stranraer", "Norwich");
  187.  
  188.     for (auto &el : path)
  189.     {
  190.         cout << el << ' ';
  191.     }
  192.     return 0;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement