Guest User

Untitled

a guest
Nov 15th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <climits>
  5.  
  6. struct inner {
  7. std::string bezeichnung_;
  8. int value_;
  9. };
  10.  
  11. struct node {
  12. inner inner_;
  13. std::vector<int> ptrs_;
  14. bool visited_ = false;
  15. };
  16.  
  17. std::vector<node> CreateGraph(const std::vector<inner>& in) {
  18. std::vector<node> graph{in.size()};
  19. for (unsigned int i = 0; i < in.size(); i++) {
  20. node newnode;
  21. newnode.inner_ = in[i];
  22. newnode.ptrs_.resize(in.size()-1);
  23. for (unsigned int j = 0, k = 0; j < in.size(); j++) {
  24. // Erzeuge Verweise auf alle Knoten, außer den aktuellen Knoten (Knoten i).
  25. if (j == i) continue;
  26. newnode.ptrs_[k] = j;
  27. k++;
  28. }
  29. graph[i] = newnode;
  30. }
  31. return graph;
  32. }
  33.  
  34. void Walk(std::vector<node>& graph, unsigned int current) {
  35. if (graph[current].visited_) return;
  36. graph[current].visited_ = true;
  37. std::cout << graph[current].inner_.bezeichnung_ << " ";
  38.  
  39. int cv = graph[current].inner_.value_;
  40. int diff = INT_MAX, next_elem = -1;
  41. for (unsigned int i = 0; i < graph.size(); i++) {
  42. int current_diff = graph[i].inner_.value_ - cv;
  43. if ( current_diff > 0 && current_diff < diff && !graph[i].visited_) {
  44. diff = current_diff;
  45. next_elem = i;
  46. }
  47. }
  48. if (next_elem < 0) return;
  49. // Jetzt: next_elem ist Index des nächstgrößeren Elements.
  50. Walk(graph, next_elem);
  51. }
  52.  
  53. int main(void) {
  54. std::vector<inner> initial{
  55. {",", 42},
  56. {"\n", 137},
  57. {"dieser", -5},
  58. {"Satz", 2},
  59. {"ergibt", 22},
  60. {"Sinn", 17},
  61. {"ist", 61},
  62. {"Lösung", 67},
  63. {"Wenn", -49},
  64. {"die", 65},
  65. {".", 99},
  66. {"korrekt", 81},
  67. };
  68. std::vector<node> graph = CreateGraph(initial);
  69. Walk(graph, 8);
  70. }
Add Comment
Please, Sign In to add comment