Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.73 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _10._Radioactive_Mutant_Vampire_Bunnies
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. int[] sizes = Console.ReadLine().Split(" ",StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  11.  
  12. int n = sizes[0];
  13. int m = sizes[1];
  14.  
  15. char[][] matrix = new char[n][];
  16.  
  17. string startPosition = string.Empty;
  18.  
  19. for (int row = 0; row < n; row++)
  20. {
  21. string inputLine = Console.ReadLine();
  22. matrix[row] = new char[m];
  23. for (int col = 0; col < m; col++)
  24. {
  25. matrix[row][col] = inputLine[col];
  26.  
  27. if (matrix[row][col] == 'P')
  28. {
  29. startPosition = row + " " + col;
  30. }
  31. }
  32. }
  33.  
  34. string commands = Console.ReadLine();
  35. string currentPosition = string.Empty;
  36. string status = string.Empty;
  37.  
  38. for (int i = 0; i < commands.Length; i++)
  39. {
  40. if (i == 0)
  41. {
  42. currentPosition = startPosition;
  43. }
  44. char command = commands[i];
  45. currentPosition = PlayerMove(matrix, command, currentPosition);
  46.  
  47. string[] tokens = currentPosition.Split().ToArray();
  48. int row = int.Parse(tokens[0]);
  49. int col = int.Parse(tokens[1]);
  50. if (tokens.Length == 3) //escape
  51. {
  52. status = "win";
  53. }
  54.  
  55. matrix = BunniesMultiply(matrix);
  56.  
  57. if (status == "win")
  58. {
  59. PrintMatrix(matrix);
  60. Console.WriteLine($"won: {row} {col}");
  61. break;
  62. }
  63. else // check dead
  64. {
  65. if (matrix[row][col] == 'B')
  66. {
  67. PrintMatrix(matrix);
  68. Console.WriteLine($"dead: {row} {col}");
  69. break;
  70. }
  71. }
  72.  
  73. }
  74.  
  75. }
  76.  
  77. private static char[][] BunniesMultiply(char[][] matrix)
  78. {
  79. int rowCount = matrix.GetLength(0);
  80. int colCount = matrix[0].GetLength(0);
  81.  
  82. char[][] resultingMatrix = new char[rowCount][];
  83.  
  84. for (int row = 0; row < rowCount; row++)
  85. {
  86. resultingMatrix[row] = new char[colCount];
  87. for (int col = 0; col < colCount; col++)
  88. {
  89. if (matrix[row][col] == 'B')
  90. {
  91. resultingMatrix[row][col] = 'B';
  92. continue;
  93. }
  94.  
  95. bool upLimit = row - 1 > -1 ? true : false;
  96. bool downLimit = row + 1 < rowCount ? true : false;
  97. bool rightLimit = col + 1 < colCount ? true : false;
  98. bool leftLimit = col - 1 > -1 ? true : false;
  99.  
  100. if (upLimit)
  101. {
  102. if (matrix[row - 1][col] == 'B')
  103. {
  104. resultingMatrix[row][col] = 'B';
  105. continue;
  106. }
  107. }
  108. if (rightLimit)
  109. {
  110. if (matrix[row][col + 1] == 'B')
  111. {
  112. resultingMatrix[row][col] = 'B';
  113. continue;
  114. }
  115. }
  116. if (downLimit)
  117. {
  118. if (matrix[row + 1][col] == 'B')
  119. {
  120. resultingMatrix[row][col] = 'B';
  121. continue;
  122. }
  123. }
  124. if (leftLimit)
  125. {
  126. if (matrix[row][col - 1] == 'B')
  127. {
  128. resultingMatrix[row][col] = 'B';
  129. continue;
  130. }
  131. }
  132. resultingMatrix[row][col] = '.';
  133.  
  134. }
  135. }
  136. return resultingMatrix;
  137. }
  138.  
  139. private static void PrintMatrix(char[][] matrix)
  140. {
  141. for (int row = 0; row < matrix.GetLength(0); row++)
  142. {
  143. for (int col = 0; col < matrix[0].GetLength(0); col++)
  144. {
  145. Console.Write(matrix[row][col]);
  146. }
  147. Console.WriteLine();
  148. }
  149. }
  150.  
  151. private static string PlayerMove(char[][] matrix, char command, string currentPosition)
  152. {
  153. int rowCount = matrix.GetLength(0);
  154. int colCount = matrix[0].GetLength(0);
  155.  
  156. string[] tokens = currentPosition.Split();
  157. int row = int.Parse(tokens[0]);
  158. int col = int.Parse(tokens[1]);
  159.  
  160. bool upLimit = row - 1 > -1 ? true : false;
  161. bool downLimit = row + 1 < rowCount ? true : false;
  162. bool rightLimit = col + 1 < colCount ? true : false;
  163. bool leftLimit = col - 1 > -1 ? true : false;
  164.  
  165. if (command == 'U')
  166. {
  167. if (upLimit == false)
  168. {
  169. currentPosition += " " + "win";
  170. }
  171. else
  172. {
  173. currentPosition = (row - 1) + " " + col;
  174. }
  175. }
  176. else if (command == 'R')
  177. {
  178. if (rightLimit == false)
  179. {
  180. currentPosition += " " + "win";
  181. }
  182. else
  183. {
  184. currentPosition = row + " " + (col + 1);
  185. }
  186. }
  187. else if (command == 'D')
  188. {
  189. if (downLimit == false)
  190. {
  191. currentPosition += " " + "win";
  192. }
  193. else
  194. {
  195. currentPosition = (row + 1) + " " + col;
  196. }
  197. }
  198. else if (command == 'L')
  199. {
  200. if (leftLimit == false)
  201. {
  202. currentPosition += " " + "win";
  203. }
  204. else
  205. {
  206. currentPosition = row + " " + (col - 1);
  207. }
  208. }
  209.  
  210. return currentPosition;
  211. }
  212. }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement