Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package PredvaritelnoAdvanced.Tesove.DemoAdvanced;
- import java.util.Scanner;
- public class TheGarden_01 {
- public static int harmed = 0;
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int rows = Integer.parseInt(scanner.nextLine());
- char[][] matrix = new char[rows][];
- for (int i = 0; i < matrix.length; i++) {
- String[] currentData = scanner.nextLine().split("\\s+");
- matrix[i] = new char[currentData.length];
- for (int j = 0; j < currentData.length; j++) {
- matrix[i][j] = currentData[j].charAt(0);
- }
- }
- String input;
- int countCarrots = 0;
- int countPotatoes = 0;
- int countLettuce = 0;
- while (!(input = scanner.nextLine()).equals("End of Harvest")) {
- String[] tokens = input.split("\\s+");
- String type = tokens[0];
- int row = Integer.parseInt(tokens[1]);
- int col = Integer.parseInt(tokens[2]);
- if (isInBounds(matrix, row, col)) {
- if (type.equals("Harvest")) {
- char vegetable = matrix[row][col];
- switch (vegetable) {
- case 'L':
- countLettuce++;
- matrix[row][col] = ' ';
- break;
- case 'P':
- countPotatoes++;
- matrix[row][col] = ' ';
- break;
- case 'C':
- countCarrots++;
- matrix[row][col] = ' ';
- break;
- }
- } else if (type.equals("Mole")) {
- String direction = tokens[3];
- switch (direction) {
- case "up":
- moleUp(matrix, row, col);
- break;
- case "down":
- moleDown(matrix, row, col);
- break;
- case "left":
- moleLeft(matrix, row, col);
- break;
- case "right":
- moleRight(matrix, row, col);
- break;
- }
- }
- }
- }
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[i].length; j++) {
- System.out.print(matrix[i][j] + " ");
- }
- System.out.println();
- }
- System.out.println("Carrots: " + countCarrots);
- System.out.println("Potatoes: " + countPotatoes);
- System.out.println("Lettuce: " + countLettuce);
- System.out.println("Harmed vegetables: " + harmed);
- }
- private static void moleDown(char[][] matrix, int row, int col) {
- for (int currentRow = row; currentRow < matrix.length; currentRow += 2) {
- if (matrix[currentRow][col] != ' ') {
- harmed++;
- matrix[currentRow][col] = ' ';
- }
- }
- }
- private static void moleUp(char[][] matrix, int row, int col) {
- for (int currentRow = row; currentRow >= 0; currentRow -= 2) {
- if (matrix[currentRow][col] != ' ') {
- harmed++;
- matrix[currentRow][col] = ' ';
- }
- }
- }
- private static void moleLeft(char[][] matrix, int row, int col) {
- for (int currentElement = col; currentElement >= 0; currentElement -= 2) {
- if (matrix[row][currentElement] != ' ') {
- harmed++;
- matrix[row][currentElement] = ' ';
- }
- }
- }
- private static void moleRight(char[][] matrix, int row, int col) {
- for (int currentElement = col; currentElement < matrix[row].length; currentElement += 2) {
- if (matrix[row][currentElement] != ' ') {
- harmed++;
- matrix[row][currentElement] = ' ';
- }
- }
- }
- private static boolean isInBounds(char[][] matrix, int row, int col) {
- if (row < matrix.length) {
- if (col < matrix[row].length) {
- return true;
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement