Advertisement
MeGaDeTH_91

KnightGame

Feb 5th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _02._Knight_Game
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. int boardSize = int.Parse(Console.ReadLine());
  12.  
  13. string[,] chessBoard = new string[boardSize, boardSize];
  14.  
  15. FillChessBoard(ref chessBoard, boardSize);
  16.  
  17. int minRemoved = CheckBoard(ref chessBoard, boardSize);
  18.  
  19. Console.WriteLine(minRemoved);
  20. }
  21.  
  22. private static int CheckBoard(ref string[,] chessBoard, int boardSize)
  23. {
  24. bool numIsEnough = false;
  25.  
  26. int knightRemoved = 0;
  27.  
  28. while (!numIsEnough)
  29. {
  30. List<KnightReach> knightsReach = new List<KnightReach>();
  31.  
  32. for (int row = 0; row < boardSize; row++)
  33. {
  34. for (int col = 0; col < boardSize; col++)
  35. {
  36. if (chessBoard[row, col] == "K")
  37. {
  38. //Check 2xRight 1xUp
  39. if (IsLocationValid(row - 1, col + 2, boardSize) && chessBoard[row - 1, col + 2] == "K")
  40. {
  41. if (!knightsReach.Any(x => x.RowPosition == row && x.ColPosition == col))
  42. {
  43. KnightReach currKnight = new KnightReach
  44. {
  45. RowPosition = row,
  46. ColPosition = col,
  47. ReachNum = 0
  48. };
  49. knightsReach.Add(currKnight);
  50. }
  51. knightsReach.FirstOrDefault(x => x.RowPosition == row && x.ColPosition == col).ReachNum++;
  52. }
  53.  
  54. //Check 2xRight 1xDown
  55. if (IsLocationValid(row + 1, col + 2, boardSize) && chessBoard[row + 1, col + 2] == "K")
  56. {
  57. if (!knightsReach.Any(x => x.RowPosition == row && x.ColPosition == col))
  58. {
  59. KnightReach currKnight = new KnightReach
  60. {
  61. RowPosition = row,
  62. ColPosition = col,
  63. ReachNum = 0
  64. };
  65. knightsReach.Add(currKnight);
  66. }
  67. knightsReach.FirstOrDefault(x => x.RowPosition == row && x.ColPosition == col).ReachNum++;
  68. }
  69.  
  70. //Check 2xLeft 1xUp
  71. if (IsLocationValid(row - 1, col - 2, boardSize) && chessBoard[row - 1, col - 2] == "K")
  72. {
  73. if (!knightsReach.Any(x => x.RowPosition == row && x.ColPosition == col))
  74. {
  75. KnightReach currKnight = new KnightReach
  76. {
  77. RowPosition = row,
  78. ColPosition = col,
  79. ReachNum = 0
  80. };
  81. knightsReach.Add(currKnight);
  82. }
  83. knightsReach.FirstOrDefault(x => x.RowPosition == row && x.ColPosition == col).ReachNum++;
  84. }
  85.  
  86. //Check 2xLeft 1xDown
  87. if (IsLocationValid(row + 1, col - 2, boardSize) && chessBoard[row + 1, col - 2] == "K")
  88. {
  89. if (!knightsReach.Any(x => x.RowPosition == row && x.ColPosition == col))
  90. {
  91. KnightReach currKnight = new KnightReach
  92. {
  93. RowPosition = row,
  94. ColPosition = col,
  95. ReachNum = 0
  96. };
  97. knightsReach.Add(currKnight);
  98. }
  99. knightsReach.FirstOrDefault(x => x.RowPosition == row && x.ColPosition == col).ReachNum++;
  100. }
  101.  
  102. //Check 2xUp 1xRight
  103. if (IsLocationValid(row - 2, col + 1, boardSize) && chessBoard[row - 2, col + 1] == "K")
  104. {
  105. if (!knightsReach.Any(x => x.RowPosition == row && x.ColPosition == col))
  106. {
  107. KnightReach currKnight = new KnightReach
  108. {
  109. RowPosition = row,
  110. ColPosition = col,
  111. ReachNum = 0
  112. };
  113. knightsReach.Add(currKnight);
  114. }
  115. knightsReach.FirstOrDefault(x => x.RowPosition == row && x.ColPosition == col).ReachNum++;
  116. }
  117.  
  118. //Check 2xUp 1xLeft
  119. if (IsLocationValid(row - 2, col - 1, boardSize) && chessBoard[row - 2, col - 1] == "K")
  120. {
  121. if (!knightsReach.Any(x => x.RowPosition == row && x.ColPosition == col))
  122. {
  123. KnightReach currKnight = new KnightReach
  124. {
  125. RowPosition = row,
  126. ColPosition = col,
  127. ReachNum = 0
  128. };
  129. knightsReach.Add(currKnight);
  130. }
  131. knightsReach.FirstOrDefault(x => x.RowPosition == row && x.ColPosition == col).ReachNum++;
  132. }
  133.  
  134. //Check 2xDown 1xRight
  135. if (IsLocationValid(row + 2, col + 1, boardSize) && chessBoard[row + 2, col + 1] == "K")
  136. {
  137. if (!knightsReach.Any(x => x.RowPosition == row && x.ColPosition == col))
  138. {
  139. KnightReach currKnight = new KnightReach
  140. {
  141. RowPosition = row,
  142. ColPosition = col,
  143. ReachNum = 0
  144. };
  145. knightsReach.Add(currKnight);
  146. }
  147. knightsReach.FirstOrDefault(x => x.RowPosition == row && x.ColPosition == col).ReachNum++;
  148. }
  149.  
  150. //Check 2xDown 1xLeft
  151. if (IsLocationValid(row + 2, col - 1, boardSize) && chessBoard[row + 2, col - 1] == "K")
  152. {
  153. if (!knightsReach.Any(x => x.RowPosition == row && x.ColPosition == col))
  154. {
  155. KnightReach currKnight = new KnightReach
  156. {
  157. RowPosition = row,
  158. ColPosition = col,
  159. ReachNum = 0
  160. };
  161. knightsReach.Add(currKnight);
  162. }
  163. knightsReach.FirstOrDefault(x => x.RowPosition == row && x.ColPosition == col).ReachNum++;
  164. }
  165. }
  166. }
  167. }
  168.  
  169. if (!knightsReach.Any())
  170. {
  171. return knightRemoved;
  172. }
  173.  
  174. knightsReach = knightsReach.OrderByDescending(x => x.ReachNum).ToList();
  175.  
  176. int cellRow = knightsReach[0].RowPosition;
  177. int cellCol = knightsReach[0].ColPosition;
  178. chessBoard[cellRow, cellCol] = "0";
  179. knightRemoved++;
  180. }
  181.  
  182. return knightRemoved;
  183. }
  184.  
  185. private static bool IsLocationValid(int row, int col, int boardSize)
  186. {
  187. return row < boardSize && row >= 0 && col < boardSize && col >= 0;
  188. }
  189.  
  190. private static void FillChessBoard(ref string[,] chessBoard, int boardSize)
  191. {
  192. for (int row = 0; row < boardSize; row++)
  193. {
  194. string rowInput = Console.ReadLine();
  195.  
  196. for (int col = 0; col < boardSize; col++)
  197. {
  198. chessBoard[row, col] = rowInput[col].ToString();
  199. }
  200. }
  201. }
  202. private class KnightReach
  203. {
  204. public int RowPosition { get; set; }
  205.  
  206. public int ColPosition { get; set; }
  207.  
  208. public int ReachNum { get; set; }
  209. }
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement