i_graurov

Present Delivery

Jun 26th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.11 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[] santaPositions = new int[2];
  18.         for (int row = 0; row < neighbourhood.length; row++) {
  19.             String input = scanner.nextLine();
  20.             if (input.contains("S")) {
  21.                santaRow = row;
  22.                santaCol = input.replaceAll(" ", "").indexOf('S');
  23.             }
  24.             neighbourhood[row] = input.replaceAll(" ", "").toCharArray();
  25.         }
  26.  
  27.         String command = scanner.nextLine();
  28.         while (!command.equals("Christmas morning") && presents > 0) {
  29.             switch (command) {
  30.                 case "up":
  31.                     moveUp(neighbourhood, santaRow, santaCol);
  32.                     break;
  33.                 case "down":
  34.                     moveDown(neighbourhood, santaRow, santaCol);
  35.  
  36.                     break;
  37.                 case "left":
  38.                     moveLeft(neighbourhood, santaRow, santaCol);
  39.                     break;
  40.                 case "right":
  41.                     moveRight(neighbourhood, santaRow, santaCol);
  42.                     break;
  43.  
  44.  
  45.             }
  46.  
  47.             command = scanner.nextLine();
  48.         }
  49.  
  50.         System.out.println();
  51.     }
  52.  
  53.     private static void moveRight(char[][] neighbourhood, int santaRow, int santaCol) {
  54.         int row = santaRow;
  55.         int col = santaCol + 1;
  56.         if (neighbourhood[row][col] == 'V') {
  57.             if (presents > 0) {
  58.                 presents--;
  59.                 kidsWithPresents++;
  60.             }
  61.         }
  62.         if (neighbourhood[row][col] == 'C') {
  63.             presentsForAll(neighbourhood, row, col);
  64.         }
  65.         neighbourhood[row][col - 1] = '-';
  66.         neighbourhood[row][col] = 'S';
  67.         santaCol = col;
  68.     }
  69.  
  70.     private static void moveLeft(char[][] neighbourhood, int santaRow, int santaCol) {
  71.         int row = santaRow;
  72.         int col = santaCol - 1;
  73.         if (neighbourhood[row][col] == 'V') {
  74.             if (presents > 0) {
  75.                 presents--;
  76.                 kidsWithPresents++;
  77.             }
  78.         }
  79.         if (neighbourhood[row][col] == 'C') {
  80.             presentsForAll(neighbourhood, row, col);
  81.         }
  82.         neighbourhood[row][col + 1] = '-';
  83.         neighbourhood[row][col] = 'S';
  84.         santaCol = col;
  85.     }
  86.  
  87.     private static void moveDown(char[][] neighbourhood, int santaRow, int santaCol) {
  88.         int row = santaRow + 1;
  89.         int col = santaCol;
  90.         if (neighbourhood[row][col] == 'V') {
  91.             if (presents > 0) {
  92.                 presents--;
  93.                 kidsWithPresents++;
  94.             }
  95.         }
  96.         if (neighbourhood[row][col] == 'C') {
  97.             presentsForAll(neighbourhood, row, col);
  98.         }
  99.         neighbourhood[row - 1][col] = '-';
  100.         neighbourhood[row][col] = 'S';
  101.         santaRow = row;
  102.     }
  103.  
  104.  
  105.     private static void moveUp(char[][] neighbourhood, int santaRow, int santaCol) {
  106.         int row = santaRow - 1;
  107.         int col = santaCol;
  108.         if (neighbourhood[row][col] == 'V') {
  109.             if (presents > 0) {
  110.                 presents--;
  111.                 kidsWithPresents++;
  112.             }
  113.         }
  114.         if (neighbourhood[row][col] == 'C') {
  115.             presentsForAll(neighbourhood, row, col);
  116.         }
  117.         neighbourhood[row + 1][col] = '-';
  118.         neighbourhood[row][col] = 'S';
  119.         santaRow = row;
  120.     }
  121.  
  122.     private static void printMatrix(char[][] neighbourhood) {
  123.         for (int row1 = 0; row1 < neighbourhood.length; row1++) {
  124.             for (int col1 = 0; col1 < neighbourhood[row1].length; col1++) {
  125.                 System.out.print(neighbourhood[row1][col1] + " ");
  126.             }
  127.             System.out.println();
  128.         }
  129.     }
  130.  
  131.  
  132.     private static void presentsForAll(char[][] neighbourhood, int row, int col) {
  133.         if (presents > 0) {
  134.             if (neighbourhood[row - 1][col] == 'X' || neighbourhood[row - 1][col] == 'C') {
  135.                 neighbourhood[row - 1][col] = '-';
  136.                 presents--;
  137.                 kidsWithPresents++;
  138.             }
  139.             if (neighbourhood[row + 1][col] == 'X' || neighbourhood[row + 1][col] == 'C') {
  140.                 neighbourhood[row + 1][col] = '-';
  141.                 presents--;
  142.                 kidsWithPresents++;
  143.             }
  144.             if (neighbourhood[row][col - 1] == 'X' || neighbourhood[row][col - 1] == 'C') {
  145.                 neighbourhood[row][col - 1] = '-';
  146.                 presents--;
  147.                 kidsWithPresents++;
  148.             }
  149.             if (neighbourhood[row][col + 1] == 'X' || neighbourhood[row][col + 1] == 'C') {
  150.                 neighbourhood[row][col + 1] = '-';
  151.                 presents--;
  152.                 kidsWithPresents++;
  153.             }
  154.         }
  155.     }
  156. }
Add Comment
Please, Sign In to add comment