Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 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. A, B, L, M, P, Q, R, S
  12. }
  13.  
  14. class Node
  15. {
  16. public Node(NODES n)
  17. {
  18. name = n;
  19. }
  20. public NODES name;
  21. public List<NODES> ins;
  22. public List<NODES> outs;
  23. }
  24.  
  25. class Path
  26. {
  27. public Path(NODES i, NODES o)
  28. {
  29. inNode = i;
  30. outNode = o;
  31. }
  32. public NODES inNode, outNode;
  33. }
  34.  
  35. class Program
  36. {
  37. public const uint TOTAL_PATHS = 14;
  38.  
  39. public static Dictionary<NODES, Node> locations = new Dictionary<NODES, Node>();
  40.  
  41. private static void Init()
  42. {
  43. foreach (NODES n in Enum.GetValues(typeof(NODES)))
  44. {
  45. locations.Add(n, new Node(n));
  46. }
  47.  
  48. // ===============
  49. locations[NODES.A].outs = new List<NODES>() {
  50. NODES.S
  51. };
  52. locations[NODES.A].ins = new List<NODES>() {
  53. NODES.R
  54. };
  55. // ===============
  56. locations[NODES.B].outs = new List<NODES>() {
  57. NODES.L, NODES.R
  58. };
  59. locations[NODES.B].ins = new List<NODES>() {
  60. NODES.L, NODES.R
  61. };
  62. // ===============
  63. locations[NODES.L].outs = new List<NODES>() {
  64. NODES.B, NODES.M, NODES.R, NODES.P
  65. };
  66. locations[NODES.L].ins = new List<NODES>() {
  67. NODES.B, NODES.R, NODES.Q, NODES.M
  68. };
  69. // ===============
  70. locations[NODES.M].outs = new List<NODES>() {
  71. NODES.L
  72. };
  73. locations[NODES.M].ins = new List<NODES>() {
  74. NODES.L
  75. };
  76. // ===============
  77. locations[NODES.P].outs = new List<NODES>() {
  78. NODES.Q
  79. };
  80. locations[NODES.P].ins = new List<NODES>() {
  81. NODES.L
  82. };
  83. // ===============
  84. locations[NODES.Q].outs = new List<NODES>() {
  85. NODES.L
  86. };
  87. locations[NODES.Q].ins = new List<NODES>() {
  88. NODES.P
  89. };
  90. // ===============
  91. locations[NODES.R].outs = new List<NODES>() {
  92. NODES.A, NODES.B, NODES.L
  93. };
  94. locations[NODES.R].ins = new List<NODES>() {
  95. NODES.S, NODES.B, NODES.L
  96. };
  97. // ===============
  98. locations[NODES.S].outs = new List<NODES>() {
  99. NODES.R
  100. };
  101. locations[NODES.S].ins = new List<NODES>() {
  102. NODES.A
  103. };
  104. }
  105.  
  106. private static bool UsedAllPaths(Stack<NODES> routeStack)
  107. {
  108. List<NODES> route = routeStack.ToList();
  109. List<Path> usedPaths = new List<Path>();
  110.  
  111. for (int r = 0; r < route.Count - 1; r++)
  112. {
  113. if (!usedPaths.Contains(new Path(route[r], route[r + 1])))
  114. {
  115. usedPaths.Add(new Path(route[r], route[r + 1]));
  116. }
  117. }
  118. return usedPaths.Count == TOTAL_PATHS;
  119. }
  120.  
  121. static void Main(string[] args)
  122. {
  123. Init();
  124.  
  125. uint routeCount = 0;
  126.  
  127. Stack<NODES> route = new Stack<NODES>();
  128. Stack<int> outIndex = new Stack<int>();
  129.  
  130. route.Push(NODES.A);
  131. outIndex.Push(0);
  132.  
  133. do
  134. {
  135.  
  136. if(UsedAllPaths(route))
  137. {
  138. int currentOutIndex = outIndex.Pop();
  139. outIndex.Push(currentOutIndex + 1);
  140. routeCount++;
  141.  
  142. if (outIndex.Last() == locations[route.Last()].outs.Count)
  143. {
  144. outIndex.Pop();
  145. route.Pop();
  146.  
  147. for(int n = route.Count - 1; n >= 0; n--)
  148. {
  149. Console.Write(route.ToList()[n]);
  150. }
  151. Console.WriteLine();
  152. }
  153. }
  154.  
  155. route.Push(locations[route.Last()].outs[outIndex.Last()]);
  156.  
  157. } while (route.Count > 0);
  158.  
  159. Console.WriteLine("==================\n" + routeCount);
  160. Console.ReadLine();
  161. }
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement