Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Reflection.Metadata;
- namespace Knight_Game
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- char[,] chessBoard = new char[n, n];
- FillTheMatrix(chessBoard);
- int removedKnights = 0;
- int forceRow = 0;
- int forceCol = 0;
- while (true)
- {
- int maxStrikes = 0;
- for (int row = 0; row < chessBoard.GetLength(0); row++)
- {
- for (int col = 0; col < chessBoard.GetLength(1); col++)
- {
- char currentSymbol = chessBoard[row, col];
- int strikes = 0;
- if (currentSymbol == 'K')
- {
- strikes = allStrikePositions(chessBoard, row, col, strikes);
- if (strikes>maxStrikes)
- {
- maxStrikes = strikes;
- forceRow = row;
- forceCol = col;
- }
- }
- }
- }
- if (maxStrikes>0)
- {
- chessBoard[forceRow, forceCol] = '0';
- removedKnights++;
- }
- else
- {
- Console.WriteLine(removedKnights);
- break;
- }
- }
- }
- private static int allStrikePositions(char[,] chessBoard, int row, int col, int strikes)
- {
- if (isInRange(chessBoard, row - 2, col + 1) && chessBoard[row - 2, col + 1] == 'K')
- {
- strikes++;
- }
- if (isInRange(chessBoard, row - 2, col - 1) && chessBoard[row - 2, col - 1] == 'K')
- {
- strikes++;
- }
- if (isInRange(chessBoard, row + 1, col + 2) && chessBoard[row +1, col + 2] == 'K')
- {
- strikes++;
- }
- if (isInRange(chessBoard, row + 1, col - 2) && chessBoard[row +1, col - 2] == 'K')
- {
- strikes++;
- }
- if (isInRange(chessBoard, row - 1, col + 2) && chessBoard[row - 1 , col + 2] == 'K')
- {
- strikes++;
- }
- if (isInRange(chessBoard, row - 1, col - 2) && chessBoard[row - 1, col - 2] == 'K')
- {
- strikes++;
- }
- if (isInRange(chessBoard, row + 2, col - 1) && chessBoard[row + 2, col - 1] == 'K')
- {
- strikes++;
- }
- if (isInRange(chessBoard, row + 2, col + 1) && chessBoard[row + 2, col + 1] == 'K')
- {
- strikes++;
- }
- return strikes;
- }
- private static void FillTheMatrix(char[,] matrix)
- {
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- char[] currentRow = Console.ReadLine().ToCharArray();
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- matrix[col, row] = currentRow[col];
- }
- }
- }
- private static bool isInRange(char[,] matrix, int targetRow, int targetCol)
- => targetRow >= 0 && targetRow < matrix.GetLength(0) && targetCol >= 0 && targetCol < matrix.GetLength(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment