Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace KnightGame
- {
- public class StartUp
- {
- public static void Main()
- {
- int boardSize = int.Parse(Console.ReadLine());
- char[,] board = new char[boardSize, boardSize];
- var indexes = new Dictionary<List<int>, int>();
- int removeCounter = 0;
- for (int i = 0; i < board.GetLength(0); i++)
- {
- char[] rows = Console.ReadLine().ToCharArray();
- for (int j = 0; j < board.GetLength(1); j++)
- {
- board[i, j] = rows[j];
- }
- }
- while (true)
- {
- SetIndexes(indexes, board);
- if (indexes.Values.Any(x => x != 0))
- {
- RemoveKnight(indexes, board);
- removeCounter++;
- indexes.Clear();
- }
- else
- {
- break;
- }
- }
- Console.WriteLine(removeCounter);
- }
- private static void RemoveKnight(Dictionary<List<int>, int> indexes, char[,] board)
- {
- int max = indexes.Values.Max();
- var coordinatesForRemoval = indexes.FirstOrDefault(x => x.Value == max).Key;
- board[coordinatesForRemoval[0], coordinatesForRemoval[1]] = '0';
- }
- private static void SetIndexes(Dictionary<List<int>, int> indexes, char[,] board)
- {
- int counter = 0;
- for (int i = 0; i < board.GetLength(0); i++)
- {
- for (int j = 0; j < board.GetLength(1); j++)
- {
- if (board[i, j] == 'K')
- {
- int[] coordinates = { i, j };
- counter = CountKnights(board, i, j, board.GetLength(0));
- indexes.Add(new List<int>(coordinates), counter);
- }
- }
- }
- }
- private static int CountKnights(char[,] board, int i, int j, int length)
- {
- bool isThereAKnight = false;
- int counter = 0;
- if (i + 2 < length && j + 1 < length)
- {
- isThereAKnight = CheckForKnight(board[i + 2, j + 1]);
- counter = SetCounter(isThereAKnight, counter);
- }
- if (i + 2 < length && j - 1 >= 0)
- {
- isThereAKnight = CheckForKnight(board[i + 2, j - 1]);
- counter = SetCounter(isThereAKnight, counter);
- }
- if (i - 2 >= 0 && j + 1 < length)
- {
- isThereAKnight = CheckForKnight(board[i - 2, j + 1]);
- counter = SetCounter(isThereAKnight, counter);
- }
- if (i - 2 >= 0 && j - 1 >= 0)
- {
- isThereAKnight = CheckForKnight(board[i - 2, j - 1]);
- counter = SetCounter(isThereAKnight, counter);
- }
- if (j + 2 < length && i + 1 < length)
- {
- isThereAKnight = CheckForKnight(board[i + 1, j + 2]);
- counter = SetCounter(isThereAKnight, counter);
- }
- if (j + 2 < length && i - 1 >= 0)
- {
- isThereAKnight = CheckForKnight(board[i - 1, j + 2]);
- counter = SetCounter(isThereAKnight, counter);
- }
- if (j - 2 >= 0 && i + 1 < length)
- {
- isThereAKnight = CheckForKnight(board[i + 1, j - 2]);
- counter = SetCounter(isThereAKnight, counter);
- }
- if (j - 2 >= 0 && i - 1 >= 0)
- {
- isThereAKnight = CheckForKnight(board[i - 1, j - 2]);
- counter = SetCounter(isThereAKnight, counter);
- }
- return counter;
- }
- private static int SetCounter(bool isThereAKnight, int counter)
- {
- if (isThereAKnight)
- {
- counter++;
- }
- return counter;
- }
- private static bool CheckForKnight(char element)
- {
- if (element == 'K')
- {
- return true;
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment