Advertisement
social1986

Untitled

Dec 14th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Problem_2___Knight_Game
  4. {
  5. public class Startup
  6. {
  7. public static void Main()
  8. {
  9. var board = FillingBoard();
  10. var countOverlapping = 0;
  11. var maxCounter = 0;
  12. var maxRow = 0;
  13. var maxCol = 0;
  14. var countKnightsToBeRemoved = 0;
  15. var isThereOverlapping = false;
  16.  
  17. while (true)
  18. {
  19. for (int row = 0; row < board.Length; row++)
  20. {
  21. for (int col = 0; col < board[row].Length; col++)
  22. {
  23. if (board[row][col] == 'K')
  24. {
  25. countOverlapping = CountingOverlapping(board, row, col);
  26.  
  27. if (countOverlapping > maxCounter)
  28. {
  29. isThereOverlapping = true;
  30. maxCounter = countOverlapping;
  31. maxRow = row;
  32. maxCol = col;
  33. }
  34. }
  35. }
  36. }
  37.  
  38. if (isThereOverlapping)
  39. {
  40. countKnightsToBeRemoved++;
  41. board[maxRow][maxCol] = '0';
  42. isThereOverlapping = false;
  43. maxCounter = 0;
  44. }
  45. else
  46. {
  47. break;
  48. }
  49. }
  50. Console.WriteLine(countKnightsToBeRemoved);
  51. }
  52.  
  53. public static char[][] FillingBoard()
  54. {
  55. var n = int.Parse(Console.ReadLine());
  56. var matrix = new char[n][];
  57.  
  58. for (int i = 0; i < matrix.Length; i++)
  59. {
  60. matrix[i] = Console.ReadLine().ToCharArray();
  61. }
  62. return matrix;
  63. }
  64.  
  65. public static int CountingOverlapping(char[][] board, int row, int col)
  66. {
  67. int counter = 0;
  68.  
  69. int[][] knightMoves =
  70. {
  71. new int[] { row - 1, col - 2 },
  72. new int[] { row - 1, col + 2 },
  73. new int[] { row + 1, col + 2 },
  74. new int[] { row + 1, col - 2 },
  75. new int[] { row - 2, col - 1 },
  76. new int[] { row - 2, col + 1 },
  77. new int[] { row + 2, col + 1 },
  78. new int[] { row + 2, col - 1 }
  79. };
  80.  
  81. for (int i = 0; i < knightMoves.Length; i++)
  82. {
  83. var currentRow = knightMoves[i][0];
  84. var currentCol = knightMoves[i][1];
  85.  
  86. if (IsOnBoard(board, knightMoves[i]) && board[currentRow][currentCol] == 'K')
  87. {
  88. counter++;
  89. }
  90. }
  91. return counter;
  92. }
  93.  
  94. public static bool IsOnBoard(char[][] matrix, int[] knightMoves)
  95. {
  96. var row = knightMoves[0];
  97. var col = knightMoves[1];
  98.  
  99. if (row < 0 || row > matrix.Length - 1 || col < 0 || col > matrix[0].Length - 1)
  100. {
  101. return false;
  102. }
  103. return true;
  104. }
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement