Advertisement
ralichka

07.KnightGame

Jan 24th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.08 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _07.KnightGame
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int size = int.Parse(Console.ReadLine());
  11.  
  12.             char[,] matrix = new char[size, size];
  13.  
  14.             for (int row = 0; row < size; row++)
  15.             {
  16.                 char[] input = Console.ReadLine().ToCharArray();
  17.  
  18.                 for (int col = 0; col < size; col++)
  19.                 {
  20.                     matrix[row, col] = input[col];
  21.                 }
  22.             }
  23.  
  24.             int maxAttacks = 0;
  25.             int KillerRow = 0;
  26.             int KillerCol = 0;
  27.             int removedKnights = 0;
  28.  
  29.             while (true)
  30.             {
  31.                 for (int row = 0; row < size; row++)
  32.                 {
  33.                     for (int col = 0; col < size; col++)
  34.                     {
  35.                         int currentAttacks = 0;
  36.  
  37.                         if (matrix[row, col] == 'K')
  38.                         {
  39.                             //arr[row+1,col+2]
  40.                             if (Inside(matrix, row+1, col+2) && matrix[row + 1, col + 2] == 'K')
  41.                             {
  42.                                 currentAttacks++;
  43.                             }
  44.                             //arr[row+1,col-2]
  45.                             if (Inside(matrix, row+1, col-2) && matrix[row + 1, col - 2] == 'K')
  46.                             {
  47.                                 currentAttacks++;
  48.                             }
  49.                             //arr[row+2,col+1]
  50.                             if (Inside(matrix, row+2, col+1) && matrix[row + 2, col + 1] == 'K')
  51.                             {
  52.                                 currentAttacks++;
  53.                             }
  54.                             //arr[row+2,col-1]
  55.                             if (Inside(matrix, row+2, col-1) && matrix[row + 2, col - 1] == 'K')
  56.                             {
  57.                                 currentAttacks++;
  58.                             }
  59.                             //arr[row-2,col-1]
  60.                             if (Inside(matrix, row-2, col-1) && matrix[row - 2, col - 1] == 'K')
  61.                             {
  62.                                 currentAttacks++;
  63.                             }
  64.                             //arr[row-1,col-2]
  65.                             if (Inside(matrix, row-1, col-2) && matrix[row - 1, col - 2] == 'K')
  66.                             {
  67.                                 currentAttacks++;
  68.                             }
  69.                             //arr[row-1,col+2]
  70.                             if (Inside(matrix, row-1, col+2) && matrix[row - 1, col + 2] == 'K')
  71.                             {
  72.                                 currentAttacks++;
  73.                             }
  74.                             //arr[row-2,col+1]
  75.                             if (Inside(matrix, row-2, col+1) && matrix[row - 2, col + 1] == 'K')
  76.                             {
  77.                                 currentAttacks++;
  78.                             }
  79.                         }
  80.                         if (currentAttacks > maxAttacks)
  81.                         {
  82.                             maxAttacks = currentAttacks;
  83.                             KillerRow = row;
  84.                             KillerCol = col;
  85.                         }
  86.                     }
  87.                 }
  88.                 // ако имаме кон, който атакува, трябва да го премахнем и задължително зануляваме maxAttacsa!!!
  89.                 if (maxAttacks > 0)
  90.                 {
  91.                     matrix[KillerRow, KillerCol] = '0';
  92.                     removedKnights++;
  93.                     maxAttacks = 0;
  94.                 }
  95.                 else
  96.                 {
  97.                     Console.WriteLine(removedKnights);
  98.                     break;
  99.                 }
  100.             }
  101.         }
  102.         private static bool Inside(char[,] matrix, int row, int col)
  103.         {
  104.             return row >= 0 && row < matrix.GetLength(0) && col >= 0 && col < matrix.GetLength(1);
  105.  
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement