Advertisement
Guest User

Untitled

a guest
Aug 29th, 2021
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.23 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace Knight_Game
  6. {
  7.     class Program
  8.     {
  9.  
  10.  
  11.         static void Main(string[] args)
  12.         {
  13.             int n = int.Parse(Console.ReadLine());
  14.             char[][] matrix = new char[n][];
  15.  
  16.             for (int row = 0; row < n; row++)
  17.             {
  18.                 matrix[row] = Console.ReadLine().ToLower().ToCharArray(); // Тук
  19.             }
  20.             int removeKnight = 0;
  21.             while (true)
  22.             {
  23.                 int knightRow = -1;  //za da zapocha izvyn igrishteto
  24.                 int knightCol = -1;
  25.                 int maxAtacked = 0;
  26.  
  27.                 for (int row = 0; row < n; row++)
  28.                 {
  29.                     for (int col = 0; col < n; col++)
  30.                     {
  31.                         if (matrix[row][col] == 'k')
  32.                         {
  33.                             int tempAtack = CountAtacks(matrix, row, col);
  34.  
  35.                             if (tempAtack > maxAtacked)
  36.                             {
  37.                                 maxAtacked = tempAtack;
  38.                                 knightRow = row;
  39.                                 knightCol = col;
  40.                             }
  41.                         }
  42.                     }
  43.  
  44.                 }
  45.  
  46.                 if (maxAtacked > 0)
  47.                 {
  48.                     matrix[knightRow][knightCol] = '0';
  49.                     removeKnight++;
  50.                 }
  51.  
  52.                 else
  53.                 {
  54.                     break;
  55.                 }
  56.  
  57.             }
  58.  
  59.             Console.WriteLine(removeKnight);
  60.         }
  61.  
  62.         static int CountAtacks(char[][] matrix, int row, int col)
  63.         {
  64.             int atacks = 0; //колко атаки ще направя
  65.  
  66.             if (IsExist(row + 1, col - 2, matrix.Length) && matrix[row + 1][col - 2] == 'k')
  67.             {
  68.                 atacks++;
  69.             }
  70.  
  71.             if (IsExist(row - 1, col - 2, matrix.Length) && matrix[row - 1][col - 2] == 'k')
  72.             {
  73.                 atacks++;
  74.             }
  75.  
  76.             if (IsExist(row - 1, col + 2, matrix.Length) && matrix[row - 1][col + 2] == 'k')
  77.             {
  78.                 atacks++;
  79.             }
  80.             if (IsExist(row + 1, col + 2, matrix.Length) && matrix[row + 1][col + 2] == 'k')
  81.             {
  82.                 atacks++;
  83.             }
  84.             if (IsExist(row - 2, col - 1, matrix.Length) && matrix[row - 2][col - 1] == 'k')
  85.             {
  86.                 atacks++;
  87.             }
  88.             if (IsExist(row - 2, col + 1, matrix.Length) && matrix[row - 2][col + 1] == 'k')
  89.             {
  90.                 atacks++;
  91.             }
  92.             if (IsExist(row + 2, col - 1, matrix.Length) && matrix[row + 2][col - 1] == 'k')
  93.             {
  94.                 atacks++;
  95.             }
  96.             if (IsExist(row + 2, col + 1, matrix.Length) && matrix[row + 2][col + 1] == 'k')
  97.             {
  98.                 atacks++;
  99.             }
  100.             return atacks;
  101.  
  102.         }
  103.  
  104.         private static bool IsExist(int row, int col, int lenght)
  105.         {
  106.             return row >= 0 && row < lenght && col >= 0 && col < lenght; ///проверка  дали е в полето
  107.         }
  108.     }
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement