Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace _07._KnightGame
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- char[,] board = new char[n, n];
- // 1. read TheBoard
- for (int row = 0; row < board.GetLength(0); row++)
- {
- string rowData = Console.ReadLine();
- for (int col = 0; col < board.GetLength(1); col++)
- {
- board[row, col] = rowData[col];
- }
- }
- int removedHorses = 0;
- while (true)
- {
- int maxAtacksNumber = -1;
- int maxRow = -1;
- int maxCol = -1;
- // 2. find Horse
- for (int row = 0; row < board.GetLength(0); row++)
- {
- for (int col = 0; col < board.GetLength(1); col++)
- {
- int atacksNumber = 0;
- if (board[row, col] == 'K')// horse is found
- {
- //checkAtacksInFront
- atacksNumber += CheckAttacksInFront(board, row, col);
- //checkAtacksBehind
- atacksNumber += CheckAttacksBehind(board, row, col);
- //checkAtacksOnLeft
- atacksNumber += CheckAttacksOnLeft(board, row, col);
- //checkAtacksOnRight
- atacksNumber += CheckAttacksOnRight(board, row, col);
- }
- if (atacksNumber > maxAtacksNumber) // set horse to remove
- {
- maxAtacksNumber = atacksNumber;
- maxRow = row;
- maxCol = col;
- }
- }
- }
- if (maxAtacksNumber > 0) // remove horse with most attacks
- {
- board[maxRow, maxCol] = '0';
- removedHorses++;
- }
- else // if no horse to remove, end
- {
- break;
- }
- } // end of while
- Console.WriteLine(removedHorses);
- } // end of main
- private static int CheckAttacksOnRight(char[,] board, int row, int col)
- {
- int attackNumbers = 0;
- if (CanAttack(board, row - 1, col + 2))
- {
- attackNumbers++;
- }
- if (CanAttack(board, row + 1, col + 2))
- {
- attackNumbers++;
- }
- return attackNumbers;
- }
- private static int CheckAttacksOnLeft(char[,] board, int row, int col)
- {
- int attackNumbers = 0;
- if (CanAttack(board, row - 1, col - 2))
- {
- attackNumbers++;
- }
- if (CanAttack(board, row + 1, col - 2))
- {
- attackNumbers++;
- }
- return attackNumbers;
- }
- private static int CheckAttacksBehind(char[,] board, int row, int col)
- {
- int attackNumbers = 0;
- if (CanAttack(board, row + 2, col - 1))
- {
- attackNumbers++;
- }
- if (CanAttack(board, row + 2, col + 1))
- {
- attackNumbers++;
- }
- return attackNumbers;
- }
- private static int CheckAttacksInFront(char[,] board, int row, int col)
- {
- int attackNumbers = 0;
- if (CanAttack(board, row - 2, col - 1))
- {
- attackNumbers++;
- }
- if (CanAttack(board, row - 2, col + 1))
- {
- attackNumbers++;
- }
- return attackNumbers;
- }
- private static bool CanAttack(char[,] board, int row, int col)
- {
- if (isInCell(board, row, col))
- {
- if (board[row, col] == 'K')
- {
- return true;
- }
- }
- return false;
- }
- private static bool isInCell(char[,] board, int row, int col)
- {
- return row >= 0 && row < board.GetLength(0) && col >= 0 && col < board.GetLength(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement