viraco4a

Knight's Tour

Mar 31st, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. using System;
  2.  
  3. namespace GreedyKnight
  4. {
  5. class Program
  6. {
  7.  
  8. private static int[,] chessboard;
  9. private static int counter;
  10.  
  11. static void Main()
  12. {
  13. int N = int.Parse(Console.ReadLine());
  14. chessboard = new int[N, N];
  15. counter = 1;
  16. chessboard[0, 0] = counter;
  17. FindPath(0, 0);
  18. }
  19.  
  20. private static void FindPath(int col, int row)
  21. {
  22. if (!IsInMa3x(col, row))
  23. {
  24. return;
  25. }
  26. int possibilities = 0;
  27. int bestPossibility = int.MaxValue;
  28. int nextCol = 0;
  29. int nextRow = 0;
  30.  
  31. possibilities = CalcWarnsdorf(col + 2, row + 1); //RRD 1
  32. if (possibilities < bestPossibility)
  33. {
  34. bestPossibility = possibilities;
  35. nextCol = col + 2;
  36. nextRow = row + 1;
  37. }
  38. possibilities = CalcWarnsdorf(col + 2, row - 1); //RRU 8
  39. if (possibilities < bestPossibility)
  40. {
  41. bestPossibility = possibilities;
  42. nextCol = col + 2;
  43. nextRow = row - 1;
  44. }
  45.  
  46.  
  47.  
  48. possibilities = CalcWarnsdorf(col - 2, row + 1); //LLD 4
  49. if (possibilities < bestPossibility)
  50. {
  51. bestPossibility = possibilities;
  52. nextCol = col - 2;
  53. nextRow = row + 1;
  54. }
  55. possibilities = CalcWarnsdorf(col - 2, row - 1); //LLU 5
  56. if (possibilities < bestPossibility)
  57. {
  58. bestPossibility = possibilities;
  59. nextCol = col - 2;
  60. nextRow = row - 1;
  61. }
  62.  
  63. possibilities = CalcWarnsdorf(col + 1, row + 2); //DDR 2
  64. if (possibilities < bestPossibility)
  65. {
  66. bestPossibility = possibilities;
  67. nextCol = col + 1;
  68. nextRow = row + 2;
  69. }
  70. possibilities = CalcWarnsdorf(col - 1, row + 2); //DDL 3
  71. if (possibilities < bestPossibility)
  72. {
  73. bestPossibility = possibilities;
  74. nextCol = col - 1;
  75. nextRow = row + 2;
  76. }
  77.  
  78. possibilities = CalcWarnsdorf(col + 1, row - 2); //RUU 7
  79. if (possibilities < bestPossibility)
  80. {
  81. bestPossibility = possibilities;
  82. nextCol = col + 1;
  83. nextRow = row - 2;
  84. }
  85.  
  86. possibilities = CalcWarnsdorf(col - 1, row - 2); //LUU 6
  87. if (possibilities < bestPossibility)
  88. {
  89. bestPossibility = possibilities;
  90. nextCol = col - 1;
  91. nextRow = row - 2;
  92. }
  93.  
  94. counter++;
  95. chessboard[nextCol, nextRow] = counter;
  96. if (counter == (chessboard.GetLength(0)*chessboard.GetLength(1)))
  97. {
  98. Print(chessboard);
  99. return;
  100. }
  101. FindPath(nextCol, nextRow);
  102. }
  103.  
  104. private static void Print(int[,] chessboard)
  105. {
  106. for (int i = 0; i < chessboard.GetLength(0); i++)
  107. {
  108. for (int j = 0; j < chessboard.GetLength(1); j++)
  109. {
  110. string str = string.Format("{0} ", chessboard[j, i]);
  111. Console.Write(str.PadLeft(4, ' '));
  112. }
  113. Console.WriteLine();
  114. }
  115. }
  116.  
  117. private static int CalcWarnsdorf(int col, int row)
  118. {
  119. if (!IsInMa3x(col, row))
  120. {
  121. return int.MaxValue;
  122. }
  123. else if (!IsNewPosition(col, row))
  124. {
  125. return int.MaxValue;
  126. }
  127. else
  128. {
  129. int possibilities = 0;
  130. possibilities += IsSolution(col + 2, row + 1); //RRD 1
  131. possibilities += IsSolution(col + 1, row + 2); //DDR 2
  132. possibilities += IsSolution(col - 1, row + 2); //DDL 3
  133. possibilities += IsSolution(col - 2, row + 1); //LLD 4
  134. possibilities += IsSolution(col - 2, row - 1); //LLU 5
  135. possibilities += IsSolution(col - 1, row - 2); //LUU 6
  136. possibilities += IsSolution(col + 1, row - 2); //RUU 7
  137. possibilities += IsSolution(col + 2, row - 1); //RRU 8
  138. return possibilities;
  139. }
  140. }
  141.  
  142. private static int IsSolution(int col, int row)
  143. {
  144. if (!IsInMa3x(col, row))
  145. {
  146. return 0;
  147. }
  148. if (!IsNewPosition(col, row))
  149. {
  150. return 0;
  151. }
  152. else
  153. {
  154. return 1;
  155. }
  156. }
  157.  
  158. private static bool IsNewPosition(int col, int row)
  159. {
  160. if (chessboard[col, row] == 0)
  161. {
  162. return true;
  163. }
  164. return false;
  165. }
  166.  
  167. private static bool IsInMa3x(int col, int row)
  168. {
  169. if (col < 0 || row < 0)
  170. {
  171. return false;
  172. }
  173. if (col > chessboard.GetLength(0) - 1 || row > chessboard.GetLength(1) - 1)
  174. {
  175. return false;
  176. }
  177. return true;
  178. }
  179. }
  180. }
Add Comment
Please, Sign In to add comment