Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.61 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class TheGarden {
  6.     public static void main(String[] args) throws IOException {
  7.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  8.  
  9.         int rows = Integer.parseInt(reader.readLine());
  10.         String[][] matrix = new String[rows][];
  11.  
  12.         for (int i = 0; i < matrix.length; i++) {
  13.             String[] vegetables = reader.readLine().split("\\s+");
  14.             matrix[i] = vegetables;
  15.         }
  16.  
  17.         int carrots = 0;
  18.         int potatoes = 0;
  19.         int lettuce = 0;
  20.         int harmedVegetables = 0;
  21.  
  22.         String input = reader.readLine();
  23.         while (!input.equals("End of Harvest")) {
  24.             String[] commands = input.split("\\s+");
  25.  
  26.             String command = commands[0];
  27.             if ("Harvest".equals(command)) {
  28.                 int r = Integer.parseInt(commands[1]);
  29.                 int c = Integer.parseInt(commands[2]);
  30.                 if (inBounds(matrix, r, c)) {
  31.                     switch (matrix[r][c]) {
  32.                         case "C":
  33.                             carrots++;
  34.                             break;
  35.                         case "P":
  36.                             potatoes++;
  37.                             break;
  38.                         case "L":
  39.                             lettuce++;
  40.                             break;
  41.                     }
  42.                     matrix[r][c] = " ";
  43.                 }
  44.             } else if ("Mole".equals(command)) {
  45.                 int r = Integer.parseInt(commands[1]);
  46.                 int c = Integer.parseInt(commands[2]);
  47.                 String direction = commands[3];
  48.  
  49.                 if (inBounds(matrix, r, c)) {
  50.                     int harmVegies = 0;
  51.                     switch (direction) {
  52.                         case "up":
  53.                             harmVegies = goUp(matrix, r, c);
  54.                             break;
  55.                         case "down":
  56.                             harmVegies = goDown(matrix, r, c);
  57.                             break;
  58.                         case "left":
  59.                             harmVegies = goLeft(matrix, r, c);
  60.                             break;
  61.                         case "right":
  62.                             harmVegies = goRight(matrix, r, c);
  63.                             break;
  64.                     }
  65.                     harmedVegetables += harmVegies;
  66.                 }
  67.             }
  68.             input = reader.readLine();
  69.         }
  70.  
  71.         for (int i = 0; i < matrix.length; i++) {
  72.             String[] letters = matrix[i];
  73.             System.out.println(String.join(" ", letters).trim());
  74.         }
  75.  
  76.         System.out.println("Carrots: " + carrots);
  77.         System.out.println("Potatoes: " + potatoes);
  78.         System.out.println("Lettuce: " + lettuce);
  79.         System.out.println("Harmed vegetables: " + harmedVegetables);
  80.     }
  81.  
  82.     private static int goUp(String[][] matrix, int r, int c) {
  83.         int count = 0;
  84.         while (r >= 0) {
  85.             if (inBounds(matrix, r, c)) {
  86.                 if (eat(matrix, r, c)) {
  87.                     count++;
  88.                 }
  89.             }
  90.             r -= 2;
  91.         }
  92.         return count;
  93.     }
  94.  
  95.     private static int goDown(String[][] matrix, int r, int c) {
  96.         int count = 0;
  97.         while (r < matrix.length) {
  98.             if (inBounds(matrix, r, c)) {
  99.                 if (eat(matrix, r, c)) {
  100.                     count++;
  101.                 }
  102.             }
  103.             r += 2;
  104.         }
  105.         return count;
  106.     }
  107.  
  108.     private static int goLeft(String[][] matrix, int r, int c) {
  109.         int count = 0;
  110.         while (c >= 0) {
  111.             if (inBounds(matrix, r, c)) {
  112.                 if (eat(matrix, r, c)) {
  113.                     count++;
  114.                 }
  115.             }
  116.             c += 2;
  117.         }
  118.         return count;
  119.     }
  120.  
  121.     private static int goRight(String[][] matrix, int r, int c) {
  122.         int count = 0;
  123.         while (c < matrix[r].length) {
  124.             if (inBounds(matrix, r, c)) {
  125.                 if (eat(matrix, r, c)) {
  126.                     count++;
  127.                 }
  128.             }
  129.             c += 2;
  130.         }
  131.         return count;
  132.     }
  133.  
  134.     private static boolean inBounds(String[][] matrix, int row, int col) {
  135.         return row >= 0 && row < matrix.length && col >= 0 && col < matrix[row].length;
  136.     }
  137.  
  138.     private static boolean eat(String[][] matrix, int r, int c) {
  139.         if (!matrix[r][c].equals(" ")) {
  140.             matrix[r][c] = " ";
  141.             return true;
  142.         }
  143.         return false;
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement