i_graurov

Present Delivery

Jun 26th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.95 KB | None | 0 0
  1. import java.lang.reflect.Array;
  2. import java.util.Scanner;
  3.  
  4. public class PresentDeliveryMatrix {
  5.  
  6.     static int kidsWithPresents;
  7.     static int presents;
  8.     static int santaRow;
  9.     static int santaCol;
  10.  
  11.  
  12.     public static void main(String[] args) {
  13.         Scanner scanner = new Scanner(System.in);
  14.         presents = Integer.parseInt(scanner.nextLine());
  15.         int n = Integer.parseInt(scanner.nextLine());
  16.         char[][] neighbourhood = new char[n][n];
  17.         int allGoodKids = 0;
  18.         int finalGoodKids = 0;
  19.         boolean pres = false;
  20.         for (int row = 0; row < neighbourhood.length; row++) {
  21.             String input = scanner.nextLine();
  22.             if (input.contains("S")) {
  23.                santaRow = row;
  24.                santaCol = input.replaceAll(" ", "").indexOf('S');
  25.             }
  26.             if (input.contains("V")) {
  27.                 allGoodKids++;
  28.             }
  29.             neighbourhood[row] = input.replaceAll(" ", "").toCharArray();
  30.         }
  31.  
  32.         String command = scanner.nextLine();
  33.         while (!command.equals("Christmas morning") && !pres) {
  34.             switch (command) {
  35.                 case "up":
  36.                     moveUp(neighbourhood);
  37.                     break;
  38.                 case "down":
  39.                     moveDown(neighbourhood);
  40.  
  41.                     break;
  42.                 case "left":
  43.                     moveLeft(neighbourhood);
  44.                     break;
  45.                 case "right":
  46.                     moveRight(neighbourhood);
  47.                     break;
  48.             }
  49.             if (presents<=0){
  50.                 System.out.println("Santa ran out of presents!");
  51.                 pres = true;
  52.             }
  53.             command = scanner.nextLine();
  54.         }
  55.         printMatrix(neighbourhood);
  56.  
  57. //        if (allGoodKids>kidsWithPresents){
  58. //            System.out.println("No presents for "+ (allGoodKids-kidsWithPresents) +" nice kid/s.");
  59. //        } else {
  60. //            System.out.println("Good job, Santa! " + allGoodKids + " happy nice kid/s.");
  61. //        }
  62.         for (int row = 0; row < neighbourhood.length; row++) {
  63.             for (int col = 0; col < neighbourhood[row].length; col++) {
  64.                 if (neighbourhood[row][col]=='V'){
  65.                     finalGoodKids++;
  66.                 }
  67.             }
  68.         }
  69.         if (finalGoodKids>0){
  70.             System.out.println("No presents for "+ finalGoodKids +" nice kid/s.");
  71.         } else {
  72.             System.out.println("Good job, Santa! " + allGoodKids + " happy nice kid/s.");
  73.         }
  74.  
  75.     }
  76.  
  77.     private static void moveRight(char[][] neighbourhood) {
  78.         int row = santaRow;
  79.         int col = santaCol + 1;
  80.         if (neighbourhood[row][col] == 'V') {
  81.             if (presents > 0) {
  82.                 presents--;
  83.                 kidsWithPresents++;
  84.             }
  85.         }
  86.         if (neighbourhood[row][col] == 'C') {
  87.             presentsForAll(neighbourhood, row, col);
  88.         }
  89.         neighbourhood[row][col - 1] = '-';
  90.         neighbourhood[row][col] = 'S';
  91.         santaCol = col;
  92.     }
  93.  
  94.     private static void moveLeft(char[][] neighbourhood) {
  95.         int row = santaRow;
  96.         int col = santaCol - 1;
  97.         if (neighbourhood[row][col] == 'V') {
  98.             if (presents > 0) {
  99.                 presents--;
  100.                 kidsWithPresents++;
  101.             }
  102.         }
  103.         if (neighbourhood[row][col] == 'C') {
  104.             presentsForAll(neighbourhood, row, col);
  105.         }
  106.         neighbourhood[row][col + 1] = '-';
  107.         neighbourhood[row][col] = 'S';
  108.         santaCol = col;
  109.     }
  110.  
  111.     private static void moveDown(char[][] neighbourhood) {
  112.         int row = santaRow + 1;
  113.         int col = santaCol;
  114.         if (neighbourhood[row][col] == 'V') {
  115.             if (presents > 0) {
  116.                 presents--;
  117.                 kidsWithPresents++;
  118.             }
  119.         }
  120.         if (neighbourhood[row][col] == 'C') {
  121.             presentsForAll(neighbourhood, row, col);
  122.         }
  123.         neighbourhood[row - 1][col] = '-';
  124.         neighbourhood[row][col] = 'S';
  125.         santaRow = row;
  126.     }
  127.  
  128.  
  129.     private static void moveUp(char[][] neighbourhood) {
  130.         int row = santaRow - 1;
  131.         int col = santaCol;
  132.         if (neighbourhood[row][col] == 'V') {
  133.             if (presents > 0) {
  134.                 presents--;
  135.                 kidsWithPresents++;
  136.             }
  137.         }
  138.         if (neighbourhood[row][col] == 'C') {
  139.             presentsForAll(neighbourhood, row, col);
  140.         }
  141.         neighbourhood[row + 1][col] = '-';
  142.         neighbourhood[row][col] = 'S';
  143.         santaRow = row;
  144.     }
  145.  
  146.     private static void printMatrix(char[][] neighbourhood) {
  147.         for (int row1 = 0; row1 < neighbourhood.length; row1++) {
  148.             for (int col1 = 0; col1 < neighbourhood[row1].length; col1++) {
  149.                 System.out.print(neighbourhood[row1][col1] + " ");
  150.             }
  151.             System.out.println();
  152.         }
  153.     }
  154.  
  155.  
  156.     private static void presentsForAll(char[][] neighbourhood, int row, int col) {
  157.         if (presents > 0) {
  158.             if (neighbourhood[row - 1][col] == 'X' || neighbourhood[row - 1][col] == 'V') {
  159.                 neighbourhood[row - 1][col] = '-';
  160.                 presents--;
  161.                 kidsWithPresents++;
  162.             }
  163.             if (neighbourhood[row + 1][col] == 'X' || neighbourhood[row + 1][col] == 'V') {
  164.                 neighbourhood[row + 1][col] = '-';
  165.                 presents--;
  166.                 kidsWithPresents++;
  167.             }
  168.             if (neighbourhood[row][col - 1] == 'X' || neighbourhood[row][col - 1] == 'V') {
  169.                 neighbourhood[row][col - 1] = '-';
  170.                 presents--;
  171.                 kidsWithPresents++;
  172.             }
  173.             if (neighbourhood[row][col + 1] == 'X' || neighbourhood[row][col + 1] == 'V') {
  174.                 neighbourhood[row][col + 1] = '-';
  175.                 presents--;
  176.                 kidsWithPresents++;
  177.             }
  178.         }
  179.     }
  180. }
Add Comment
Please, Sign In to add comment