Advertisement
IvanIYankov

01. The Garden

Jun 26th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TheGarden {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         int rows = Integer.parseInt(scanner.nextLine());
  8.         String[][] matrix = new String[rows][];
  9.         for (int row = 0; row < rows; row++) {
  10.             String[] line = scanner.nextLine().split("\\s+");
  11.             matrix[row] = line;
  12.         }
  13.         String input;
  14.         int carrots = 0;
  15.         int potatoes = 0;
  16.         int lettuce = 0;
  17.         int harmedVeg = 0;
  18.  
  19.  
  20.         while (!"End of Harvest".equals(input = scanner.nextLine())) {
  21.             String[] tokens = input.split("\\s+");
  22.             String command = tokens[0];
  23.             int row = Integer.parseInt(tokens[1]);
  24.             int col = Integer.parseInt(tokens[2]);
  25.  
  26.             if (command.equals("Harvest")){
  27.                 if (!isOutOfBound(matrix, row, col)){
  28.                     String vegetable = matrix[row][col];
  29.                     switch (vegetable){
  30.                         case "C":
  31.                             carrots++;
  32.                             break;
  33.                         case "P":
  34.                             potatoes++;
  35.                             break;
  36.                         case "L":
  37.                             lettuce++;
  38.                             break;
  39.                     }
  40.                     matrix[row][col] = " ";
  41.                 }
  42.             } else {
  43.                 if (!isOutOfBound(matrix, row, col)){
  44.  
  45.                     String direction = tokens[3];
  46.  
  47.                     switch (direction){
  48.                         case "up":
  49.                             for (int i = row; i >= 0; i-=2) {
  50.                                 if (!isOutOfBound(matrix, row, col)) {
  51.                                     if (!matrix[i][col].equals(" ")) {
  52.                                         matrix[i][col] = " ";
  53.                                         harmedVeg++;
  54.                                     }
  55.                                 }
  56.                             }
  57.                             break;
  58.                         case "down":
  59.                             for (int i = row; i < matrix.length; i+=2) {
  60.                                 if (!isOutOfBound(matrix, row, col)) {
  61.                                     if (!matrix[i][col].equals(" ")) {
  62.                                         matrix[i][col] = " ";
  63.                                         harmedVeg++;
  64.                                     }
  65.                                 }
  66.                             }
  67.                             break;
  68.                         case "left":
  69.                             for (int i = col; i >= 0; i-=2) {
  70.                                 if (!isOutOfBound(matrix, row, col)) {
  71.                                     if (!matrix[row][i].equals(" ")) {
  72.                                         matrix[row][i] = " ";
  73.                                         harmedVeg++;
  74.                                     }
  75.                                 }
  76.                             }
  77.                             break;
  78.                         case "right":
  79.                             for (int i = col; i < matrix[row].length; i+=2) {
  80.                                 if (!isOutOfBound(matrix, row, col)) {
  81.                                     if (!matrix[row][i].equals(" ")) {
  82.                                         matrix[row][i] = " ";
  83.                                         harmedVeg++;
  84.                                     }
  85.                                 }
  86.                             }
  87.                             break;
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.         printMatrix(matrix);
  93.  
  94.         System.out.printf("Carrots: %d%nPotatoes: %d%nLettuce: %d%nHarmed vegetables: %d%n", carrots,
  95.                 potatoes, lettuce, harmedVeg);
  96.     }
  97.  
  98.  
  99.     private static void printMatrix(String[][] matrix) {
  100.         for (int i = 0; i < matrix.length; i++) {
  101.             for (int j = 0; j < matrix[i].length; j++) {
  102.                 System.out.print(matrix[i][j] + " ");
  103.             }
  104.             System.out.println();
  105.         }
  106.     }
  107.  
  108.     public static boolean isOutOfBound(String[][] matrix, int row, int col) {
  109.         return row < 0 || row >= matrix.length || col < 0 || col >= matrix[row].length;
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement