Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.reflect.Array;
- import java.util.Scanner;
- public class PresentDeliveryMatrix {
- static int kidsWithPresents;
- static int presents;
- static int santaRow;
- static int santaCol;
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- presents = Integer.parseInt(scanner.nextLine());
- int n = Integer.parseInt(scanner.nextLine());
- char[][] neighbourhood = new char[n][n];
- // int[] santaPositions = new int[2];
- for (int row = 0; row < neighbourhood.length; row++) {
- String input = scanner.nextLine();
- if (input.contains("S")) {
- santaRow = row;
- santaCol = input.replaceAll(" ", "").indexOf('S');
- }
- neighbourhood[row] = input.replaceAll(" ", "").toCharArray();
- }
- String command = scanner.nextLine();
- while (!command.equals("Christmas morning") && presents > 0) {
- switch (command) {
- case "up":
- moveUp(neighbourhood, santaRow, santaCol);
- break;
- case "down":
- moveDown(neighbourhood, santaRow, santaCol);
- break;
- case "left":
- moveLeft(neighbourhood, santaRow, santaCol);
- break;
- case "right":
- moveRight(neighbourhood, santaRow, santaCol);
- break;
- }
- command = scanner.nextLine();
- }
- System.out.println();
- }
- private static void moveRight(char[][] neighbourhood, int santaRow, int santaCol) {
- int row = santaRow;
- int col = santaCol + 1;
- if (neighbourhood[row][col] == 'V') {
- if (presents > 0) {
- presents--;
- kidsWithPresents++;
- }
- }
- if (neighbourhood[row][col] == 'C') {
- presentsForAll(neighbourhood, row, col);
- }
- neighbourhood[row][col - 1] = '-';
- neighbourhood[row][col] = 'S';
- santaCol = col;
- }
- private static void moveLeft(char[][] neighbourhood, int santaRow, int santaCol) {
- int row = santaRow;
- int col = santaCol - 1;
- if (neighbourhood[row][col] == 'V') {
- if (presents > 0) {
- presents--;
- kidsWithPresents++;
- }
- }
- if (neighbourhood[row][col] == 'C') {
- presentsForAll(neighbourhood, row, col);
- }
- neighbourhood[row][col + 1] = '-';
- neighbourhood[row][col] = 'S';
- santaCol = col;
- }
- private static void moveDown(char[][] neighbourhood, int santaRow, int santaCol) {
- int row = santaRow + 1;
- int col = santaCol;
- if (neighbourhood[row][col] == 'V') {
- if (presents > 0) {
- presents--;
- kidsWithPresents++;
- }
- }
- if (neighbourhood[row][col] == 'C') {
- presentsForAll(neighbourhood, row, col);
- }
- neighbourhood[row - 1][col] = '-';
- neighbourhood[row][col] = 'S';
- santaRow = row;
- }
- private static void moveUp(char[][] neighbourhood, int santaRow, int santaCol) {
- int row = santaRow - 1;
- int col = santaCol;
- if (neighbourhood[row][col] == 'V') {
- if (presents > 0) {
- presents--;
- kidsWithPresents++;
- }
- }
- if (neighbourhood[row][col] == 'C') {
- presentsForAll(neighbourhood, row, col);
- }
- neighbourhood[row + 1][col] = '-';
- neighbourhood[row][col] = 'S';
- santaRow = row;
- }
- private static void printMatrix(char[][] neighbourhood) {
- for (int row1 = 0; row1 < neighbourhood.length; row1++) {
- for (int col1 = 0; col1 < neighbourhood[row1].length; col1++) {
- System.out.print(neighbourhood[row1][col1] + " ");
- }
- System.out.println();
- }
- }
- private static void presentsForAll(char[][] neighbourhood, int row, int col) {
- if (presents > 0) {
- if (neighbourhood[row - 1][col] == 'X' || neighbourhood[row - 1][col] == 'C') {
- neighbourhood[row - 1][col] = '-';
- presents--;
- kidsWithPresents++;
- }
- if (neighbourhood[row + 1][col] == 'X' || neighbourhood[row + 1][col] == 'C') {
- neighbourhood[row + 1][col] = '-';
- presents--;
- kidsWithPresents++;
- }
- if (neighbourhood[row][col - 1] == 'X' || neighbourhood[row][col - 1] == 'C') {
- neighbourhood[row][col - 1] = '-';
- presents--;
- kidsWithPresents++;
- }
- if (neighbourhood[row][col + 1] == 'X' || neighbourhood[row][col + 1] == 'C') {
- neighbourhood[row][col + 1] = '-';
- presents--;
- kidsWithPresents++;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment