Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Advanced
- {
- class Program
- {
- static void Main(string[] args)
- {
- int boardSize = int.Parse(Console.ReadLine());
- int rows = boardSize;
- int cols = boardSize;
- var matrix = new char[rows][];
- for (int row = 0; row < rows; row++)
- {
- matrix[row] = Console.ReadLine().ToCharArray();
- }
- int removedHorses = 0;
- while (true)
- {
- int knightRow = -1;
- int knightCol = -1;
- int maxAttacked = 0;
- for (int row = 0; row < rows; row++)
- {
- for (int col = 0; col < cols; col++)
- {
- if (matrix[row][col] == 'K')
- {
- int tempAttack = CountAttacks(matrix, row, col);
- if (tempAttack > maxAttacked)
- {
- maxAttacked = tempAttack;
- knightRow = row;
- knightCol = col;
- }
- }
- }
- }
- if (maxAttacked > 0)
- {
- matrix[knightRow][knightCol] = '0';
- removedHorses++;
- }
- else
- {
- break;
- }
- }
- Console.WriteLine(removedHorses);
- }
- private static int CountAttacks(char[][] matrix, int row, int col)
- {
- int attacks = 0;
- if (IsInMatrix(row - 1, col - 2, matrix.Length) && matrix[row - 1][col - 2] == 'K')
- {
- attacks++;
- }
- if (IsInMatrix(row - 1, col + 2, matrix.Length) && matrix[row - 1][col + 2] == 'K')
- {
- attacks++;
- }
- if (IsInMatrix(row + 1, col - 2, matrix.Length) && matrix[row + 1][col - 2] == 'K')
- {
- attacks++;
- }
- if (IsInMatrix(row + 1, col + 2, matrix.Length) && matrix[row + 1][col + 2] == 'K')
- {
- attacks++;
- }
- if (IsInMatrix(row - 2, col - 1, matrix.Length) && matrix[row - 2][col - 1] == 'K')
- {
- attacks++;
- }
- if (IsInMatrix(row - 2, col + 1, matrix.Length) && matrix[row - 2][col + 1] == 'K')
- {
- attacks++;
- }
- if (IsInMatrix(row + 2, col - 1, matrix.Length) && matrix[row + 2][col - 1] == 'K')
- {
- attacks++;
- }
- if (IsInMatrix(row + 2, col + 1, matrix.Length) && matrix[row + 2][col + 1] == 'K')
- {
- attacks++;
- }
- return attacks;
- }
- private static bool IsInMatrix(int row, int col, int lenght)
- {
- return row >= 0 && row < lenght && col >= 0 && col < lenght;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement