Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class PresentDelivery {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int presentCount = Integer.parseInt(sc.nextLine());
- int size = Integer.parseInt(sc.nextLine());
- String[][] neighborhood = new String[size][size];
- int santaRow = -1;
- int santaCol = -1;
- int niceKidsCount = 0;
- for (int row = 0; row < size; row++) {
- String[] line = sc.nextLine().split("\\s+");
- for (int col = 0; col < size; col++) {
- neighborhood[row][col] = line[col];
- if (line[col].equals("S")) {
- santaRow = row;
- santaCol = col;
- }
- if (line[col].equals("V")) {
- niceKidsCount++;
- }
- }
- }
- neighborhood[santaRow][santaCol] = "-";
- int niceKids = niceKidsCount;
- String command = sc.nextLine();
- while (!"Christmas morning".equals(command)) {
- switch (command) {
- case "up":
- santaRow--;
- if (santaInRange(size, santaRow, santaCol)) {
- if (niceKidFound(neighborhood, santaRow, santaCol)) {
- niceKidsCount--;
- presentCount--;
- neighborhood[santaRow][santaCol] = "-";
- } else if (badKidFound(neighborhood, santaRow, santaCol)) {
- neighborhood[santaRow][santaCol] = "-";
- } else if (cookieFound(neighborhood, santaRow, santaCol)) {
- neighborhood[santaRow][santaCol] = "-";
- if (neighborhood[santaRow + 1][santaCol].equals("X")) {
- niceKids++;
- presentCount--;
- neighborhood[santaRow + 1][santaCol] = "-";
- } else if (neighborhood[santaRow + 1][santaCol].equals("V")) {
- niceKidsCount--;
- presentCount--;
- neighborhood[santaRow + 1][santaCol] = "-";
- }
- if (neighborhood[santaRow - 1][santaCol].equals("X")) {
- niceKids++;
- presentCount--;
- neighborhood[santaRow - 1][santaCol] = "-";
- } else if (neighborhood[santaRow - 1][santaCol].equals("V")) {
- niceKidsCount--;
- presentCount--;
- neighborhood[santaRow - 1][santaCol] = "-";
- }
- if (neighborhood[santaRow][santaCol - 1].equals("X")) {
- niceKids++;
- presentCount--;
- neighborhood[santaRow][santaCol - 1] = "-";
- } else if (neighborhood[santaRow][santaCol - 1].equals("V")) {
- niceKidsCount--;
- presentCount--;
- neighborhood[santaRow][santaCol - 1] = "-";
- }
- if (neighborhood[santaRow][santaCol + 1].equals("X")) {
- niceKids++;
- presentCount--;
- neighborhood[santaRow][santaCol + 1] = "-";
- } else if (neighborhood[santaRow][santaCol + 1].equals("V")) {
- niceKidsCount--;
- presentCount--;
- neighborhood[santaRow][santaCol + 1] = "-";
- }
- }
- }
- break;
- case "down":
- santaRow++;
- if (santaInRange(size, santaRow, santaCol)) {
- if (niceKidFound(neighborhood, santaRow, santaCol)) {
- niceKidsCount--;
- presentCount--;
- neighborhood[santaRow][santaCol] = "-";
- } else if (badKidFound(neighborhood, santaRow, santaCol)) {
- neighborhood[santaRow][santaCol] = "-";
- } else if (cookieFound(neighborhood, santaRow, santaCol)) {
- neighborhood[santaRow][santaCol] = "-";
- if (neighborhood[santaRow + 1][santaCol].equals("X")) {
- niceKids++;
- presentCount--;
- neighborhood[santaRow + 1][santaCol] = "-";
- } else if (neighborhood[santaRow + 1][santaCol].equals("V")) {
- niceKidsCount--;
- presentCount--;
- neighborhood[santaRow + 1][santaCol] = "-";
- }
- if (neighborhood[santaRow - 1][santaCol].equals("X")) {
- niceKids++;
- presentCount--;
- neighborhood[santaRow - 1][santaCol] = "-";
- } else if (neighborhood[santaRow - 1][santaCol].equals("V")) {
- niceKidsCount--;
- presentCount--;
- neighborhood[santaRow - 1][santaCol] = "-";
- }
- if (neighborhood[santaRow][santaCol - 1].equals("X")) {
- niceKids++;
- presentCount--;
- neighborhood[santaRow][santaCol - 1] = "-";
- } else if (neighborhood[santaRow][santaCol - 1].equals("V")) {
- niceKidsCount--;
- presentCount--;
- neighborhood[santaRow][santaCol - 1] = "-";
- }
- if (neighborhood[santaRow][santaCol + 1].equals("X")) {
- niceKids++;
- presentCount--;
- neighborhood[santaRow][santaCol + 1] = "-";
- } else if (neighborhood[santaRow][santaCol + 1].equals("V")) {
- niceKidsCount--;
- presentCount--;
- neighborhood[santaRow][santaCol + 1] = "-";
- }
- }
- }
- break;
- case "left":
- santaCol--;
- if (santaInRange(size, santaRow, santaCol)) {
- if (niceKidFound(neighborhood, santaRow, santaCol)) {
- niceKidsCount--;
- presentCount--;
- neighborhood[santaRow][santaCol] = "-";
- } else if (badKidFound(neighborhood, santaRow, santaCol)) {
- neighborhood[santaRow][santaCol] = "-";
- } else if (cookieFound(neighborhood, santaRow, santaCol)) {
- neighborhood[santaRow][santaCol] = "-";
- if (neighborhood[santaRow + 1][santaCol].equals("X")) {
- niceKids++;
- presentCount--;
- neighborhood[santaRow + 1][santaCol] = "-";
- } else if (neighborhood[santaRow + 1][santaCol].equals("V")) {
- niceKidsCount--;
- presentCount--;
- neighborhood[santaRow + 1][santaCol] = "-";
- }
- if (neighborhood[santaRow - 1][santaCol].equals("X")) {
- niceKids++;
- presentCount--;
- neighborhood[santaRow - 1][santaCol] = "-";
- } else if (neighborhood[santaRow - 1][santaCol].equals("V")) {
- niceKidsCount--;
- presentCount--;
- neighborhood[santaRow - 1][santaCol] = "-";
- }
- if (neighborhood[santaRow][santaCol - 1].equals("X")) {
- niceKids++;
- presentCount--;
- neighborhood[santaRow][santaCol - 1] = "-";
- } else if (neighborhood[santaRow][santaCol - 1].equals("V")) {
- niceKidsCount--;
- presentCount--;
- neighborhood[santaRow][santaCol - 1] = "-";
- }
- if (neighborhood[santaRow][santaCol + 1].equals("X")) {
- niceKids++;
- presentCount--;
- neighborhood[santaRow][santaCol + 1] = "-";
- } else if (neighborhood[santaRow][santaCol + 1].equals("V")) {
- niceKidsCount--;
- presentCount--;
- neighborhood[santaRow][santaCol + 1] = "-";
- }
- }
- }
- break;
- case "right":
- santaCol++;
- if (santaInRange(size, santaRow, santaCol)) {
- if (niceKidFound(neighborhood, santaRow, santaCol)) {
- niceKidsCount--;
- presentCount--;
- neighborhood[santaRow][santaCol] = "-";
- } else if (badKidFound(neighborhood, santaRow, santaCol)) {
- neighborhood[santaRow][santaCol] = "-";
- } else if (cookieFound(neighborhood, santaRow, santaCol)) {
- neighborhood[santaRow][santaCol] = "-";
- if (neighborhood[santaRow + 1][santaCol].equals("X")) {
- niceKids++;
- presentCount--;
- neighborhood[santaRow + 1][santaCol] = "-";
- } else if (neighborhood[santaRow + 1][santaCol].equals("V")) {
- niceKidsCount--;
- presentCount--;
- neighborhood[santaRow + 1][santaCol] = "-";
- }
- if (neighborhood[santaRow - 1][santaCol].equals("X")) {
- niceKids++;
- presentCount--;
- neighborhood[santaRow - 1][santaCol] = "-";
- } else if (neighborhood[santaRow - 1][santaCol].equals("V")) {
- niceKidsCount--;
- presentCount--;
- neighborhood[santaRow - 1][santaCol] = "-";
- }
- if (neighborhood[santaRow][santaCol - 1].equals("X")) {
- niceKids++;
- presentCount--;
- neighborhood[santaRow][santaCol - 1] = "-";
- } else if (neighborhood[santaRow][santaCol - 1].equals("V")) {
- niceKidsCount--;
- presentCount--;
- neighborhood[santaRow][santaCol - 1] = "-";
- }
- if (neighborhood[santaRow][santaCol + 1].equals("X")) {
- niceKids++;
- presentCount--;
- neighborhood[santaRow][santaCol + 1] = "-";
- } else if (neighborhood[santaRow][santaCol + 1].equals("V")) {
- niceKidsCount--;
- presentCount--;
- neighborhood[santaRow][santaCol + 1] = "-";
- }
- }
- }
- break;
- }
- if (presentCount <= 0) {
- break;
- }
- if (niceKidsCount == 0) {
- break;
- }
- command = sc.nextLine();
- }
- neighborhood[santaRow][santaCol] = "S";
- if (presentCount == 0) {
- System.out.println("Santa ran out of presents!");
- }
- printNeighborhood(neighborhood);
- if (niceKidsCount == 0) {
- System.out.printf("Good job, Santa! %d happy nice kid/s.", niceKids);
- } else {
- System.out.printf("No presents for %d nice kid/s.", niceKidsCount);
- }
- }
- private static boolean santaInRange(int size, int santaRow, int santaCol) {
- return santaRow >= 0 && santaCol >= 0 && santaRow < size && santaCol < size;
- }
- private static void printNeighborhood(String[][] neighborhood) {
- for (String[] line : neighborhood) {
- for (String p : line) {
- System.out.print(p + " ");
- }
- System.out.println();
- }
- }
- private static boolean niceKidFound(String[][] field, int row, int col) {
- return field[row][col].equals("V");
- }
- private static boolean badKidFound(String[][] field, int row, int col) {
- return field[row][col].equals("X");
- }
- private static boolean cookieFound(String[][] field, int row, int col) {
- return field[row][col].equals("C");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement