Advertisement
Chockrit

Petey Patterns Source Code

Feb 12th, 2017
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.88 KB | None | 0 0
  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. #include <map>
  5. #include <algorithm>
  6. #include <iomanip>
  7. using namespace std;
  8.  
  9. struct Node {
  10.     string name;
  11.     vector<string> next;
  12.  
  13.     Node(string n)
  14.     : name(n) {}
  15. };
  16.  
  17. struct Edge {
  18.     string a;
  19.     string b;
  20.     double time;
  21.     Edge(string A, string B, double t)
  22.         :a(A), b(B), time(t) {}
  23. };
  24.  
  25. struct Route {
  26.     vector<string> points;
  27.     int scount;
  28.     double time;
  29. };
  30.  
  31. vector<Edge> defEdges() {
  32.     vector<Edge> EDGES;
  33.     EDGES.push_back(Edge("S8", "N1", 2.918));
  34.     EDGES.push_back(Edge("N4", "N6", 6.740));
  35.     EDGES.push_back(Edge("S1", "N1", 3.854));
  36.     EDGES.push_back(Edge("N4", "S2", 7.341));
  37.     EDGES.push_back(Edge("S8", "N4", 6.673));
  38.     EDGES.push_back(Edge("N1", "S4", 4.471));
  39.     EDGES.push_back(Edge("N1", "N2", 4.771));
  40.     EDGES.push_back(Edge("S4", "S5", 5.839));
  41.     EDGES.push_back(Edge("S5", "S6", 6.590));
  42.     EDGES.push_back(Edge("S5", "N5", 4.945));
  43.     EDGES.push_back(Edge("N5", "N2", 4.838));
  44.     EDGES.push_back(Edge("N5", "N3", 6.573));
  45.     EDGES.push_back(Edge("N5", "S7", 5.622));
  46.     EDGES.push_back(Edge("N2", "S5", 5.739));
  47.     EDGES.push_back(Edge("S1", "N4", 8.442));
  48.     EDGES.push_back(Edge("S1", "S2", 3.153));
  49.     EDGES.push_back(Edge("S1", "N2", 4.337));
  50.     EDGES.push_back(Edge("S3", "N3", 8.642));
  51.     EDGES.push_back(Edge("N3", "S2", 2.536));
  52.     EDGES.push_back(Edge("S3", "S2", 6.823));
  53.     EDGES.push_back(Edge("S3", "S7", 13.097));
  54.     EDGES.push_back(Edge("S7", "N3", 8.608));
  55.     EDGES.push_back(Edge("S7", "S6", 5.122));
  56.     EDGES.push_back(Edge("N6", "S3", 4.638));
  57.     return EDGES;
  58. }
  59.  
  60. vector<Node> defNodes() {
  61.     vector<Node> NODES;
  62.  
  63.     Node N1("N1");
  64.     N1.next.push_back("N2");
  65.     N1.next.push_back("S1");
  66.     N1.next.push_back("S4");
  67.     N1.next.push_back("S8");
  68.     NODES.push_back(N1);
  69.  
  70.     Node N2("N2");
  71.     N2.next.push_back("N1");
  72.     N2.next.push_back("S1");
  73.     N2.next.push_back("N5");
  74.     N2.next.push_back("S5");
  75.     NODES.push_back(N2);
  76.  
  77.     Node N3("N3");
  78.     N3.next.push_back("S2");
  79.     N3.next.push_back("S3");
  80.     N3.next.push_back("S7");
  81.     N3.next.push_back("N5");
  82.     NODES.push_back(N3);
  83.  
  84.     Node N4("N4");
  85.     N4.next.push_back("S8");
  86.     N4.next.push_back("S1");
  87.     N4.next.push_back("S2");
  88.     N4.next.push_back("N6");
  89.     NODES.push_back(N4);
  90.  
  91.     Node N5("N5");
  92.     N5.next.push_back("S5");
  93.     N5.next.push_back("N2");
  94.     N5.next.push_back("N3");
  95.     N5.next.push_back("S7");
  96.     NODES.push_back(N5);
  97.  
  98.     Node N6("N6");
  99.     N6.next.push_back("S3");
  100.     N6.next.push_back("N4");
  101.     NODES.push_back(N6);
  102.  
  103.     Node S1("S1");
  104.     S1.next.push_back("N1");
  105.     S1.next.push_back("N2");
  106.     S1.next.push_back("S2");
  107.     S1.next.push_back("N4");
  108.     NODES.push_back(S1);
  109.  
  110.     Node S2("S2");
  111.     S2.next.push_back("S1");
  112.     S2.next.push_back("N4");
  113.     S2.next.push_back("N3");
  114.     S2.next.push_back("S3");
  115.     NODES.push_back(S2);
  116.  
  117.     Node S3("S3");
  118.     S3.next.push_back("S2");
  119.     S3.next.push_back("N6");
  120.     S3.next.push_back("N3");
  121.     S3.next.push_back("S7");
  122.     NODES.push_back(S3);
  123.  
  124.     Node S4("S4");
  125.     S4.next.push_back("N1");
  126.     S4.next.push_back("S5");
  127.     NODES.push_back(S4);
  128.  
  129.     Node S5("S5");
  130.     S5.next.push_back("S4");
  131.     S5.next.push_back("N2");
  132.     S5.next.push_back("N5");
  133.     S5.next.push_back("S6");
  134.     NODES.push_back(S5);
  135.  
  136.     Node S6("S6");
  137.     S6.next.push_back("S5");
  138.     S6.next.push_back("S7");
  139.     NODES.push_back(S6);
  140.  
  141.     Node S7("S7");
  142.     S7.next.push_back("S6");
  143.     S7.next.push_back("N5");
  144.     S7.next.push_back("N3");
  145.     S7.next.push_back("S3");
  146.     NODES.push_back(S7);
  147.  
  148.     Node S8("S8");
  149.     S8.next.push_back("N1");
  150.     S8.next.push_back("N4");
  151.     NODES.push_back(S8);
  152.  
  153.     return NODES;
  154. }
  155.  
  156. double findEdge(vector<Edge> v, string a, string b)
  157. //return time value of an edge ab
  158. {
  159.    
  160.     int i = 0;
  161.     while (i < v.size()) {
  162.         if ((v[i].a == a && v[i].b == b) || (v[i].a == b && v[i].b == a)) {
  163.             return v[i].time;
  164.         }
  165.         i++;
  166.     }
  167.     return -1;
  168. }
  169.  
  170. Node findNode(vector<Node> v, string a) {
  171.     //return node with all next points given name of node a
  172.     int i = 0;
  173.     while (i < v.size()) {
  174.         if (v[i].name == a)
  175.             return v[i];
  176.         i++;
  177.     }
  178.     Node n("");
  179.     return n;
  180. }
  181.  
  182. double routeTime(Route r) {
  183.     double result = 0;
  184.     vector<Edge> EDGES = defEdges();
  185.     for (int i = 1; i < r.points.size(); i++) {
  186.         result += findEdge(EDGES, r.points[i - 1], r.points[i]);
  187.     }
  188.     return result + 10.651;
  189. }
  190.  
  191. bool compRoute(Route a, Route b) {
  192.     return a.time < b.time;
  193. }
  194.  
  195. int main() {
  196.     vector<Edge> EDGES = defEdges();
  197.     vector<Node> NODES = defNodes();
  198.  
  199.     int routes = 0;
  200.    
  201.     vector<Route> result;
  202.  
  203.     vector<Route> toDo;
  204.     Route start;
  205.     start.scount = 0;
  206.     start.points.push_back("N1");
  207.     toDo.push_back(start);
  208.  
  209.     while (!toDo.empty()) {
  210.         Route curr = toDo.back();
  211.         toDo.pop_back();
  212.  
  213.         if (curr.scount == 3) {
  214.             curr.time = routeTime(curr);
  215.             result.push_back(curr);
  216.             continue;
  217.         }
  218.  
  219.         //cout << "Current route: ";
  220.         int x = 0;
  221.         while (x < curr.points.size()) {
  222.             //cout << curr.points[x] << " ";
  223.             x++;
  224.         }
  225.         //cout << endl;
  226.  
  227.         string s = curr.points.back();
  228.         //cout << "Finding neighbors to " << s << endl;
  229.         Node n = findNode(NODES, s);
  230.         if (n.name == "") {
  231.             //cout << "Node N returned without name" << endl;
  232.             exit(0);
  233.         }
  234.  
  235.        
  236.         for (int i = 0; i < n.next.size(); i++) {
  237.             //cout << i << " " << endl;
  238.             Route r = curr;
  239.             string last = "";
  240.             if (r.points.size() >= 2)
  241.                 last = r.points.at(r.points.size() - 2);
  242.             if (n.next[i] != last) {
  243.                 r.points.push_back(n.next[i]);
  244.                 //cout << "New route: " << n.next[i] << endl;
  245.                 if (n.next[i] == "S1" ||
  246.                     n.next[i] == "S2" ||
  247.                     n.next[i] == "S3" ||
  248.                     n.next[i] == "S4" ||
  249.                     n.next[i] == "S5" ||
  250.                     n.next[i] == "S6" ||
  251.                     n.next[i] == "S7" ||
  252.                     n.next[i] == "S8")
  253.                     r.scount++;
  254.                 toDo.push_back(r);
  255.             }
  256.         }
  257.     }
  258.  
  259.     sort(result.begin(), result.end(), compRoute);
  260.     cout << setprecision(3) << fixed;
  261.  
  262.     for (int j = 0; j < result.size(); j++) {
  263.         cout << result[j].time << ": ";
  264.         for (int k = 0; k < result[j].points.size(); k++) {
  265.             cout << result[j].points[k] << " ";
  266.         }
  267.         //cout << " .... " << j + 1 << endl;
  268.         cout << endl;
  269.     }
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement