Advertisement
MilenaPetkanova

02-Knight-Game-25062017

Feb 8th, 2018
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. using System;
  2.  
  3. class KnightGame
  4. {
  5. static void Main()
  6. {
  7. int n = int.Parse(Console.ReadLine());
  8.  
  9. var board = new char[n][];
  10. for (int i = 0; i < n; i++)
  11. {
  12. board[i] = Console.ReadLine().ToCharArray();
  13. }
  14.  
  15. int maxAttacked = 0;
  16. int maxRow = 0;
  17. int maxColumn = 0;
  18. int countOfRemovedKnights = 0;
  19.  
  20. do
  21. {
  22. if (maxAttacked > 0)
  23. {
  24. board[maxColumn][maxColumn] = '0';
  25. maxAttacked = 0;
  26. countOfRemovedKnights++;
  27. }
  28.  
  29. int currentAttacks = 0;
  30.  
  31. for (int row = 0; row < n; row++)
  32. {
  33. for (int column = 0; column < n; column++)
  34. {
  35. if (board[row][column] == 'K')
  36. {
  37. currentAttacks = CalculateAttacks(row, column, board);
  38.  
  39. if (currentAttacks > maxAttacked)
  40. {
  41. maxAttacked = currentAttacks;
  42. maxRow = row;
  43. maxColumn = column;
  44. }
  45. }
  46. }
  47. }
  48. }
  49. while (maxAttacked > 0);
  50.  
  51. Console.WriteLine(countOfRemovedKnights);
  52. }
  53.  
  54. private static int CalculateAttacks(int row, int column, char[][] board)
  55. {
  56. int result = 0;
  57.  
  58. if (IsPositionAttacked(row - 2, column - 1, board)) result++;
  59. if (IsPositionAttacked(row - 2, column + 1, board)) result++;
  60. if (IsPositionAttacked(row - 1, column - 2, board)) result++;
  61. if (IsPositionAttacked(row - 1, column + 2, board)) result++;
  62. if (IsPositionAttacked(row + 1, column - 2, board)) result++;
  63. if (IsPositionAttacked(row + 1, column + 2, board)) result++;
  64. if (IsPositionAttacked(row + 2, column - 1, board)) result++;
  65. if (IsPositionAttacked(row + 2, column + 1, board)) result++;
  66.  
  67. return result;
  68. }
  69.  
  70. private static bool IsPositionAttacked(int row, int column, char[][] board)
  71. {
  72. return IsOnChessBoard(row, column, board[0].Length)
  73. && board[row][column] == 'K';
  74. }
  75.  
  76. private static bool IsOnChessBoard(int row, int column, int n)
  77. {
  78. return (row >= 0 && row < n && column >= 0 && column < n);
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement