Advertisement
Guest User

Untitled

a guest
Jun 13th, 2020
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. using System;
  2.  
  3. namespace PresentDelivery
  4. {
  5. class PresentDeliveryStart
  6. {
  7. static void Main()
  8. {
  9. int presentsCount = int.Parse(Console.ReadLine());
  10. int size = int.Parse(Console.ReadLine());
  11.  
  12. string[,] matrix = new string[size, size];
  13.  
  14. int santaRow = -1;
  15. int santaCol = -1;
  16.  
  17. int happyKids = 0;
  18.  
  19. for (int i = 0; i < matrix.GetLength(0); i++)
  20. {
  21. string[] line = Console.ReadLine()
  22. .Split(" ", StringSplitOptions.RemoveEmptyEntries);
  23.  
  24. for (int j = 0; j < matrix.GetLength(1); j++)
  25. {
  26. if (line[j].ToString() == "S")
  27. {
  28. santaRow = i;
  29. santaCol = j;
  30. }
  31. else if (line[j].ToString() == "V")
  32. {
  33. happyKids++;
  34. }
  35.  
  36. matrix[i, j] = line[j];
  37. }
  38. }
  39.  
  40. string command = "";
  41.  
  42. while ((command = Console.ReadLine()) != "Christmas morning" && presentsCount > 0)
  43. {
  44. matrix[santaRow, santaCol] = "-";
  45.  
  46. if (command == "up")
  47. {
  48. santaRow--;
  49. }
  50. else if (command == "down")
  51. {
  52. santaRow++;
  53. }
  54. else if (command == "left")
  55. {
  56. santaCol--;
  57. }
  58. else if (command == "right")
  59. {
  60. santaCol++;
  61. }
  62.  
  63. if (CheckBounderies(matrix, santaRow, santaCol))
  64. {
  65. Console.WriteLine("Santa ran out of presents.");
  66. break;
  67. }
  68.  
  69. if (matrix[santaRow, santaCol] == "-")
  70. {
  71. matrix[santaRow, santaCol] = "S";
  72. }
  73. else if (matrix[santaRow, santaCol] == "V")
  74. {
  75. matrix[santaRow, santaCol] = "S";
  76. presentsCount--;
  77.  
  78. if (presentsCount == 0)
  79. {
  80. Console.WriteLine("Santa ran out of presents!");
  81. break;
  82. }
  83. }
  84. else if (matrix[santaRow, santaCol] == "X")
  85. {
  86. continue;
  87. }
  88. else if (matrix[santaRow, santaCol] == "C")
  89. {
  90. matrix[santaRow, santaCol] = "S";
  91.  
  92.  
  93. if (matrix[santaRow, santaCol + 1] != "-")// right
  94. {
  95. presentsCount--;
  96. matrix[santaRow, santaCol + 1] = "-";
  97. if (presentsCount == 0)
  98. {
  99. break;
  100. }
  101. }
  102. if (matrix[santaRow, santaCol - 1] != "-")// left
  103. {
  104. presentsCount--;
  105. matrix[santaRow, santaCol - 1] = "-";
  106. if (presentsCount == 0)
  107. {
  108. break;
  109. }
  110. }
  111. if (matrix[santaRow - 1, santaCol] != "-")// up
  112. {
  113. presentsCount--;
  114. matrix[santaRow - 1, santaCol] = "-";
  115. if (presentsCount == 0)
  116. {
  117. break;
  118. }
  119. }
  120. if (matrix[santaRow + 1, santaCol] == "V")// down
  121. {
  122. presentsCount--;
  123. matrix[santaRow + 1, santaCol] = "-";
  124. if (presentsCount == 0)
  125. {
  126. break;
  127. }
  128. }
  129. }
  130. }
  131.  
  132. int goodKidsLeft = 0;
  133.  
  134. foreach (var item in matrix)
  135. {
  136. if (item == "V")
  137. {
  138. goodKidsLeft++;
  139. }
  140. }
  141.  
  142. Print(matrix);
  143.  
  144. if (goodKidsLeft == 0)
  145. {
  146. Console.WriteLine($"Good job, Santa! {happyKids} happy nice kid/s.");
  147. }
  148. else
  149. {
  150. Console.WriteLine($"No presents for {goodKidsLeft} nice kid/s.");
  151. }
  152. }
  153.  
  154. static bool CheckBounderies(string[,] matrix, int santaRow, int santaCol)
  155. {
  156. return santaRow < 0 || santaRow >= matrix.GetLength(0) || santaCol < 0 || santaCol >= matrix.GetLength(1);
  157. }
  158.  
  159. static void Print(string[,] matrix)
  160. {
  161. for (int i = 0; i < matrix.GetLength(0); i++)
  162. {
  163. for (int j = 0; j < matrix.GetLength(1); j++)
  164. {
  165. Console.Write(matrix[i, j] + " ");
  166. }
  167. Console.WriteLine();
  168. }
  169. }
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement