Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 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 Programmeeropdracht_6
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. int aantal;
  14.  
  15. // Aantal zinnen om in te zoeken
  16. aantal = Int32.Parse(Console.ReadLine());
  17. string[] zinnetjes = new string[aantal];
  18.  
  19. for (int i = 0; i < aantal; i++)
  20. {
  21. zinnetjes[i] = Console.ReadLine();
  22. }
  23.  
  24. for (int i = 0; i < aantal; i++)
  25. {
  26. Tricky.Vertex min = new Tricky.Vertex();
  27. min = Tricky.BFS(zinnetjes[i]);
  28. Console.WriteLine(min.Dist.ToString() + " " + min.Action);
  29. }
  30.  
  31. // Zodat cmd blijft staan
  32. Console.ReadLine();
  33. }
  34. }
  35.  
  36. class Tricky
  37. {
  38. // Methode voor Breadth-First Search
  39. public static Vertex BFS (string A)
  40. {
  41. // Nieuwe string aanmaken en sorteren om als referentie te gebruiken
  42. string Sorted = "";
  43. Sorted = String.Concat(A.OrderBy(c => c));
  44.  
  45. // Aanmaken eerste vertex S
  46. Vertex S = new Vertex();
  47. S.Key = A;
  48. S.Dist = 0;
  49. S.Parent = null;
  50.  
  51. // Aanmaken hashset met strings om te checken of deze al in de queue zit
  52. HashSet<string> tamos = new HashSet<string>();
  53.  
  54. // Aanmaken Queue om alle te onderzoeken vertices in te stoppen
  55. Queue<Vertex> Q = new Queue<Vertex>();
  56. Q.Enqueue(S);
  57.  
  58. // Doorgaan zolang er elementen in de queue zitten
  59. while (Q.Count != 0)
  60. {
  61. // Aanmaken vertex U
  62. Vertex U = new Vertex();
  63. U = Q.Dequeue();
  64.  
  65. // Breadth-First zoeken
  66. for (int i = 1; i < 4; i++)
  67. {
  68. if (i == 1)
  69. {
  70. // Aanmaken vertex V
  71. Vertex V = new Vertex();
  72. V.Key = doA(S.Key);
  73.  
  74. // Als Key zelfde is als gesorteerde string, return dan vertex
  75. if (V.Key == Sorted)
  76. {
  77. V.Dist = U.Dist + 1;
  78. V.Action += 'a';
  79. return V;
  80. }
  81.  
  82. // Checken of-ie niet in hashset zit, anders toevoegen en in queue stoppen
  83. if (!tamos.Contains(V.Key))
  84. {
  85. // V.Key toevoegen aan hashset en V alle waardes geven
  86. tamos.Add(V.Key);
  87. V.Dist = U.Dist + 1;
  88. V.Action += 'a';
  89. V.Parent = U;
  90. Q.Enqueue(V);
  91. }
  92. }
  93.  
  94. if (i == 2)
  95. {
  96. // Aanmaken vertex V
  97. Vertex V = new Vertex();
  98. V.Key = doB(S.Key);
  99.  
  100. // Als Key zelfde is als gesorteerde string, return dan vertex
  101. if (V.Key == Sorted)
  102. {
  103. V.Dist = U.Dist + 1;
  104. V.Action += 'b';
  105. return V;
  106. }
  107.  
  108. // Checken of-ie niet in hashset zit, anders toevoegen en in queue stoppen
  109. if (!tamos.Contains(V.Key))
  110. {
  111. // V.Key toevoegen aan hashset en V alle waardes geven
  112. tamos.Add(V.Key);
  113. V.Dist = U.Dist + 1;
  114. V.Action += 'b';
  115. V.Parent = U;
  116. Q.Enqueue(V);
  117. }
  118. }
  119.  
  120. if (i == 3)
  121. {
  122. // Aanmaken vertex V
  123. Vertex V = new Vertex();
  124. V.Key = doX(S.Key);
  125.  
  126. // Als Key zelfde is als gesorteerde string, return dan vertex
  127. if (V.Key == Sorted)
  128. {
  129. V.Dist = U.Dist + 1;
  130. V.Action += 'x';
  131. return V;
  132. }
  133.  
  134. // Checken of-ie niet in hashset zit, anders toevoegen en in queue stoppen
  135. if (!tamos.Contains(V.Key))
  136. {
  137. // V.Key toevoegen aan hashset en V alle waardes geven
  138. tamos.Add(V.Key);
  139. V.Dist = U.Dist + 1;
  140. V.Action += 'x';
  141. V.Parent = U;
  142. Q.Enqueue(V);
  143. }
  144. }
  145. }
  146. }
  147.  
  148. // Return de vertex
  149. return S;
  150. }
  151.  
  152. public class Vertex
  153. {
  154. public Vertex Parent;
  155. public string Key;
  156. public int Dist;
  157. public string Action;
  158. }
  159.  
  160. // Methode om actie "A" uit te voeren
  161. public static string doA(string S)
  162. {
  163. // Nieuwe string aanmaken en eerste twee letters omdraaien
  164. string a = "";
  165.  
  166. a += S[1];
  167. a += S[0];
  168. for (int i = 2; i < S.Length; i++)
  169. a += S[i];
  170.  
  171. // Return nieuwe string
  172. return a;
  173. }
  174.  
  175. // Methode om actie "B" uit te voeren
  176. public static string doB (string S)
  177. {
  178. // Nieuwe string aanmaken en laatste twee letters omdraaien
  179. string b = "";
  180.  
  181. for (int i = 0; i < S.Length - 2; i++)
  182. b += S[i];
  183. b += S[S.Length - 1];
  184. b += S[S.Length - 2];
  185.  
  186. // Return nieuwe string
  187. return b;
  188. }
  189.  
  190. // Methode om actie "X" uit te voeren
  191. public static string doX (string S)
  192. {
  193. // Nieuwe string aanmaken en alle middelste letters rechtsom roteren
  194. string x = "";
  195. x += S[0];
  196. x += S[S.Length - 2];
  197.  
  198. for (int i = 1; i < S.Length - 2; i++)
  199. x += S[i];
  200.  
  201. x += S[S.Length - 1];
  202.  
  203. // Return nieuwe string
  204. return x;
  205. }
  206. }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement