Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.11 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class PresentDelivery {
  3.     private static int rowSanta;
  4.     private static int colSanta;
  5.     public static void main(String[] args) {
  6.  
  7.    Scanner scanner = new Scanner(System.in);
  8.    int presentsNumber = Integer.parseInt(scanner.nextLine());
  9.    int n = Integer.parseInt(scanner.nextLine());
  10.  
  11.    int keepInitialValue = presentsNumber;
  12.    String [][] neighborhood = new String[n][n];
  13.        for (int r = 0; r < n; r++) {
  14.        String line = scanner.nextLine();
  15.        String [] row = line.split(" ");
  16.        if (line.contains("S")){
  17.            rowSanta = r;
  18.            line = line.replaceAll(" ", "");
  19.            colSanta = line.indexOf("S");
  20.        }
  21.        neighborhood[r] = row;
  22.    }
  23.     String direction = scanner.nextLine();
  24.    while (!direction.equals("Christmas morning")){
  25.        neighborhood[rowSanta][colSanta] = "-";
  26.        switch (direction) {
  27.            case "up":
  28.                rowSanta--;
  29.                break;
  30.            case "down":
  31.                rowSanta++;
  32.                break;
  33.            case "right":
  34.                colSanta++;
  35.                break;
  36.            case "left":
  37.                colSanta--;
  38.                break;
  39.        }
  40.        presentsNumber -= applyDirectionReducePresents(neighborhood);
  41.        if (hasSantaLeft(n , presentsNumber)){
  42.            break;
  43.        }
  44.        direction = scanner.nextLine();
  45.    }
  46.    if (hasSantaLeft(n, presentsNumber)) {
  47.        System.out.println("Santa ran out of presents!");
  48.    }
  49.    printMatrix(neighborhood);
  50.    int niceKidsLeft = 0;
  51.         for (int r = 0; r < n; r++) {
  52.             for (int c = 0; c < n; c++) {
  53.                 if (neighborhood[r][c].equals("V")){
  54.                     niceKidsLeft++;
  55.                 }
  56.             }
  57.         }
  58.         if (niceKidsLeft > 0) {
  59.             System.out.printf("No presents for %d nice kid/s.", niceKidsLeft);
  60.         }
  61.         else {
  62.             System.out.printf("Good job, Santa! %d happy nice kid/s.", keepInitialValue - presentsNumber);
  63.         }
  64.     }
  65.     private static int applyDirectionReducePresents(String[][] neighborhood) {
  66.         int numberOfGrantPresents = 0;
  67.         if (isInBounds(rowSanta, colSanta, neighborhood.length)){
  68.             if (neighborhood[rowSanta][colSanta].equals("V")){
  69.                 numberOfGrantPresents += 1;
  70.             }
  71.             else if (neighborhood[rowSanta][colSanta].equals("C")){
  72.                if (neighborhood[rowSanta - 1][colSanta].equals("V")
  73.                || neighborhood[rowSanta - 1][colSanta].equals("X")){
  74.                    numberOfGrantPresents += 1;
  75.                }
  76.                 if (neighborhood[rowSanta + 1][colSanta].equals("V")
  77.                         || neighborhood[rowSanta + 1][colSanta].equals("X")){
  78.                     numberOfGrantPresents += 1;
  79.                 }
  80.                if (neighborhood[rowSanta][colSanta + 1].equals("V")
  81.                || neighborhood[rowSanta][colSanta + 1].equals("X")){
  82.                    numberOfGrantPresents += 1;
  83.                }
  84.                 if (neighborhood[rowSanta][colSanta - 1].equals("V")
  85.                         || neighborhood[rowSanta][colSanta - 1].equals("X")){
  86.                     numberOfGrantPresents += 1;
  87.                 }
  88.                neighborhood[rowSanta - 1][colSanta] = "-";
  89.                neighborhood[rowSanta + 1][colSanta] = "-";
  90.                neighborhood[rowSanta][colSanta + 1] = "-";
  91.                neighborhood[rowSanta][colSanta - 1] = "-";
  92.             }
  93.             neighborhood[rowSanta][colSanta] = "S";
  94.         }
  95.  
  96.         return numberOfGrantPresents;
  97.     }
  98.     private static boolean hasSantaLeft(int n, int m) {
  99.         return !isInBounds(rowSanta, colSanta, n) || m == 0;
  100.     }
  101.  
  102.     private static void printMatrix (String [][] matrix) {
  103.         for (int r = 0; r < matrix.length; r++) {
  104.             for (int c = 0; c < matrix[r].length; c++) {
  105.                 System.out.print(matrix[r][c] + " ");
  106.             }
  107.             System.out.println();
  108.         }
  109.     }
  110.     private static boolean isInBounds (int row, int col, int n){
  111.         return row < n && row >= 0 && col < n && col >= 0;
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement