Guest User

Untitled

a guest
Apr 25th, 2020
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.78 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace _10_Radio_Active_Mutant_Vampire_Bunnies
  6. {
  7. class Program
  8. {
  9. static char[,] matrix;
  10. static void Main(string[] args)
  11. {
  12. var input = Console.ReadLine()
  13. .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  14. .Select(int.Parse)
  15. .ToArray();
  16.  
  17. matrix = new char[input[0], input[1]];
  18.  
  19. (int rowCheck, int colCheck) = MatrixLoading();
  20.  
  21. string moves = Console.ReadLine();
  22.  
  23. bool IsSuccessMove = false;
  24. bool IsLive = false;
  25.  
  26. for (int i = 0; i < moves.Length; i++)
  27. {
  28. if (moves[i] == 'R')
  29. {
  30. if (IsValid(rowCheck, colCheck + 1) == true)
  31. {
  32. if (matrix[rowCheck, colCheck + 1] == '.')
  33. {
  34. matrix[rowCheck, colCheck] = '.';
  35. IsSuccessMove = true;
  36. }
  37. colCheck = colCheck + 1;
  38. }
  39. else
  40. {
  41. matrix[rowCheck, colCheck] = '.';
  42. BonneyMoves();
  43. MatrixPrint();
  44. Console.WriteLine($"won: {rowCheck} {colCheck}");
  45. return;
  46. }
  47. }
  48. else if (moves[i] == 'L')
  49. {
  50. if (IsValid(rowCheck, colCheck - 1) == true)
  51. {
  52. if (matrix[rowCheck, colCheck - 1] == '.')
  53. {
  54. matrix[rowCheck, colCheck] = '.';
  55. IsSuccessMove = true;
  56. }
  57. colCheck = colCheck - 1;
  58. }
  59. else
  60. {
  61. matrix[rowCheck, colCheck] = '.'; matrix[rowCheck, colCheck] = '.';
  62. BonneyMoves();
  63. MatrixPrint();
  64. Console.WriteLine($"won: {rowCheck} {colCheck}");
  65. return;
  66. }
  67. }
  68. else if (moves[i] == 'U')
  69. {
  70. if (IsValid(rowCheck - 1, colCheck) == true)
  71. {
  72. if (matrix[rowCheck - 1, colCheck] == '.')
  73. {
  74. matrix[rowCheck, colCheck] = '.';
  75. IsSuccessMove = true;
  76. }
  77. rowCheck = rowCheck - 1;
  78. }
  79. else
  80. {
  81. matrix[rowCheck, colCheck] = '.';
  82. BonneyMoves();
  83. MatrixPrint();
  84. Console.WriteLine($"won: {rowCheck} {colCheck}");
  85. return;
  86. }
  87. }
  88. else if (moves[i] == 'D')
  89. {
  90. if (IsValid(rowCheck + 1, colCheck) == true)
  91. {
  92. if (matrix[rowCheck + 1, colCheck] == '.')
  93. {
  94. matrix[rowCheck, colCheck] = '.';
  95. IsSuccessMove = true;
  96. }
  97. rowCheck = rowCheck + 1;
  98. }
  99. else
  100. {
  101. matrix[rowCheck, colCheck] = '.';
  102. BonneyMoves();
  103. MatrixPrint();
  104. Console.WriteLine($"won: {rowCheck} {colCheck}");
  105. return;
  106. }
  107. }
  108.  
  109. if (IsSuccessMove)
  110. {
  111. matrix[rowCheck, colCheck] = 'P';
  112. BonneyMoves();
  113.  
  114. foreach (var items in matrix)
  115. {
  116. if (items == 'P')
  117. {
  118. IsLive = true;
  119. }
  120.  
  121. }
  122.  
  123. if (IsLive == false)
  124. {
  125. MatrixPrint();
  126. Console.WriteLine($"dead: {rowCheck} {colCheck}");
  127. return;
  128. }
  129. }
  130.  
  131. else
  132. {
  133. BonneyMoves();
  134. MatrixPrint();
  135. Console.WriteLine($"dead: {rowCheck} {colCheck}");
  136. return;
  137. }
  138.  
  139. IsSuccessMove = false;
  140. }
  141. }
  142.  
  143. private static (int, int) MatrixLoading()
  144. {
  145. int rowCheck = 0;
  146. int colCheck = 0;
  147.  
  148. for (int row = 0; row < matrix.GetLength(0); row++)
  149. {
  150. string items = Console.ReadLine();
  151. for (int col = 0; col < matrix.GetLength(1); col++)
  152. {
  153. matrix[row, col] = items[col];
  154.  
  155. if (matrix[row, col] == 'P')
  156. {
  157. rowCheck = row;
  158. colCheck = col;
  159. }
  160. }
  161. }
  162.  
  163. return (rowCheck, colCheck);
  164. }
  165.  
  166. private static bool IsValid(int rowCheck, int colCheck)
  167. {
  168. if ((rowCheck >= 0 && rowCheck < matrix.GetLength(0)) && (colCheck >= 0 && colCheck < matrix.GetLength(1)))
  169. {
  170. return true;
  171. }
  172. else
  173. {
  174. return false;
  175. }
  176. }
  177.  
  178. private static void MatrixPrint()
  179. {
  180. for (int row = 0; row < matrix.GetLength(0); row++)
  181. {
  182.  
  183. for (int col = 0; col < matrix.GetLength(1); col++)
  184. {
  185. Console.Write(matrix[row, col]);
  186. }
  187. Console.WriteLine();
  188. }
  189. }
  190.  
  191. private static void BonneyMoves()
  192. {
  193. var indexBunnies = new Dictionary<int, List<int>>();
  194. int count = 0;
  195.  
  196. for (int row = 0; row < matrix.GetLength(0); row++)
  197. {
  198. for (int col = 0; col < matrix.GetLength(1); col++)
  199. {
  200. if (matrix[row, col] == 'B')
  201. {
  202. indexBunnies.Add(count, new List<int>());
  203. indexBunnies[count].Add(row);
  204. indexBunnies[count].Add(col);
  205. count++;
  206. }
  207. }
  208. }
  209.  
  210. for (int i =0;i<indexBunnies.Count;i++)
  211. {
  212. int row = indexBunnies[i][0];
  213. int col = indexBunnies[i][1];
  214.  
  215.  
  216. if (IsValid(row - 1, col) == true && matrix[row - 1, col] != 'B')
  217. {
  218. matrix[row - 1, col] = 'B';
  219. }
  220. if (IsValid(row + 1, col) == true && matrix[row + 1, col] != 'B')
  221. {
  222. matrix[row + 1, col] = 'B';
  223. }
  224. if (IsValid(row, col - 1) == true && matrix[row, col - 1] != 'B')
  225. {
  226. matrix[row, col - 1] = 'B';
  227. }
  228. if (IsValid(row, col + 1) == true && matrix[row, col + 1] != 'B')
  229. {
  230. matrix[row, col + 1] = 'B';
  231. }
  232. }
  233. indexBunnies.Clear();
  234. }
  235. }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment