Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class Pr1 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int countCarrots = 0;
- int countPotatoes = 0;
- int countLettuce = 0;
- int countHarmed = 0;
- int n = Integer.parseInt(scanner.nextLine());
- List<List<String>> matrix = new ArrayList<>();
- for (int i = 0; i < n; i++) {
- matrix.add(i, new ArrayList<>());
- matrix.get(i).addAll(Arrays
- .stream(scanner.nextLine().split("\\s+"))
- .collect(Collectors.toList()));
- }
- String input = scanner.nextLine();
- while (!input.equals("End of Harvest")) {
- String[] arr = input.split("\\s+");
- String command = arr[0];
- int row = Integer.parseInt(arr[1]);
- int col = Integer.parseInt(arr[2]);
- switch (command) {
- case "Harvest":
- if (row >= 0 && row < matrix.size()) {
- if (col >= 0 && col < matrix.get(row).size()) {
- String symbol = matrix.get(row).get(col);
- if (symbol.equals("C")) countCarrots++;
- else if (symbol.equals("L")) countLettuce++;
- else if (symbol.equals("P")) countPotatoes++;
- matrix.get(row).set(col, " ");
- }
- }
- break;
- case "Mole":
- String direction = arr[3];
- direction = direction.substring(0, 1).toUpperCase() + direction.substring(1);
- if (row >= 0 && row < matrix.size()) {
- if (col >= 0 && col < matrix.get(row).size()) {
- if (direction.equals("Up")) {
- for (int i = row; i >= 0; i -= 2) {
- if (!matrix.get(row).get(col).equals(" ")){
- matrix.get(i).set(col, " ");
- countHarmed++;
- }
- }
- } else if (direction.equals("Down")) {
- for (int i = row; i < matrix.size(); i += 2) {
- if (!matrix.get(row).get(col).equals(" ")){
- matrix.get(i).set(col, " ");
- countHarmed++;
- }
- }
- } else if (direction.equals("Left")) {
- for (int i = col; i >= 0; i -= 2) {
- if (!matrix.get(row).get(col).equals(" ")){
- matrix.get(row).set(i, " ");
- countHarmed++;
- }
- }
- } else if (direction.equals("Right")) {
- for (int i = col; i < matrix.get(row).size(); i += 2) {
- if (!matrix.get(row).get(col).equals(" ")){
- matrix.get(row).set(i, " ");
- countHarmed++;
- }
- }
- }
- }
- }
- break;
- }
- input = scanner.nextLine();
- }
- printMatrix(matrix);
- System.out.println("Carrots: " + countCarrots);
- System.out.println("Potatoes: " + countPotatoes);
- System.out.println("Lettuce: " + countLettuce);
- System.out.println("Harmed: " + countHarmed);
- }
- private static void printMatrix(List<List<String>> matrix) {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < matrix.size(); i++) {
- for (int j = 0; j < matrix.get(i).size(); j++) {
- sb.append(matrix.get(i).get(j)).append(" ");
- }
- String line = sb.toString();
- line = line.substring(0, line.length() - 1);
- System.out.print(line);
- sb.delete(0, sb.length());
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement