Advertisement
Tsuki11

Untitled

May 25th, 2020
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.29 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4.  
  5. public class E10_RadioactiveMutantVampireBunnies1 {
  6.     private static int[] playerPosition = new int[2];
  7.     private static final int[] rowMovement = {1, -1, 0, 0};
  8.     private static final int[] colMovement = {0, 0, 1, -1};
  9.     private static boolean isPlayerDead = false;
  10.  
  11.     public static void main(String[] args) {
  12.         Scanner scan = new Scanner(System.in);
  13.  
  14.         int[] dimensions = Arrays.stream(scan.nextLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();
  15.         int rows = dimensions[0];
  16.         int cols = dimensions[1];
  17.  
  18.         char[][] matrix = new char[rows][cols];
  19.         ArrayDeque<Integer> queueOfPositions = new ArrayDeque<>();
  20.         for (int row = 0; row < matrix.length; row++) {
  21.             String[] input = scan.nextLine().split("");
  22.             for (int col = 0; col < matrix[row].length; col++) {
  23.                 matrix[row][col] = input[col].charAt(0);
  24.                 if (matrix[row][col] == 'B') {
  25.                     queueOfPositions.offer(row);
  26.                     queueOfPositions.offer(col);
  27.                 }
  28.                 if (matrix[row][col] == 'P') {
  29.                     playerPosition[0] = row;
  30.                     playerPosition[1] = col;
  31.                     matrix[row][col] = '.';
  32.                 }
  33.             }
  34.         }
  35.  
  36.         String[] cmdArgs = scan.nextLine().split("");
  37.  
  38.         int counter = -1;
  39.         while (++counter < cmdArgs.length && !isPlayerDead) {
  40.             String command = cmdArgs[counter];
  41.             int playerRow = playerPosition[0];
  42.             int playerCol = playerPosition[1];
  43.  
  44.             switch (command) {
  45.                 case "R":
  46.                     playerCol += 1;
  47.                     break;
  48.                 case "L":
  49.                     playerCol -= 1;
  50.                     break;
  51.                 case "U":
  52.                     playerRow -= 1;
  53.                     break;
  54.                 case "D":
  55.                     playerRow += 1;
  56.                     break;
  57.             }
  58.             boolean isInGrid = isInBounds(playerRow, playerCol, matrix);
  59.  
  60.             if (!isInGrid) {
  61.                 break;
  62.             }
  63.             playerPosition[0] = playerRow;
  64.             playerPosition[1] = playerCol;
  65.  
  66.             if (!isCellFree(playerRow, playerCol, matrix)) {
  67.                 isPlayerDead = true;
  68.                 break;
  69.             }
  70.  
  71.             moveBunnies(matrix, queueOfPositions);
  72.             if (matrix[playerRow][playerCol] == 'B') {
  73.                 isPlayerDead = true;
  74.             }
  75.         }
  76.         moveBunnies(matrix, queueOfPositions);
  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