Advertisement
YORDAN2347

07. Knight Game

Feb 12th, 2021
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.48 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _07._KnightGame
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int n = int.Parse(Console.ReadLine());
  10.             char[,] board = new char[n, n];
  11.  
  12.             // 1. read TheBoard
  13.  
  14.             for (int row = 0; row < board.GetLength(0); row++)
  15.             {
  16.                 string rowData = Console.ReadLine();
  17.                 for (int col = 0; col < board.GetLength(1); col++)
  18.                 {
  19.                     board[row, col] = rowData[col];
  20.                 }
  21.             }
  22.  
  23.             int removedHorses = 0;
  24.             while (true)
  25.             {
  26.                 int maxAtacksNumber = -1;
  27.                 int maxRow = -1;
  28.                 int maxCol = -1;
  29.                 // 2. find Horse
  30.                 for (int row = 0; row < board.GetLength(0); row++)
  31.                 {
  32.                     for (int col = 0; col < board.GetLength(1); col++)
  33.                     {
  34.                         int atacksNumber = 0;
  35.                         if (board[row, col] == 'K')// horse is found
  36.                         {
  37.                             //checkAtacksInFront
  38.                             atacksNumber += CheckAttacksInFront(board, row, col);
  39.  
  40.                             //checkAtacksBehind
  41.                             atacksNumber += CheckAttacksBehind(board, row, col);
  42.  
  43.                             //checkAtacksOnLeft
  44.                             atacksNumber += CheckAttacksOnLeft(board, row, col);
  45.  
  46.                             //checkAtacksOnRight
  47.                             atacksNumber += CheckAttacksOnRight(board, row, col);
  48.                         }
  49.                         if (atacksNumber > maxAtacksNumber) // set horse to remove
  50.                         {
  51.                             maxAtacksNumber = atacksNumber;
  52.                             maxRow = row;
  53.                             maxCol = col;
  54.                         }
  55.                     }
  56.                 }
  57.                 if (maxAtacksNumber > 0) // remove horse with most attacks
  58.                 {
  59.                     board[maxRow, maxCol] = '0';
  60.                     removedHorses++;
  61.                 }
  62.                 else // if no horse to remove, end
  63.                 {
  64.                     break;
  65.                 }
  66.  
  67.             } // end of while
  68.             Console.WriteLine(removedHorses);
  69.         } // end of main
  70.  
  71.         private static int CheckAttacksOnRight(char[,] board, int row, int col)
  72.         {
  73.             int attackNumbers = 0;
  74.             if (CanAttack(board, row - 1, col + 2))
  75.             {
  76.                 attackNumbers++;
  77.             }
  78.             if (CanAttack(board, row + 1, col + 2))
  79.             {
  80.                 attackNumbers++;
  81.             }
  82.             return attackNumbers;
  83.         }
  84.  
  85.         private static int CheckAttacksOnLeft(char[,] board, int row, int col)
  86.         {
  87.             int attackNumbers = 0;
  88.             if (CanAttack(board, row - 1, col - 2))
  89.             {
  90.                 attackNumbers++;
  91.             }
  92.             if (CanAttack(board, row + 1, col - 2))
  93.             {
  94.                 attackNumbers++;
  95.             }
  96.             return attackNumbers;
  97.         }
  98.  
  99.         private static int CheckAttacksBehind(char[,] board, int row, int col)
  100.         {
  101.             int attackNumbers = 0;
  102.             if (CanAttack(board, row + 2, col - 1))
  103.             {
  104.                 attackNumbers++;
  105.             }
  106.             if (CanAttack(board, row + 2, col + 1))
  107.             {
  108.                 attackNumbers++;
  109.             }
  110.             return attackNumbers;
  111.         }
  112.  
  113.         private static int CheckAttacksInFront(char[,] board, int row, int col)
  114.         {
  115.             int attackNumbers = 0;
  116.             if (CanAttack(board, row - 2, col - 1))
  117.             {
  118.                 attackNumbers++;
  119.             }
  120.             if (CanAttack(board, row - 2, col + 1))
  121.             {
  122.                 attackNumbers++;
  123.             }
  124.             return attackNumbers;
  125.         }
  126.  
  127.         private static bool CanAttack(char[,] board, int row, int col)
  128.         {
  129.             if (isInCell(board, row, col))
  130.             {
  131.                 if (board[row, col] == 'K')
  132.                 {
  133.                     return true;
  134.                 }
  135.             }
  136.             return false;
  137.         }
  138.  
  139.         private static bool isInCell(char[,] board, int row, int col)
  140.         {
  141.             return row >= 0 && row < board.GetLength(0) && col >= 0 && col < board.GetLength(1);
  142.         }
  143.     }
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement