Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _02.Knight_Game
- {
- class Program
- {
- static void Main(string[] args)
- {
- int matrixLength = int.Parse(Console.ReadLine());
- char[][] matrix = new char[matrixLength][];
- ReadMatrix(matrix, matrixLength);
- var dict = new Dictionary<string, int>();
- int countRemovetKnight = 0;
- while (true)
- {
- for (int row = 0; row < matrix.Length; row++)
- {
- for (int coll = 0; coll < matrix[row].Length; coll++)
- {
- if (matrix[row][coll] == 'K')
- {
- CheckPosition(dict, row, coll, matrix);
- }
- }
- }
- string maxPosition = "";
- foreach (var item in dict.Where(x => x.Value != 0).OrderByDescending(x => x.Value))
- {
- maxPosition = item.Key;
- break;
- }
- if (maxPosition != "")
- {
- countRemovetKnight++;
- dict.Remove(maxPosition);
- int[] rowAndColl = maxPosition.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
- matrix[rowAndColl[0]][rowAndColl[1]] = '0';
- }
- else
- {
- Console.WriteLine(countRemovetKnight);
- return;
- }
- }
- }
- private static void ReadMatrix(char[][] matrix, int matrixLength)
- {
- for (int row = 0; row < matrixLength; row++)
- {
- matrix[row] = new char[matrixLength];
- matrix[row] = Console.ReadLine().ToCharArray();
- }
- }
- private static void CheckPosition(Dictionary<string, int> dict, int row, int coll, char[][] matrix)
- {
- int count = 0;
- if (row - 2 >= 0 && coll - 1 >= 0)
- {
- if (matrix[row - 2][coll - 1] == 'K')
- {
- count++;
- }
- }
- if (row - 1 >= 0 && coll - 2 >= 0)
- {
- if (matrix[row - 1][coll - 2] == 'K')
- {
- count++;
- }
- }
- if (row + 1 < matrix.Length && coll - 2 >= 0)
- {
- if (matrix[row + 1][coll - 2] == 'K')
- {
- count++;
- }
- }
- if (row + 2 < matrix.Length && coll - 1 >= 0)
- {
- if (matrix[row + 2][coll - 1] == 'K')
- {
- count++;
- }
- }
- if (row - 2 >= 0 && coll + 1 < matrix.Length)
- {
- if (matrix[row - 2][coll + 1] == 'K')
- {
- count++;
- }
- }
- if (row - 1 >= 0 && coll + 2 < matrix.Length)
- {
- if (matrix[row - 1][coll + 2] == 'K')
- {
- count++;
- }
- }
- if (row + 1 < matrix.Length && coll + 2 < matrix.Length)
- {
- if (matrix[row + 1][coll + 2] == 'K')
- {
- count++;
- }
- }
- if (row + 2 < matrix.Length && coll + 1 < matrix.Length)
- {
- if (matrix[row + 2][coll + 1] == 'K')
- {
- count++;
- }
- }
- string position = $"{row} {coll}";
- if (count != 0)
- {
- if (dict.ContainsKey(position))
- {
- dict[position] = count;
- }
- else
- {
- dict.Add(position, count);
- }
- }
- else if(dict.ContainsKey(position))
- {
- dict[position] = count;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment