Advertisement
social1986

Untitled

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