Advertisement
Guest User

Untitled

a guest
May 30th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Linq;
  4.  
  5. namespace _02_Present_Delivery
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. int m = int.Parse(Console.ReadLine());
  12. int n = int.Parse(Console.ReadLine());
  13.  
  14. var matrix = new char[n, n];
  15. int rowSanta = 0;
  16. int colSanta = 0;
  17. int countOfNiceKids = 0;
  18.  
  19. for (int row = 0; row < n; row++)
  20. {
  21. var input = Console.ReadLine()
  22. .Split(' ', StringSplitOptions.RemoveEmptyEntries)
  23. .Select(char.Parse)
  24. .ToArray();
  25. for (int col = 0; col < n; col++)
  26. {
  27. if (input[col] == 'S')
  28. {
  29. rowSanta = row;
  30. colSanta = col;
  31. }
  32. if (input[col] == 'V')
  33. {
  34. countOfNiceKids++;
  35. }
  36. matrix[row, col] = input[col];
  37. }
  38. }
  39.  
  40. int comparisonForCount = countOfNiceKids;
  41.  
  42. string command;
  43.  
  44. while (m > 0)
  45. {
  46. if ((command = Console.ReadLine()) == "Christmas morning")
  47. {
  48. break;
  49. }
  50.  
  51.  
  52.  
  53. matrix[rowSanta, colSanta] = '-';
  54.  
  55. if (command == "up")
  56. {
  57. if (MatrixValidate(matrix, rowSanta - 1, colSanta) == true)
  58. {
  59. rowSanta = rowSanta - 1;
  60.  
  61. MoveResult(ref countOfNiceKids, matrix, rowSanta, colSanta, ref m);
  62. }
  63. }
  64.  
  65. else if (command == "down")
  66. {
  67. if (MatrixValidate(matrix, rowSanta + 1, colSanta) == true)
  68. {
  69. rowSanta = rowSanta + 1;
  70.  
  71. MoveResult(ref countOfNiceKids, matrix, rowSanta, colSanta, ref m);
  72. }
  73. }
  74. else if (command == "left")
  75. {
  76. if (MatrixValidate(matrix, rowSanta, colSanta - 1) == true)
  77. {
  78. colSanta = colSanta - 1;
  79.  
  80. MoveResult(ref countOfNiceKids, matrix, rowSanta, colSanta, ref m);
  81. }
  82.  
  83. }
  84. else if (command == "right")
  85. {
  86. if (MatrixValidate(matrix, rowSanta, colSanta + 1) == true)
  87. {
  88. colSanta = colSanta + 1;
  89.  
  90. MoveResult(ref countOfNiceKids, matrix, rowSanta, colSanta, ref m);
  91. }
  92. }
  93.  
  94. matrix[rowSanta, colSanta] = 'S';
  95. }
  96.  
  97. if (m == 0)
  98. {
  99. Console.WriteLine("Santa ran out of presents!");
  100. }
  101.  
  102. MatrixPrint(matrix);
  103.  
  104. if (countOfNiceKids==0)
  105. {
  106. Console.WriteLine($"Good job, Santa! {comparisonForCount} happy nice kid/s.");
  107. }
  108. else
  109. {
  110. Console.WriteLine($"No presents for {countOfNiceKids} nice kid/s.");
  111. }
  112. }
  113.  
  114. private static void MatrixPrint(char[,] matrix)
  115. {
  116. for (int row = 0; row < matrix.GetLength(0); row++)
  117. {
  118. for (int col = 0; col < matrix.GetLength(1); col++)
  119. {
  120. Console.Write(matrix[row, col] + " ");
  121. }
  122. Console.WriteLine();
  123. }
  124. }
  125. private static bool MatrixValidate (char[,] matrix, int rowSanta, int colSanta)
  126. {
  127. if (rowSanta>=0&&rowSanta<=matrix.GetLength(0)&&colSanta>=0&&colSanta<=matrix.GetLength(1))
  128. {
  129. return true;
  130. }
  131. else
  132. {
  133. return false;
  134. }
  135. }
  136. private static void MoveResult(ref int countOfNiceKids, char[,] matrix, int rowSanta, int colSanta, ref int m)
  137. {
  138. char symbol = matrix[rowSanta, colSanta];
  139.  
  140. if (symbol == 'V')
  141. {
  142. m--;
  143. countOfNiceKids--;
  144. }
  145. else if (symbol == 'C')
  146. {
  147.  
  148. if (matrix[rowSanta - 1, colSanta] != '-' )
  149. {
  150. if (matrix[rowSanta - 1, colSanta] == 'V')
  151. {
  152. countOfNiceKids--;
  153. }
  154. matrix[rowSanta - 1, colSanta] = '-';
  155. m--;
  156. }
  157. if (matrix[rowSanta + 1, colSanta] != '-' )
  158. {
  159. if (matrix[rowSanta + 1, colSanta] == 'V')
  160. {
  161. countOfNiceKids--;
  162. }
  163. matrix[rowSanta + 1, colSanta] = '-';
  164. m--;
  165. }
  166. if (matrix[rowSanta, colSanta - 1] != '-' )
  167. {
  168. if (matrix[rowSanta, colSanta - 1] == 'V')
  169. {
  170. countOfNiceKids--;
  171. }
  172. matrix[rowSanta, colSanta - 1] = '-';
  173. m--;
  174. }
  175. if (matrix[rowSanta, colSanta + 1] != '-')
  176. {
  177. if (matrix[rowSanta, colSanta + 1] == 'V')
  178. {
  179. countOfNiceKids--;
  180. }
  181. matrix[rowSanta, colSanta + 1] = '-';
  182. m--;
  183. }
  184.  
  185. }
  186.  
  187. }
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement