Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace MathsBombe_Q7
  8. {
  9. enum NODES
  10. { // locations
  11. AS, B, L, M, PQ, R
  12. }
  13.  
  14. class Node
  15. {
  16. public Node(NODES n)
  17. {
  18. name = n;
  19. }
  20. public NODES name;
  21. public List<NODES> links;
  22. }
  23.  
  24. class Path
  25. {
  26. public Path(NODES i, NODES o)
  27. {
  28. inNode = i;
  29. outNode = o;
  30. }
  31. public NODES inNode, outNode;
  32. }
  33.  
  34. class Program
  35. {
  36. public const uint TOTAL_PATHS = 14;
  37.  
  38. public static Dictionary<NODES, Node> locations = new Dictionary<NODES, Node>();
  39.  
  40. private static void Init()
  41. {
  42. foreach (NODES n in Enum.GetValues(typeof(NODES)))
  43. {
  44. locations.Add(n, new Node(n));
  45. }
  46. locations[NODES.AS].links = new List<NODES>() {
  47. NODES.R
  48. };
  49. locations[NODES.B].links = new List<NODES>() {
  50. NODES.L, NODES.R
  51. };
  52. locations[NODES.L].links = new List<NODES>() {
  53. NODES.B, NODES.R, NODES.PQ, NODES.M
  54. };
  55. locations[NODES.M].links = new List<NODES>() {
  56. NODES.L
  57. };
  58. // ===============
  59. locations[NODES.PQ].links = new List<NODES>() {
  60. NODES.L
  61. };
  62. // ===============
  63. locations[NODES.R].links = new List<NODES>() {
  64. NODES.AS, NODES.B, NODES.L
  65. };
  66. }
  67.  
  68. private static bool UsedAllPaths(Stack<NODES> routeStack)
  69. {
  70. List<NODES> route = routeStack.ToList();
  71. List<Path> usedPaths = new List<Path>();
  72.  
  73. for (int r = 0; r < route.Count - 1; r++)
  74. {
  75. if (!usedPaths.Contains(new Path(route[r], route[r + 1])))
  76. {
  77. usedPaths.Add(new Path(route[r], route[r + 1]));
  78. }
  79. }
  80. return usedPaths.Count == TOTAL_PATHS;
  81. }
  82.  
  83. static void Main(string[] args)
  84. {
  85. Init();
  86.  
  87. uint routeCount = 0;
  88.  
  89. Stack<NODES> route = new Stack<NODES>();
  90. Stack<int> outIndex = new Stack<int>();
  91.  
  92. route.Push(NODES.AS);
  93. outIndex.Push(0);
  94.  
  95. do
  96. {
  97.  
  98. if(UsedAllPaths(route))
  99. {
  100. int currentOutIndex = outIndex.Pop();
  101. outIndex.Push(currentOutIndex + 1);
  102. routeCount++;
  103.  
  104. if (outIndex.First() == locations[route.First()].links.Count)
  105. {
  106. outIndex.Pop();
  107. route.Pop();
  108.  
  109. for(int n = route.Count - 1; n >= 0; n--)
  110. {
  111. Console.Write(route.ToList()[n]);
  112. }
  113. Console.WriteLine();
  114. }
  115. }
  116.  
  117. route.Push(locations[route.First()].links[outIndex.First()]);
  118.  
  119. } while (route.Count > 0);
  120.  
  121. Console.WriteLine("==================\n" + routeCount);
  122. Console.ReadLine();
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement