Advertisement
KockataVelev3

The Garden

Jun 18th, 2019
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ExOne
  4. {
  5. class Program
  6. {
  7. public static bool Valid(string[][] matrix, int row, int col)
  8. {
  9. if (row >= 0 && row < matrix.Length)
  10. {
  11. int current = matrix[row].Length;
  12. if (col >= 0 && col <= current)
  13. {
  14. return true;
  15. }
  16. }
  17. return false;
  18. }
  19.  
  20. static void Main(string[] args)
  21. {
  22. int countRows = int.Parse(Console.ReadLine());
  23. string[][] array = new string[countRows][];
  24.  
  25. for (int i = 0; i < countRows; i++)
  26. {
  27. string[] input = Console.ReadLine()
  28. .Split(" ");
  29.  
  30. array[i] = new string[input.Length];
  31.  
  32. for (int j = 0; j < input.Length; j++)
  33. {
  34. array[i][j] = input[j];
  35. }
  36. }
  37.  
  38. int carrotsCount = 0;
  39. int potatoesCount = 0;
  40. int lettuceCount = 0;
  41. int harmedCount = 0;
  42.  
  43. string command = Console.ReadLine();
  44. while (command.ToLower() != "end of harvest")
  45. {
  46.  
  47. string[] tokens = command.
  48. Split(" ");
  49.  
  50. string action = tokens[0].ToLower();
  51. int row = int.Parse(tokens[1]);
  52. int col = int.Parse(tokens[2]);
  53.  
  54. if (action == "harvest")
  55. {
  56. if (Valid(array, row, col))
  57. {
  58. string cell = array[row][col];
  59. if (cell == "P")
  60. {
  61. potatoesCount++;
  62. array[row][col] = " ";
  63. }
  64. else if (cell == "L")
  65. {
  66. lettuceCount++;
  67. array[row][col] = " ";
  68. }
  69. else if (cell == "C")
  70. {
  71. carrotsCount++;
  72. array[row][col] = " ";
  73. }
  74. }
  75. }
  76.  
  77. else if (action == "mole")
  78. {
  79. string direction = tokens[3].ToLower();
  80. if (Valid(array, row, col))
  81. {
  82. if (direction == "up")
  83. {
  84. for (int i = row; i >= 0; i -= 2)
  85. {
  86. if (array[i][col] != " ")
  87. {
  88. array[i][col] = " ";
  89. harmedCount++;
  90. }
  91. }
  92. }
  93. else if (direction == "down")
  94. {
  95. for (int i = row; i < countRows - 1; i += 2)
  96. {
  97. if (array[i][col] != " ")
  98. {
  99. array[i][col] = " ";
  100. harmedCount++;
  101. }
  102. }
  103. }
  104. else if (direction == "left")
  105. {
  106. for (int i = col; i >= 0; i -= 2)
  107. {
  108. if (array[row][i] != " ")
  109. {
  110. array[row][i] = " ";
  111. harmedCount++;
  112. }
  113. }
  114. }
  115. else if (direction == "right")
  116. {
  117.  
  118. for (int i = col; i < array[row].Length; i += 2)
  119. {
  120. if (array[row][i] != " ")
  121. {
  122. array[row][i] = " ";
  123. harmedCount++;
  124. }
  125. }
  126. }
  127. }
  128.  
  129. }
  130. command = Console.ReadLine();
  131. }
  132.  
  133. foreach (var item in array)
  134. {
  135. Console.WriteLine(string.Join(" ", item));
  136. }
  137.  
  138. Console.WriteLine("Carrots: {0}", carrotsCount);
  139. Console.WriteLine("Potatoes: {0}", potatoesCount);
  140. Console.WriteLine("Lettuce: {0}", lettuceCount);
  141. Console.WriteLine($"Harmed vegetables: {harmedCount}");
  142. }
  143.  
  144.  
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement