using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Problem_2___Knight_Game { public class Startup { public static void Main() { var board = FillingBoard(); var countOverlapping = 0; var maxCounter = 0; var maxRow = 0; var maxCol = 0; var countKnightsToBeRemoved = 0; var isThereOverlapping = false; while (true) { for (int row = 0; row < board.Length; row++) { for (int col = 0; col < board[row].Length; col++) { if (board[row][col] == 'K') { countOverlapping = CountingOverlapping(board, row, col); if (countOverlapping > maxCounter) { isThereOverlapping = true; maxCounter = countOverlapping; maxRow = row; maxCol = col; } } } } if (isThereOverlapping) { countKnightsToBeRemoved++; board[maxRow][maxCol] = '0'; isThereOverlapping = false; maxCounter = 0; } else { break; } } Console.WriteLine(countKnightsToBeRemoved); } public static char[][] FillingBoard() { var n = int.Parse(Console.ReadLine()); var matrix = new char[n][]; for (int i = 0; i < matrix.Length; i++) { matrix[i] = Console.ReadLine().ToCharArray(); } return matrix; } public static int CountingOverlapping(char[][] board, int row, int col) { int counter = 0; int[][] knightMoves = { new int[] { row - 1, col - 2 }, new int[] { row - 1, col + 2 }, new int[] { row + 1, col + 2 }, new int[] { row + 1, col - 2 }, new int[] { row - 2, col - 1 }, new int[] { row - 2, col + 1 }, new int[] { row + 2, col + 1 }, new int[] { row + 2, col - 1 } }; for (int i = 0; i < knightMoves.Length; i++) { var currentRow = knightMoves[i][0]; var currentCol = knightMoves[i][1]; if (IsOnBoard(board, knightMoves[i]) && board[currentRow][currentCol] == 'K') { counter++; } } return counter; } public static bool IsOnBoard(char[][] matrix, int[] knightMoves) { var row = knightMoves[0]; var col = knightMoves[1]; if (row < 0 || row > matrix.Length - 1 || col < 0 || col > matrix[0].Length - 1) { return false; } return true; } } }