Advertisement
Lyubohd

02. Seashell Treasure

Feb 14th, 2020
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.93 KB | None | 0 0
  1. import java.util.LinkedList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class Problem02 {
  6.     public static void main(String[] args) {
  7.         Scanner scan = new Scanner(System.in);
  8.  
  9.         int n = Integer.parseInt(scan.nextLine());
  10.         String[][] beach = new String[n][];
  11.         for (int i = 0; i < beach.length; i++) {
  12.             beach[i] = scan.nextLine().split("\\s+");
  13.         }
  14.         List<String> collectedShells = new LinkedList<>();
  15.         int stolenShells = 0;
  16.         String input = scan.nextLine();
  17.         while (!"Sunset".equals(input)) {
  18.             String[] tokens = input.split("\\s+");
  19.             String command = tokens[0];
  20.             int row = Integer.parseInt(tokens[1]);
  21.             int col = Integer.parseInt(tokens[2]);
  22.  
  23.             switch (command) {
  24.                 case "Collect":
  25.                     if (isValidIndex(row, col, beach)) {
  26.                         String shell = beach[row][col];
  27.                         if (!shell.equals("-")) {
  28.                             collectedShells.add(shell);
  29.                             beach[row][col] = "-";
  30.                         }
  31.                     }
  32.                     break;
  33.                 case "Steal":
  34.                     String direction = tokens[3];
  35.                     if (isValidIndex(row, col, beach)) {
  36.                         String shell = beach[row][col];
  37.                         if (!shell.equals("-")) {
  38.                             stolenShells++;
  39.                             beach[row][col] = "-";
  40.                         }
  41.                         stolenShells += stealing(beach, row, col, direction);
  42.                     }
  43.                     break;
  44.             }
  45.  
  46.             input = scan.nextLine();
  47.         }
  48.  
  49.         printMatrix(beach);
  50.         System.out.print(String.format("Collected seashells: %d", collectedShells.size()));
  51.         if (!collectedShells.isEmpty()) {
  52.             System.out.print(" -> ");
  53.             System.out.println(String.join(", ", collectedShells));
  54.         } else {
  55.             System.out.println();
  56.         }
  57.  
  58.         System.out.println(String.format("Stolen seashells: %d", stolenShells));
  59.     }
  60.  
  61.     private static int stealing(String[][] beach, int row, int col, String direction) {
  62.         int counter = 0;
  63.         switch (direction) {
  64.             case "up":
  65.                 for (int i = row; i >= row - 3; i--) {
  66.                     counter = getCounter(beach, i, counter, col);
  67.                 }
  68.                 break;
  69.             case "down":
  70.                 for (int i = row; i <= row + 3; i++) {
  71.                     counter = getCounter(beach, i, counter, col);
  72.                 }
  73.                 break;
  74.             case "left":
  75.                 for (int i = col; i >= col - 3; i--) {
  76.                     counter = getCounter(beach, row, counter, i);
  77.                 }
  78.                 break;
  79.             case "right":
  80.                 for (int i = col; i <= col + 3; i++) {
  81.                     counter = getCounter(beach, row, counter, i);
  82.                 }
  83.                 break;
  84.         }
  85.         return counter;
  86.     }
  87.  
  88.     private static int getCounter(String[][] beach, int row, int counter, int i) {
  89.         if (isValidIndex(row, i, beach)) {
  90.             String shell = beach[row][i];
  91.             if (!shell.equals("-")) {
  92.                 counter++;
  93.                 beach[row][i] = "-";
  94.             }
  95.         }
  96.         return counter;
  97.     }
  98.  
  99.     private static boolean isValidIndex(int row, int col, String[][] beach) {
  100.         if (row >= 0 && row < beach.length) {
  101.             if (col >= 0 && col < beach[row].length) {
  102.                 return true;
  103.             }
  104.         }
  105.         return false;
  106.     }
  107.  
  108.     private static void printMatrix(String[][] beach) {
  109.         for (String[] row : beach) {
  110.             for (String col : row) {
  111.                 System.out.print(col + " ");
  112.             }
  113.             System.out.println();
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement