Advertisement
Prohause

02 Knight Game

Oct 11th, 2018
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace _02._Knight_Game
  5. {
  6. internal class Program
  7. {
  8. private static void Main(string[] args)
  9. {
  10. var size = int.Parse(Console.ReadLine());
  11.  
  12. var manipulatedBoard = new char[size][];
  13.  
  14. for (int i = 0; i < size; i++)
  15. {
  16. manipulatedBoard[i] = Console.ReadLine().Trim().ToCharArray();
  17. }
  18.  
  19. var counter = 0;
  20.  
  21. while (true)
  22. {
  23. var conflict = false;
  24. var mostDangerous = 0;
  25. var location = new KeyValuePair<int, int>();
  26.  
  27. for (int i = 0; i < size; i++)
  28. {
  29. for (int j = 0; j < size; j++)
  30. {
  31. var currentEnemies = 0;
  32. if (manipulatedBoard[i][j].Equals('0'))
  33. {
  34. continue;
  35. }
  36.  
  37. try
  38. {
  39. if (manipulatedBoard[i - 2][j - 1].Equals('K'))
  40. {
  41. currentEnemies++;
  42. }
  43. }
  44. catch (Exception)
  45. {
  46. //ignored
  47. }
  48.  
  49. try
  50. {
  51. if (manipulatedBoard[i - 2][j + 1].Equals('K'))
  52. {
  53. currentEnemies++;
  54. }
  55. }
  56. catch (Exception)
  57. {
  58. //ignored
  59. }
  60. try
  61. {
  62. if (manipulatedBoard[i + 2][j - 1].Equals('K'))
  63. {
  64. currentEnemies++;
  65. }
  66. }
  67. catch (Exception)
  68. {
  69. //ignored
  70. }
  71. try
  72. {
  73. if (manipulatedBoard[i + 2][j + 1].Equals('K'))
  74. {
  75. currentEnemies++;
  76. }
  77. }
  78. catch (Exception)
  79. {
  80. //ignored
  81. }
  82. try
  83. {
  84. if (manipulatedBoard[i - 1][j - 2].Equals('K'))
  85. {
  86. currentEnemies++;
  87. }
  88. }
  89. catch (Exception)
  90. {
  91. //ignored
  92. }
  93. try
  94. {
  95. if (manipulatedBoard[i - 1][j + 2].Equals('K'))
  96. {
  97. currentEnemies++;
  98. }
  99. }
  100. catch (Exception)
  101. {
  102. //ignored
  103. }
  104. try
  105. {
  106. if (manipulatedBoard[i + 1][j - 2].Equals('K'))
  107. {
  108. currentEnemies++;
  109. }
  110. }
  111. catch (Exception)
  112. {
  113. //ignored
  114. }
  115. try
  116. {
  117. if (manipulatedBoard[i + 1][j + 2].Equals('K'))
  118. {
  119. currentEnemies++;
  120. }
  121. }
  122. catch (Exception)
  123. {
  124. //ignored
  125. }
  126.  
  127. if (currentEnemies <= mostDangerous)
  128. {
  129. continue;
  130. }
  131.  
  132. mostDangerous = currentEnemies;
  133. location = new KeyValuePair<int, int>(i, j);
  134. }
  135. }
  136.  
  137. if (mostDangerous > 0)
  138. {
  139. conflict = true;
  140. manipulatedBoard[location.Key][location.Value] = '0';
  141. }
  142. if (!conflict)
  143. {
  144. break;
  145. }
  146.  
  147. counter++;
  148. }
  149.  
  150. Console.WriteLine(counter);
  151. }
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement