Advertisement
Ligh7_of_H3av3n

10. Radioactive Mutant Vampire Bunnies

May 16th, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 KB | None | 0 0
  1. package Uprajnenie;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6.  
  7. public class RadioactiveMutantVampireBunnies {
  8.     private static int[] playerPosition = new int[2];
  9.     private static final int[] rowMovement = {1, -1, 0, 0};
  10.     private static final int[] colMovement = {0, 0, 1, -1};
  11.     private static boolean isPlayerDead = false;
  12.  
  13.     public static void main(String[] args) {
  14.         Scanner scanner = new Scanner(System.in);
  15.  
  16.         int[] dimensions = Arrays.stream(scanner.nextLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();
  17.         int rows = dimensions[0];
  18.         int cols = dimensions[1];
  19.  
  20.         char[][] matrix = new char[rows][cols];
  21.         ArrayDeque<Integer> queueOfPositions = new ArrayDeque<>();
  22.         for (int row = 0; row < matrix.length; row++) {
  23.             String[] input = scanner.nextLine().split("");
  24.             for (int col = 0; col < matrix[row].length; col++) {
  25.                 matrix[row][col] = input[col].charAt(0);
  26.                 if (matrix[row][col] == 'B') {
  27.                     queueOfPositions.offer(row);
  28.                     queueOfPositions.offer(col);
  29.                 }
  30.                 if (matrix[row][col] == 'P') {
  31.                     playerPosition[0] = row;
  32.                     playerPosition[1] = col;
  33.                     matrix[row][col] = '.';
  34.                 }
  35.             }
  36.         }
  37.  
  38.         String[] cmdArgs = scanner.nextLine().split("");
  39.  
  40.         int counter = -1;
  41.         while (++counter < cmdArgs.length && !isPlayerDead) {
  42.             String command = cmdArgs[counter];
  43.  
  44.             moveBunnies(matrix, queueOfPositions);
  45.  
  46.             int playerRow = playerPosition[0];
  47.             int playerCol = playerPosition[1];
  48.  
  49.             switch (command) {
  50.                 case "R":
  51.                     playerCol += 1;
  52.                     break;
  53.                 case "L":
  54.                     playerCol -= 1;
  55.                     break;
  56.                 case "U":
  57.                     playerRow -= 1;
  58.                     break;
  59.                 case "D":
  60.                     playerRow += 1;
  61.                     break;
  62.             }
  63.  
  64.             if (!isInBounds(playerRow, playerCol, matrix)) {
  65.                 break;
  66.             }
  67.  
  68.             playerPosition[0] = playerRow;
  69.             playerPosition[1] = playerCol;
  70.  
  71.             if (!isCellFree(playerRow, playerCol, matrix)) {
  72.                 isPlayerDead = true;
  73.                 break;
  74.             }
  75.         }
  76.  
  77.         printCharMatrix(matrix);
  78.         if (isPlayerDead) {
  79.             System.out.print("dead: ");
  80.         } else {
  81.             System.out.print("won: ");
  82.         }
  83.         System.out.println(playerPosition[0] + " " + playerPosition[1]);
  84.     }
  85.  
  86.     private static void printCharMatrix(char[][] finalMatrix) {
  87.         for (char[] matrix : finalMatrix) {
  88.             for (char symbol : matrix) {
  89.                 System.out.print(symbol);
  90.             }
  91.             System.out.println();
  92.         }
  93.     }
  94.  
  95.     private static void moveBunnies(char[][] matrix, ArrayDeque<Integer> queueOfPositions) {
  96.         int lengthOperations = queueOfPositions.size() / 2;
  97.         for (int i = 0; i < lengthOperations; i++) {
  98.             int row = queueOfPositions.poll();
  99.             int col = queueOfPositions.poll();
  100.             for (int j = 0; j < rowMovement.length; j++) {
  101.                 int newRow = row + rowMovement[j];
  102.                 int newCol = col + colMovement[j];
  103.  
  104.                 if (!isInBounds(newRow, newCol, matrix)) {
  105.                     continue;
  106.                 }
  107.                 if (matrix[newRow][newCol] == 'B') {
  108.                     continue;
  109.                 }
  110.                 matrix[newRow][newCol] = 'B';
  111.                 queueOfPositions.offer(newRow);
  112.                 queueOfPositions.offer(newCol);
  113.             }
  114.         }
  115.     }
  116.  
  117.     private static boolean isCellFree(int row, int col, char[][] matrix) {
  118.         return matrix[row][col] == '.';
  119.     }
  120.  
  121.     private static boolean isInBounds(int row, int col, char[][] matrix) {
  122.         return row >= 0 && row < matrix.length && col >= 0 && col < matrix[row].length;
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement