Advertisement
CuBG

Untitled

Jun 12th, 2023
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.18 KB | Software | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PresentDelivery {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.  
  7.         int presentCount = Integer.parseInt(sc.nextLine());
  8.         int size = Integer.parseInt(sc.nextLine());
  9.  
  10.         String[][] neighborhood = new String[size][size];
  11.         int santaRow = -1;
  12.         int santaCol = -1;
  13.  
  14.         int niceKidsCount = 0;
  15.         for (int row = 0; row < size; row++) {
  16.             String[] line = sc.nextLine().split("\\s+");
  17.             for (int col = 0; col < size; col++) {
  18.                 neighborhood[row][col] = line[col];
  19.                 if (line[col].equals("S")) {
  20.                     santaRow = row;
  21.                     santaCol = col;
  22.                 }
  23.                 if (line[col].equals("V")) {
  24.                     niceKidsCount++;
  25.                 }
  26.             }
  27.         }
  28.         neighborhood[santaRow][santaCol] = "-";
  29.         int niceKids = niceKidsCount;
  30.         String command = sc.nextLine();
  31.         while (!"Christmas morning".equals(command)) {
  32.             switch (command) {
  33.                 case "up":
  34.                     santaRow--;
  35.                     if (santaInRange(size, santaRow, santaCol)) {
  36.                         if (niceKidFound(neighborhood, santaRow, santaCol)) {
  37.                             niceKidsCount--;
  38.                             presentCount--;
  39.                             neighborhood[santaRow][santaCol] = "-";
  40.                         } else if (badKidFound(neighborhood, santaRow, santaCol)) {
  41.                             neighborhood[santaRow][santaCol] = "-";
  42.                         } else if (cookieFound(neighborhood, santaRow, santaCol)) {
  43.                             neighborhood[santaRow][santaCol] = "-";
  44.                             if (neighborhood[santaRow + 1][santaCol].equals("X")) {
  45.                                 niceKids++;
  46.                                 presentCount--;
  47.                                 neighborhood[santaRow + 1][santaCol] = "-";
  48.                             } else if (neighborhood[santaRow + 1][santaCol].equals("V")) {
  49.                                 niceKidsCount--;
  50.                                 presentCount--;
  51.                                 neighborhood[santaRow + 1][santaCol] = "-";
  52.                             }
  53.  
  54.                             if (neighborhood[santaRow - 1][santaCol].equals("X")) {
  55.                                 niceKids++;
  56.                                 presentCount--;
  57.                                 neighborhood[santaRow - 1][santaCol] = "-";
  58.                             } else if (neighborhood[santaRow - 1][santaCol].equals("V")) {
  59.                                 niceKidsCount--;
  60.                                 presentCount--;
  61.                                 neighborhood[santaRow - 1][santaCol] = "-";
  62.                             }
  63.  
  64.                             if (neighborhood[santaRow][santaCol - 1].equals("X")) {
  65.                                 niceKids++;
  66.                                 presentCount--;
  67.                                 neighborhood[santaRow][santaCol - 1] = "-";
  68.                             } else if (neighborhood[santaRow][santaCol - 1].equals("V")) {
  69.                                 niceKidsCount--;
  70.                                 presentCount--;
  71.                                 neighborhood[santaRow][santaCol - 1] = "-";
  72.                             }
  73.  
  74.                             if (neighborhood[santaRow][santaCol + 1].equals("X")) {
  75.                                 niceKids++;
  76.                                 presentCount--;
  77.                                 neighborhood[santaRow][santaCol + 1] = "-";
  78.                             } else if (neighborhood[santaRow][santaCol + 1].equals("V")) {
  79.                                 niceKidsCount--;
  80.                                 presentCount--;
  81.                                 neighborhood[santaRow][santaCol + 1] = "-";
  82.                             }
  83.                         }
  84.                     }
  85.                     break;
  86.                 case "down":
  87.                     santaRow++;
  88.                     if (santaInRange(size, santaRow, santaCol)) {
  89.                         if (niceKidFound(neighborhood, santaRow, santaCol)) {
  90.                             niceKidsCount--;
  91.                             presentCount--;
  92.                             neighborhood[santaRow][santaCol] = "-";
  93.                         } else if (badKidFound(neighborhood, santaRow, santaCol)) {
  94.                             neighborhood[santaRow][santaCol] = "-";
  95.                         } else if (cookieFound(neighborhood, santaRow, santaCol)) {
  96.                             neighborhood[santaRow][santaCol] = "-";
  97.                             if (neighborhood[santaRow + 1][santaCol].equals("X")) {
  98.                                 niceKids++;
  99.                                 presentCount--;
  100.                                 neighborhood[santaRow + 1][santaCol] = "-";
  101.                             } else if (neighborhood[santaRow + 1][santaCol].equals("V")) {
  102.                                 niceKidsCount--;
  103.                                 presentCount--;
  104.                                 neighborhood[santaRow + 1][santaCol] = "-";
  105.                             }
  106.                             if (neighborhood[santaRow - 1][santaCol].equals("X")) {
  107.                                 niceKids++;
  108.                                 presentCount--;
  109.                                 neighborhood[santaRow - 1][santaCol] = "-";
  110.                             } else if (neighborhood[santaRow - 1][santaCol].equals("V")) {
  111.                                 niceKidsCount--;
  112.                                 presentCount--;
  113.                                 neighborhood[santaRow - 1][santaCol] = "-";
  114.                             }
  115.                             if (neighborhood[santaRow][santaCol - 1].equals("X")) {
  116.                                 niceKids++;
  117.                                 presentCount--;
  118.                                 neighborhood[santaRow][santaCol - 1] = "-";
  119.                             } else if (neighborhood[santaRow][santaCol - 1].equals("V")) {
  120.                                 niceKidsCount--;
  121.                                 presentCount--;
  122.                                 neighborhood[santaRow][santaCol - 1] = "-";
  123.                             }
  124.                             if (neighborhood[santaRow][santaCol + 1].equals("X")) {
  125.                                 niceKids++;
  126.                                 presentCount--;
  127.                                 neighborhood[santaRow][santaCol + 1] = "-";
  128.                             } else if (neighborhood[santaRow][santaCol + 1].equals("V")) {
  129.                                 niceKidsCount--;
  130.                                 presentCount--;
  131.                                 neighborhood[santaRow][santaCol + 1] = "-";
  132.                             }
  133.                         }
  134.                     }
  135.                     break;
  136.                 case "left":
  137.                     santaCol--;
  138.                     if (santaInRange(size, santaRow, santaCol)) {
  139.                         if (niceKidFound(neighborhood, santaRow, santaCol)) {
  140.                             niceKidsCount--;
  141.                             presentCount--;
  142.                             neighborhood[santaRow][santaCol] = "-";
  143.                         } else if (badKidFound(neighborhood, santaRow, santaCol)) {
  144.                             neighborhood[santaRow][santaCol] = "-";
  145.                         } else if (cookieFound(neighborhood, santaRow, santaCol)) {
  146.                             neighborhood[santaRow][santaCol] = "-";
  147.                             if (neighborhood[santaRow + 1][santaCol].equals("X")) {
  148.                                 niceKids++;
  149.                                 presentCount--;
  150.                                 neighborhood[santaRow + 1][santaCol] = "-";
  151.                             } else if (neighborhood[santaRow + 1][santaCol].equals("V")) {
  152.                                 niceKidsCount--;
  153.                                 presentCount--;
  154.                                 neighborhood[santaRow + 1][santaCol] = "-";
  155.                             }
  156.                             if (neighborhood[santaRow - 1][santaCol].equals("X")) {
  157.                                 niceKids++;
  158.                                 presentCount--;
  159.                                 neighborhood[santaRow - 1][santaCol] = "-";
  160.                             } else if (neighborhood[santaRow - 1][santaCol].equals("V")) {
  161.                                 niceKidsCount--;
  162.                                 presentCount--;
  163.                                 neighborhood[santaRow - 1][santaCol] = "-";
  164.                             }
  165.                             if (neighborhood[santaRow][santaCol - 1].equals("X")) {
  166.                                 niceKids++;
  167.                                 presentCount--;
  168.                                 neighborhood[santaRow][santaCol - 1] = "-";
  169.                             } else if (neighborhood[santaRow][santaCol - 1].equals("V")) {
  170.                                 niceKidsCount--;
  171.                                 presentCount--;
  172.                                 neighborhood[santaRow][santaCol - 1] = "-";
  173.                             }
  174.                             if (neighborhood[santaRow][santaCol + 1].equals("X")) {
  175.                                 niceKids++;
  176.                                 presentCount--;
  177.                                 neighborhood[santaRow][santaCol + 1] = "-";
  178.                             } else if (neighborhood[santaRow][santaCol + 1].equals("V")) {
  179.                                 niceKidsCount--;
  180.                                 presentCount--;
  181.                                 neighborhood[santaRow][santaCol + 1] = "-";
  182.                             }
  183.                         }
  184.                     }
  185.                     break;
  186.                 case "right":
  187.                     santaCol++;
  188.                     if (santaInRange(size, santaRow, santaCol)) {
  189.                         if (niceKidFound(neighborhood, santaRow, santaCol)) {
  190.                             niceKidsCount--;
  191.                             presentCount--;
  192.                             neighborhood[santaRow][santaCol] = "-";
  193.                         } else if (badKidFound(neighborhood, santaRow, santaCol)) {
  194.                             neighborhood[santaRow][santaCol] = "-";
  195.                         } else if (cookieFound(neighborhood, santaRow, santaCol)) {
  196.                             neighborhood[santaRow][santaCol] = "-";
  197.                             if (neighborhood[santaRow + 1][santaCol].equals("X")) {
  198.                                 niceKids++;
  199.                                 presentCount--;
  200.                                 neighborhood[santaRow + 1][santaCol] = "-";
  201.                             } else if (neighborhood[santaRow + 1][santaCol].equals("V")) {
  202.                                 niceKidsCount--;
  203.                                 presentCount--;
  204.                                 neighborhood[santaRow + 1][santaCol] = "-";
  205.                             }
  206.                             if (neighborhood[santaRow - 1][santaCol].equals("X")) {
  207.                                 niceKids++;
  208.                                 presentCount--;
  209.                                 neighborhood[santaRow - 1][santaCol] = "-";
  210.                             } else if (neighborhood[santaRow - 1][santaCol].equals("V")) {
  211.                                 niceKidsCount--;
  212.                                 presentCount--;
  213.                                 neighborhood[santaRow - 1][santaCol] = "-";
  214.                             }
  215.                             if (neighborhood[santaRow][santaCol - 1].equals("X")) {
  216.                                 niceKids++;
  217.                                 presentCount--;
  218.                                 neighborhood[santaRow][santaCol - 1] = "-";
  219.                             } else if (neighborhood[santaRow][santaCol - 1].equals("V")) {
  220.                                 niceKidsCount--;
  221.                                 presentCount--;
  222.                                 neighborhood[santaRow][santaCol - 1] = "-";
  223.                             }
  224.                             if (neighborhood[santaRow][santaCol + 1].equals("X")) {
  225.                                 niceKids++;
  226.                                 presentCount--;
  227.                                 neighborhood[santaRow][santaCol + 1] = "-";
  228.                             } else if (neighborhood[santaRow][santaCol + 1].equals("V")) {
  229.                                 niceKidsCount--;
  230.                                 presentCount--;
  231.                                 neighborhood[santaRow][santaCol + 1] = "-";
  232.                             }
  233.                         }
  234.                     }
  235.                     break;
  236.             }
  237.             if (presentCount <= 0) {
  238.                 break;
  239.             }
  240.             if (niceKidsCount == 0) {
  241.                 break;
  242.             }
  243.             command = sc.nextLine();
  244.         }
  245.         neighborhood[santaRow][santaCol] = "S";
  246.  
  247.         if (presentCount == 0) {
  248.             System.out.println("Santa ran out of presents!");
  249.         }
  250.         printNeighborhood(neighborhood);
  251.         if (niceKidsCount == 0) {
  252.             System.out.printf("Good job, Santa! %d happy nice kid/s.", niceKids);
  253.         } else {
  254.             System.out.printf("No presents for %d nice kid/s.", niceKidsCount);
  255.         }
  256.     }
  257.  
  258.  
  259.     private static boolean santaInRange(int size, int santaRow, int santaCol) {
  260.         return santaRow >= 0 && santaCol >= 0 && santaRow < size && santaCol < size;
  261.     }
  262.  
  263.     private static void printNeighborhood(String[][] neighborhood) {
  264.         for (String[] line : neighborhood) {
  265.             for (String p : line) {
  266.                 System.out.print(p + " ");
  267.             }
  268.             System.out.println();
  269.         }
  270.     }
  271.  
  272.     private static boolean niceKidFound(String[][] field, int row, int col) {
  273.         return field[row][col].equals("V");
  274.     }
  275.  
  276.     private static boolean badKidFound(String[][] field, int row, int col) {
  277.         return field[row][col].equals("X");
  278.     }
  279.  
  280.     private static boolean cookieFound(String[][] field, int row, int col) {
  281.         return field[row][col].equals("C");
  282.     }
  283. }
  284.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement