Advertisement
Guest User

Java Matrix

a guest
Dec 7th, 2019
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.69 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Arrays;
  5.  
  6. public class firstStQexercise {
  7.     public static void main(String[] args) throws IOException {
  8.         BufferedReader reader = new BufferedReader
  9.                 (new InputStreamReader(System.in));
  10.  
  11.         int[] dimension = Arrays.stream(reader.readLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();
  12.  
  13.         int rows = dimension[0];
  14.         int columns = dimension[1];
  15.  
  16.         String [][] matrix = new String[rows][columns];
  17.         boolean [][] bunniesIndexes = new boolean[rows][columns];
  18.  
  19.         int [] playerIndexes = new int[2];
  20.  
  21.         for (int r = 0; r < rows; r++) {
  22.             String [] currentRow = reader.readLine().split("");
  23.             matrix[r] = currentRow;
  24.             for (int c = 0; c < columns; c++) {
  25.                 if (matrix[r][c].equals("P")) {
  26.                     playerIndexes[0] = r;
  27.                     playerIndexes[1] = c;
  28.                 }
  29.                 else if (matrix[r][c].equals("B")){
  30.                     bunniesIndexes[r][c] = true;
  31.                 }
  32.             }
  33.         }
  34.  
  35.         String [] playerDirections = reader.readLine().split("");
  36.  
  37.         for (String direction : playerDirections) {
  38.             int currentRow = playerIndexes[0];
  39.             int currentCol = playerIndexes[1];
  40.  
  41.             switch (direction) {
  42.                 case "U":
  43.                     currentRow--;
  44.                     break;
  45.  
  46.                 case "D":
  47.                     currentRow++;
  48.                     break;
  49.  
  50.                 case "R":
  51.                     currentCol++;
  52.                     break;
  53.  
  54.                 case "L":
  55.                     currentCol--;
  56.                     break;
  57.             }
  58.  
  59.             if (isInBounds(matrix, currentRow, currentCol)) {
  60.                 if (!bunniesIndexes[currentRow][currentCol]) {
  61.                     matrix[playerIndexes[0]][playerIndexes[1]] = ".";
  62.                     playerIndexes[0] = currentRow;
  63.                     playerIndexes[1] = currentCol;
  64.                     matrix[playerIndexes[0]][playerIndexes[1]] = "P";
  65.                     matrix = multipleBunnies(matrix, bunniesIndexes);
  66.                     if (bunniesIndexes[playerIndexes[0]][playerIndexes[1]]) {
  67.                         printMatrix(matrix);
  68.                         System.out.println(String.format("dead: %d %d", playerIndexes[0], playerIndexes[1]));
  69.                         return;
  70.                     }
  71.                 } else {
  72.                     matrix = multipleBunnies(matrix, bunniesIndexes);
  73.                     printMatrix(matrix);
  74.                     System.out.println(String.format("dead: %d %d", currentRow, currentCol));
  75.                     return;
  76.                 }
  77.             } else  {
  78.                 matrix = multipleBunnies(matrix, bunniesIndexes);
  79.                 if (matrix[playerIndexes[0]][playerIndexes[1]].equals("P")) {
  80.                     matrix[playerIndexes[0]][playerIndexes[1]] = ".";
  81.                 }
  82.                 printMatrix(matrix);
  83.                 System.out.printf("won: %d %d%n", playerIndexes[0], playerIndexes[1]);
  84.             }
  85.         }
  86.     }
  87.  
  88.     private static String[][] multipleBunnies (String[][] matrix, boolean [][] bunniesIndexes) {
  89.  
  90.         for (int r = 0; r < matrix.length; r++) {
  91.             for (int c = 0; c < matrix[r].length; c++) {
  92.                 if (bunniesIndexes[r][c]) {
  93.                     if (isInBounds(matrix, r - 1, c)) {
  94.                         matrix[r - 1][c] = "B";
  95.                     }
  96.                     if (isInBounds(matrix, r + 1, c)) {
  97.                         matrix[r + 1][c] = "B";
  98.                     }
  99.                     if (isInBounds(matrix, r, c + 1)){
  100.                         matrix[r][c + 1] = "B";
  101.                     }
  102.                     if (isInBounds(matrix, r, c - 1)){
  103.                         matrix[r][c - 1] = "B";
  104.                     }
  105.                 }
  106.             }
  107.         }
  108.         for (int r = 0; r < matrix.length; r++) {
  109.             for (int c = 0; c < matrix[r].length; c++) {
  110.                 if (matrix[r][c].equals("B")){
  111.                     bunniesIndexes[r][c] = true;
  112.                 }
  113.             }
  114.         }
  115.         return matrix;
  116.     }
  117.  
  118.     private static void printMatrix (String [][] matrix){
  119.         for (String[] strings : matrix) {
  120.             for (String string : strings) {
  121.                 System.out.print(string);
  122.             }
  123.             System.out.println();
  124.         }
  125.     }
  126.  
  127.     private static boolean isInBounds (String [][] matrix, int row, int column) {
  128.         return row >= 0 && row < matrix.length && column >= 0 && column < matrix[0].length;
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement