Advertisement
bobo_bobkata

Untitled

Jun 18th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. package PredvaritelnoAdvanced.Tesove.DemoAdvanced;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class TheGarden_01 {
  6.  
  7. public static int harmed = 0;
  8.  
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11. int rows = Integer.parseInt(scanner.nextLine());
  12. char[][] matrix = new char[rows][];
  13. for (int i = 0; i < matrix.length; i++) {
  14. String[] currentData = scanner.nextLine().split("\\s+");
  15. matrix[i] = new char[currentData.length];
  16. for (int j = 0; j < currentData.length; j++) {
  17. matrix[i][j] = currentData[j].charAt(0);
  18. }
  19. }
  20. String input;
  21. int countCarrots = 0;
  22. int countPotatoes = 0;
  23. int countLettuce = 0;
  24. while (!(input = scanner.nextLine()).equals("End of Harvest")) {
  25. String[] tokens = input.split("\\s+");
  26. String type = tokens[0];
  27. int row = Integer.parseInt(tokens[1]);
  28. int col = Integer.parseInt(tokens[2]);
  29. if (isInBounds(matrix, row, col)) {
  30. if (type.equals("Harvest")) {
  31. char vegetable = matrix[row][col];
  32. switch (vegetable) {
  33. case 'L':
  34. countLettuce++;
  35. matrix[row][col] = ' ';
  36. break;
  37. case 'P':
  38. countPotatoes++;
  39. matrix[row][col] = ' ';
  40. break;
  41. case 'C':
  42. countCarrots++;
  43. matrix[row][col] = ' ';
  44. break;
  45. }
  46. } else if (type.equals("Mole")) {
  47. String direction = tokens[3];
  48. switch (direction) {
  49. case "up":
  50. moleUp(matrix, row, col);
  51. break;
  52. case "down":
  53. moleDown(matrix, row, col);
  54. break;
  55. case "left":
  56. moleLeft(matrix, row, col);
  57. break;
  58. case "right":
  59. moleRight(matrix, row, col);
  60. break;
  61. }
  62. }
  63. }
  64.  
  65. }
  66.  
  67. for (int i = 0; i < matrix.length; i++) {
  68. for (int j = 0; j < matrix[i].length; j++) {
  69. System.out.print(matrix[i][j] + " ");
  70. }
  71. System.out.println();
  72. }
  73.  
  74. System.out.println("Carrots: " + countCarrots);
  75. System.out.println("Potatoes: " + countPotatoes);
  76. System.out.println("Lettuce: " + countLettuce);
  77. System.out.println("Harmed vegetables: " + harmed);
  78.  
  79. }
  80.  
  81. private static void moleDown(char[][] matrix, int row, int col) {
  82. for (int currentRow = row; currentRow < matrix.length; currentRow += 2) {
  83. if (matrix[currentRow][col] != ' ') {
  84. harmed++;
  85. matrix[currentRow][col] = ' ';
  86. }
  87. }
  88. }
  89.  
  90. private static void moleUp(char[][] matrix, int row, int col) {
  91. for (int currentRow = row; currentRow >= 0; currentRow -= 2) {
  92. if (matrix[currentRow][col] != ' ') {
  93. harmed++;
  94. matrix[currentRow][col] = ' ';
  95. }
  96. }
  97. }
  98.  
  99. private static void moleLeft(char[][] matrix, int row, int col) {
  100. for (int currentElement = col; currentElement >= 0; currentElement -= 2) {
  101. if (matrix[row][currentElement] != ' ') {
  102. harmed++;
  103. matrix[row][currentElement] = ' ';
  104. }
  105. }
  106. }
  107.  
  108. private static void moleRight(char[][] matrix, int row, int col) {
  109. for (int currentElement = col; currentElement < matrix[row].length; currentElement += 2) {
  110. if (matrix[row][currentElement] != ' ') {
  111. harmed++;
  112. matrix[row][currentElement] = ' ';
  113. }
  114. }
  115. }
  116.  
  117. private static boolean isInBounds(char[][] matrix, int row, int col) {
  118. if (row < matrix.length) {
  119. if (col < matrix[row].length) {
  120. return true;
  121. }
  122. }
  123. return false;
  124. }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement