petarkobakov

Knight Game

Sep 28th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. using System;
  2. using System.Reflection.Metadata;
  3.  
  4. namespace Knight_Game
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. int n = int.Parse(Console.ReadLine());
  11.  
  12. char[,] chessBoard = new char[n, n];
  13. FillTheMatrix(chessBoard);
  14. int removedKnights = 0;
  15. int forceRow = 0;
  16. int forceCol = 0;
  17.  
  18. while (true)
  19. {
  20. int maxStrikes = 0;
  21.  
  22. for (int row = 0; row < chessBoard.GetLength(0); row++)
  23. {
  24. for (int col = 0; col < chessBoard.GetLength(1); col++)
  25. {
  26. char currentSymbol = chessBoard[row, col];
  27. int strikes = 0;
  28.  
  29. if (currentSymbol == 'K')
  30. {
  31. strikes = allStrikePositions(chessBoard, row, col, strikes);
  32.  
  33. if (strikes>maxStrikes)
  34. {
  35. maxStrikes = strikes;
  36. forceRow = row;
  37. forceCol = col;
  38. }
  39. }
  40. }
  41. }
  42.  
  43. if (maxStrikes>0)
  44. {
  45. chessBoard[forceRow, forceCol] = '0';
  46. removedKnights++;
  47. }
  48.  
  49. else
  50. {
  51. Console.WriteLine(removedKnights);
  52. break;
  53. }
  54. }
  55. }
  56.  
  57. private static int allStrikePositions(char[,] chessBoard, int row, int col, int strikes)
  58. {
  59. if (isInRange(chessBoard, row - 2, col + 1) && chessBoard[row - 2, col + 1] == 'K')
  60. {
  61. strikes++;
  62. }
  63.  
  64. if (isInRange(chessBoard, row - 2, col - 1) && chessBoard[row - 2, col - 1] == 'K')
  65. {
  66. strikes++;
  67. }
  68.  
  69. if (isInRange(chessBoard, row + 1, col + 2) && chessBoard[row +1, col + 2] == 'K')
  70. {
  71. strikes++;
  72. }
  73.  
  74.  
  75. if (isInRange(chessBoard, row + 1, col - 2) && chessBoard[row +1, col - 2] == 'K')
  76. {
  77. strikes++;
  78. }
  79.  
  80. if (isInRange(chessBoard, row - 1, col + 2) && chessBoard[row - 1 , col + 2] == 'K')
  81. {
  82. strikes++;
  83. }
  84.  
  85. if (isInRange(chessBoard, row - 1, col - 2) && chessBoard[row - 1, col - 2] == 'K')
  86. {
  87. strikes++;
  88. }
  89.  
  90. if (isInRange(chessBoard, row + 2, col - 1) && chessBoard[row + 2, col - 1] == 'K')
  91. {
  92. strikes++;
  93. }
  94.  
  95.  
  96. if (isInRange(chessBoard, row + 2, col + 1) && chessBoard[row + 2, col + 1] == 'K')
  97. {
  98. strikes++;
  99. }
  100.  
  101. return strikes;
  102. }
  103.  
  104. private static void FillTheMatrix(char[,] matrix)
  105. {
  106. for (int row = 0; row < matrix.GetLength(0); row++)
  107. {
  108. char[] currentRow = Console.ReadLine().ToCharArray();
  109.  
  110. for (int col = 0; col < matrix.GetLength(1); col++)
  111. {
  112. matrix[col, row] = currentRow[col];
  113. }
  114. }
  115. }
  116. private static bool isInRange(char[,] matrix, int targetRow, int targetCol)
  117.  
  118. => targetRow >= 0 && targetRow < matrix.GetLength(0) && targetCol >= 0 && targetCol < matrix.GetLength(1);
  119. }
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment