Advertisement
Tsuki11

Untitled

May 26th, 2020
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.10 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.  
  42.             moveBunnies(matrix, queueOfPositions);
  43.  
  44.             int playerRow = playerPosition[0];
  45.             int playerCol = playerPosition[1];
  46.  
  47.             switch (command) {
  48.                 case "R":
  49.                     playerCol += 1;
  50.                     break;
  51.                 case "L":
  52.                     playerCol -= 1;
  53.                     break;
  54.                 case "U":
  55.                     playerRow -= 1;
  56.                     break;
  57.                 case "D":
  58.                     playerRow += 1;
  59.                     break;
  60.             }
  61.  
  62.             if (!isInBounds(playerRow, playerCol, matrix)) {
  63.                 break;
  64.             }
  65.             playerPosition[0] = playerRow;
  66.             playerPosition[1] = playerCol;
  67.  
  68.             if (!isCellFree(playerRow, playerCol, matrix)) {
  69.                 isPlayerDead = true;
  70.                 break;
  71.             }
  72.         }
  73.  
  74.         printCharMatrix(matrix);
  75.         if (isPlayerDead) {
  76.             System.out.print("dead: ");
  77.         } else {
  78.             System.out.print("won: ");
  79.         }
  80.         System.out.println(playerPosition[0] + " " + playerPosition[1]);
  81.     }
  82.  
  83.     private static void printCharMatrix(char[][] finalMatrix) {
  84.         for (char[] matrix : finalMatrix) {
  85.             for (char symbol : matrix) {
  86.                 System.out.print(symbol);
  87.             }
  88.             System.out.println();
  89.         }
  90.     }
  91.  
  92.     private static void moveBunnies(char[][] matrix, ArrayDeque<Integer> queueOfPositions) {
  93.         int lengthOperations = queueOfPositions.size() / 2;
  94.         for (int i = 0; i < lengthOperations; i++) {
  95.             int row = queueOfPositions.poll();
  96.             int col = queueOfPositions.poll();
  97.             for (int j = 0; j < rowMovement.length; j++) {
  98.                 int newRow = row + rowMovement[j];
  99.                 int newCol = col + colMovement[j];
  100.  
  101.                 if (!isInBounds(newRow, newCol, matrix)) {
  102.                     continue;
  103.                 }
  104.                 if (matrix[newRow][newCol] == 'B') {
  105.                     continue;
  106.                 }
  107.                 matrix[newRow][newCol] = 'B';
  108.                 queueOfPositions.offer(newRow);
  109.                 queueOfPositions.offer(newCol);
  110.             }
  111.         }
  112.     }
  113.  
  114.     private static boolean isCellFree(int row, int col, char[][] matrix) {
  115.         return matrix[row][col] == '.';
  116.     }
  117.  
  118.     private static boolean isInBounds(int row, int col, char[][] matrix) {
  119.         return row >= 0 && row < matrix.length && col >= 0 && col < matrix[row].length;
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement