Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Scanner;
- public class Seashell {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> collectedSeashell = new LinkedList<>();
- List<String> stolenSeashell = new LinkedList<>();
- int n = Integer.parseInt(scanner.nextLine());
- String [][] matrix = new String[n][];
- for (int i = 0; i<n; i++) {
- matrix [i] = scanner.nextLine().split("\\s+");
- }
- String input = scanner.nextLine();
- int count = 0;
- while (!input.equals("Sunset")) {
- String [] tokens = input.split(" ");
- int row = Integer.parseInt(tokens[1]);
- int col = Integer.parseInt(tokens[2]);
- switch (tokens[0]) {
- case "Collect":
- if (isValidIndex(matrix, row, col)) {
- String shell = matrix[row][col];
- if (!shell.equals("-")) {
- collectedSeashell.add(shell);
- matrix[row][col] = "-";
- }
- }
- break;
- case "Steal":
- if (isValidIndex(matrix, row, col)) {
- String shell = matrix[row][col];
- String direction = tokens[3];
- switch (direction) {
- case "up":
- for (int i = row; i>=row -3; i--) {
- if (isValidIndex(matrix, i, col)) {
- if (!shell.equals("-")) {
- stolenSeashell.add(shell);
- matrix[i][col] ="-";
- count++;
- }
- }
- }
- break;
- case "down":
- for (int i = row; i<=row +3; i++) {
- if (isValidIndex(matrix, i, col)) {
- if (!shell.equals("-")) {
- stolenSeashell.add(shell);
- matrix[i][col] ="-";
- count++;
- }
- }
- }
- break;
- case "left":
- for (int i = col; i>=col -3; i--) {
- if (isValidIndex(matrix, row, i)) {
- if (!shell.equals("-")) {
- stolenSeashell.add(shell);
- matrix[row][i] ="-";
- count++;
- }
- }
- }
- break;
- case "right":
- for (int i = col; i<=col +3; i++) {
- if (isValidIndex(matrix, row, i)) {
- if (!shell.equals("-")) {
- stolenSeashell.add(shell);
- matrix[row][i] ="-";
- count++;
- }
- }
- }
- break;
- }
- }
- break;
- }
- input = scanner.nextLine();
- }
- printMatrix(matrix);
- System.out.printf("Collected seashells: %d", collectedSeashell.size());
- if (!collectedSeashell.isEmpty()) {
- System.out.print(" -> ");
- System.out.println(String.join(", ", collectedSeashell));
- } else {
- System.out.println();
- }
- System.out.println("Stolen seashells: " + stolenSeashell.size());
- }
- private static void printMatrix(String[][] matrix) {
- for (int row = 0; row<matrix.length; row++) {
- for (int col = 0; col<matrix[row].length; col++) {
- System.out.print(matrix[row][col] + " ");
- }
- System.out.println();
- }
- }
- private static boolean isValidIndex(String[][] matrix, int row, int col) {
- if (row>=0 && row < matrix.length) {
- if (col >=0 && col < matrix[row].length) {
- return true;
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement