Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class TheGarden {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- int rows = Integer.parseInt(reader.readLine());
- String[][] matrix = new String[rows][];
- for (int i = 0; i < matrix.length; i++) {
- String[] vegetables = reader.readLine().split("\\s+");
- matrix[i] = vegetables;
- }
- int carrots = 0;
- int potatoes = 0;
- int lettuce = 0;
- int harmedVegetables = 0;
- String input = reader.readLine();
- while (!input.equals("End of Harvest")) {
- String[] commands = input.split("\\s+");
- String command = commands[0];
- if ("Harvest".equals(command)) {
- int r = Integer.parseInt(commands[1]);
- int c = Integer.parseInt(commands[2]);
- if (inBounds(matrix, r, c)) {
- switch (matrix[r][c]) {
- case "C":
- carrots++;
- break;
- case "P":
- potatoes++;
- break;
- case "L":
- lettuce++;
- break;
- }
- matrix[r][c] = " ";
- }
- } else if ("Mole".equals(command)) {
- int r = Integer.parseInt(commands[1]);
- int c = Integer.parseInt(commands[2]);
- String direction = commands[3];
- if (inBounds(matrix, r, c)) {
- int harmVegies = 0;
- switch (direction) {
- case "up":
- harmVegies = goUp(matrix, r, c);
- break;
- case "down":
- harmVegies = goDown(matrix, r, c);
- break;
- case "left":
- harmVegies = goLeft(matrix, r, c);
- break;
- case "right":
- harmVegies = goRight(matrix, r, c);
- break;
- }
- harmedVegetables += harmVegies;
- }
- }
- input = reader.readLine();
- }
- for (int i = 0; i < matrix.length; i++) {
- String[] letters = matrix[i];
- System.out.println(String.join(" ", letters).trim());
- }
- System.out.println("Carrots: " + carrots);
- System.out.println("Potatoes: " + potatoes);
- System.out.println("Lettuce: " + lettuce);
- System.out.println("Harmed vegetables: " + harmedVegetables);
- }
- private static int goUp(String[][] matrix, int r, int c) {
- int count = 0;
- while (r >= 0) {
- if (inBounds(matrix, r, c)) {
- if (eat(matrix, r, c)) {
- count++;
- }
- }
- r -= 2;
- }
- return count;
- }
- private static int goDown(String[][] matrix, int r, int c) {
- int count = 0;
- while (r < matrix.length) {
- if (inBounds(matrix, r, c)) {
- if (eat(matrix, r, c)) {
- count++;
- }
- }
- r += 2;
- }
- return count;
- }
- private static int goLeft(String[][] matrix, int r, int c) {
- int count = 0;
- while (c >= 0) {
- if (inBounds(matrix, r, c)) {
- if (eat(matrix, r, c)) {
- count++;
- }
- }
- c += 2;
- }
- return count;
- }
- private static int goRight(String[][] matrix, int r, int c) {
- int count = 0;
- while (c < matrix[r].length) {
- if (inBounds(matrix, r, c)) {
- if (eat(matrix, r, c)) {
- count++;
- }
- }
- c += 2;
- }
- return count;
- }
- private static boolean inBounds(String[][] matrix, int row, int col) {
- return row >= 0 && row < matrix.length && col >= 0 && col < matrix[row].length;
- }
- private static boolean eat(String[][] matrix, int r, int c) {
- if (!matrix[r][c].equals(" ")) {
- matrix[r][c] = " ";
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement