Advertisement
mirozspace

Untitled

Jun 16th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.55 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.stream.Collectors;
  6.  
  7. public class Pr1 {
  8.     public static void main(String[] args) {
  9.  
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         int countCarrots = 0;
  13.         int countPotatoes = 0;
  14.         int countLettuce = 0;
  15.  
  16.         int countHarmed = 0;
  17.  
  18.         int n = Integer.parseInt(scanner.nextLine());
  19.         List<List<String>> matrix = new ArrayList<>();
  20.  
  21.         for (int i = 0; i < n; i++) {
  22.             matrix.add(i, new ArrayList<>());
  23.             matrix.get(i).addAll(Arrays
  24.                     .stream(scanner.nextLine().split("\\s+"))
  25.                     .collect(Collectors.toList()));
  26.         }
  27.  
  28.         String input = scanner.nextLine();
  29.         while (!input.equals("End of Harvest")) {
  30.             String[] arr = input.split("\\s+");
  31.  
  32.             String command = arr[0];
  33.             int row = Integer.parseInt(arr[1]);
  34.             int col = Integer.parseInt(arr[2]);
  35.  
  36.             switch (command) {
  37.                 case "Harvest":
  38.                     if (row >= 0 && row < matrix.size()) {
  39.                         if (col >= 0 && col < matrix.get(row).size()) {
  40.                             String symbol = matrix.get(row).get(col);
  41.                             if (symbol.equals("C")) countCarrots++;
  42.                             else if (symbol.equals("L")) countLettuce++;
  43.                             else if (symbol.equals("P")) countPotatoes++;
  44.                             matrix.get(row).set(col, " ");
  45.                         }
  46.                     }
  47.                     break;
  48.                 case "Mole":
  49.                     String direction = arr[3];
  50.                     direction = direction.substring(0, 1).toUpperCase() + direction.substring(1);
  51.                     if (row >= 0 && row < matrix.size()) {
  52.                         if (col >= 0 && col < matrix.get(row).size()) {
  53.                             if (direction.equals("Up")) {
  54.                                 for (int i = row; i >= 0; i -= 2) {
  55.                                     if (!matrix.get(row).get(col).equals(" ")){
  56.                                         matrix.get(i).set(col, " ");
  57.                                         countHarmed++;
  58.                                     }
  59.                                 }
  60.                             } else if (direction.equals("Down")) {
  61.                                 for (int i = row; i < matrix.size(); i += 2) {
  62.                                     if (!matrix.get(row).get(col).equals(" ")){
  63.                                         matrix.get(i).set(col, " ");
  64.                                         countHarmed++;
  65.                                     }
  66.                                 }
  67.  
  68.                             } else if (direction.equals("Left")) {
  69.                                 for (int i = col; i >= 0; i -= 2) {
  70.                                     if (!matrix.get(row).get(col).equals(" ")){
  71.                                         matrix.get(row).set(i, " ");
  72.                                         countHarmed++;
  73.                                     }
  74.                                 }
  75.  
  76.                             } else if (direction.equals("Right")) {
  77.                                 for (int i = col; i < matrix.get(row).size(); i += 2) {
  78.                                     if (!matrix.get(row).get(col).equals(" ")){
  79.                                         matrix.get(row).set(i, " ");
  80.                                         countHarmed++;
  81.                                     }
  82.                                 }
  83.                             }
  84.                         }
  85.                     }
  86.                     break;
  87.             }
  88.             input = scanner.nextLine();
  89.         }
  90.         printMatrix(matrix);
  91.         System.out.println("Carrots: " + countCarrots);
  92.         System.out.println("Potatoes: " + countPotatoes);
  93.         System.out.println("Lettuce: " + countLettuce);
  94.         System.out.println("Harmed: " + countHarmed);
  95.     }
  96.  
  97.     private static void printMatrix(List<List<String>> matrix) {
  98.         StringBuilder sb = new StringBuilder();
  99.         for (int i = 0; i < matrix.size(); i++) {
  100.             for (int j = 0; j < matrix.get(i).size(); j++) {
  101.                 sb.append(matrix.get(i).get(j)).append(" ");
  102.             }
  103.             String line = sb.toString();
  104.             line = line.substring(0, line.length() - 1);
  105.             System.out.print(line);
  106.             sb.delete(0, sb.length());
  107.             System.out.println();
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement