Advertisement
gabi11

Multidimensional Arrays - 07. Knight Game

May 13th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace Advanced
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int boardSize = int.Parse(Console.ReadLine());
  14.  
  15.             int rows = boardSize;
  16.             int cols = boardSize;
  17.  
  18.             var matrix = new char[rows][];
  19.  
  20.             for (int row = 0; row < rows; row++)
  21.             {
  22.                 matrix[row] = Console.ReadLine().ToCharArray();
  23.             }
  24.  
  25.             int removedHorses = 0;
  26.  
  27.             while (true)
  28.             {
  29.                 int knightRow = -1;
  30.                 int knightCol = -1;
  31.                 int maxAttacked = 0;
  32.  
  33.                 for (int row = 0; row < rows; row++)
  34.                 {
  35.                     for (int col = 0; col < cols; col++)
  36.                     {
  37.                         if (matrix[row][col] == 'K')
  38.                         {
  39.                             int tempAttack = CountAttacks(matrix, row, col);
  40.  
  41.                             if (tempAttack > maxAttacked)
  42.                             {
  43.                                 maxAttacked = tempAttack;
  44.                                 knightRow = row;
  45.                                 knightCol = col;
  46.                             }
  47.  
  48.                         }
  49.                     }
  50.                 }
  51.  
  52.                 if (maxAttacked > 0)
  53.                 {
  54.                     matrix[knightRow][knightCol] = '0';
  55.                     removedHorses++;
  56.                 }
  57.  
  58.                 else
  59.                 {
  60.                     break;
  61.                 }
  62.             }
  63.             Console.WriteLine(removedHorses);
  64.         }
  65.  
  66.         private static int CountAttacks(char[][] matrix, int row, int col)
  67.         {
  68.             int attacks = 0;
  69.  
  70.             if (IsInMatrix(row - 1, col - 2, matrix.Length) && matrix[row - 1][col - 2] == 'K')
  71.             {
  72.                 attacks++;
  73.             }
  74.  
  75.             if (IsInMatrix(row - 1, col + 2, matrix.Length) && matrix[row - 1][col + 2] == 'K')
  76.             {
  77.                 attacks++;
  78.             }
  79.  
  80.             if (IsInMatrix(row + 1, col - 2, matrix.Length) && matrix[row + 1][col - 2] == 'K')
  81.             {
  82.                 attacks++;
  83.             }
  84.  
  85.             if (IsInMatrix(row + 1, col + 2, matrix.Length) && matrix[row + 1][col + 2] == 'K')
  86.             {
  87.                 attacks++;
  88.             }
  89.  
  90.             if (IsInMatrix(row - 2, col - 1, matrix.Length) && matrix[row - 2][col - 1] == 'K')
  91.             {
  92.                 attacks++;
  93.             }
  94.  
  95.             if (IsInMatrix(row - 2, col + 1, matrix.Length) && matrix[row - 2][col + 1] == 'K')
  96.             {
  97.                 attacks++;
  98.             }
  99.  
  100.             if (IsInMatrix(row + 2, col - 1, matrix.Length) && matrix[row + 2][col - 1] == 'K')
  101.             {
  102.                 attacks++;
  103.             }
  104.  
  105.             if (IsInMatrix(row + 2, col + 1, matrix.Length) && matrix[row + 2][col + 1] == 'K')
  106.             {
  107.                 attacks++;
  108.             }
  109.  
  110.             return attacks;
  111.         }
  112.  
  113.         private static bool IsInMatrix(int row, int col, int lenght)
  114.         {
  115.             return row >= 0 && row < lenght && col >= 0 && col < lenght;
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement