miroslavbstoyanov

Untitled

Feb 5th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 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 _02.Knight_Game
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. int matrixLength = int.Parse(Console.ReadLine());
  14. char[][] matrix = new char[matrixLength][];
  15. ReadMatrix(matrix, matrixLength);
  16.  
  17. var dict = new Dictionary<string, int>();
  18. int countRemovetKnight = 0;
  19.  
  20. while (true)
  21. {
  22. for (int row = 0; row < matrix.Length; row++)
  23. {
  24. for (int coll = 0; coll < matrix[row].Length; coll++)
  25. {
  26. if (matrix[row][coll] == 'K')
  27. {
  28. CheckPosition(dict, row, coll, matrix);
  29. }
  30. }
  31. }
  32.  
  33. string maxPosition = "";
  34. foreach (var item in dict.Where(x => x.Value != 0).OrderByDescending(x => x.Value))
  35. {
  36. maxPosition = item.Key;
  37. break;
  38. }
  39.  
  40. if (maxPosition != "")
  41. {
  42. countRemovetKnight++;
  43. dict.Remove(maxPosition);
  44. int[] rowAndColl = maxPosition.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  45. matrix[rowAndColl[0]][rowAndColl[1]] = '0';
  46. }
  47. else
  48. {
  49. Console.WriteLine(countRemovetKnight);
  50. return;
  51. }
  52. }
  53. }
  54.  
  55. private static void ReadMatrix(char[][] matrix, int matrixLength)
  56. {
  57.  
  58. for (int row = 0; row < matrixLength; row++)
  59. {
  60. matrix[row] = new char[matrixLength];
  61. matrix[row] = Console.ReadLine().ToCharArray();
  62. }
  63. }
  64.  
  65. private static void CheckPosition(Dictionary<string, int> dict, int row, int coll, char[][] matrix)
  66. {
  67. int count = 0;
  68.  
  69. if (row - 2 >= 0 && coll - 1 >= 0)
  70. {
  71. if (matrix[row - 2][coll - 1] == 'K')
  72. {
  73. count++;
  74. }
  75. }
  76. if (row - 1 >= 0 && coll - 2 >= 0)
  77. {
  78. if (matrix[row - 1][coll - 2] == 'K')
  79. {
  80. count++;
  81. }
  82. }
  83. if (row + 1 < matrix.Length && coll - 2 >= 0)
  84. {
  85. if (matrix[row + 1][coll - 2] == 'K')
  86. {
  87. count++;
  88. }
  89. }
  90. if (row + 2 < matrix.Length && coll - 1 >= 0)
  91. {
  92. if (matrix[row + 2][coll - 1] == 'K')
  93. {
  94. count++;
  95. }
  96. }
  97. if (row - 2 >= 0 && coll + 1 < matrix.Length)
  98. {
  99. if (matrix[row - 2][coll + 1] == 'K')
  100. {
  101. count++;
  102. }
  103. }
  104. if (row - 1 >= 0 && coll + 2 < matrix.Length)
  105. {
  106. if (matrix[row - 1][coll + 2] == 'K')
  107. {
  108. count++;
  109. }
  110. }
  111. if (row + 1 < matrix.Length && coll + 2 < matrix.Length)
  112. {
  113. if (matrix[row + 1][coll + 2] == 'K')
  114. {
  115. count++;
  116. }
  117. }
  118. if (row + 2 < matrix.Length && coll + 1 < matrix.Length)
  119. {
  120. if (matrix[row + 2][coll + 1] == 'K')
  121. {
  122. count++;
  123. }
  124. }
  125.  
  126. string position = $"{row} {coll}";
  127. if (count != 0)
  128. {
  129. if (dict.ContainsKey(position))
  130. {
  131. dict[position] = count;
  132. }
  133. else
  134. {
  135. dict.Add(position, count);
  136. }
  137. }
  138. else if(dict.ContainsKey(position))
  139. {
  140. dict[position] = count;
  141. }
  142.  
  143. }
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment