Advertisement
Mirineo

11. Reverse Matrix Diagonals

Feb 7th, 2021
793
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. package MultidimensionalArrays.Exercise;
  2.  
  3. import java.util.*;
  4.  
  5. public class ReverseMatrixDiagonals {
  6.     public static void main(String[] args) {
  7.  
  8.         Scanner scanner = new Scanner(System.in);
  9.         int rows = scanner.nextInt();
  10.         int cols = scanner.nextInt();
  11.         scanner.nextLine();
  12.  
  13.         int[][] matrix = buildMatrix(rows, cols, scanner);
  14.  
  15.         int[][] reversedDiagonalsMatrix = reverseDiagonalsOfMatrix(matrix, rows, cols);
  16.         printMatrix(reversedDiagonalsMatrix);
  17.     }
  18.  
  19.     private static void printMatrix(int[][] matrix) {
  20.         Arrays.stream(matrix).forEach(e -> System.out.println( String.join(" ", Arrays.toString(e).replaceAll("([\\[\\],])", ""))));
  21.     }
  22.  
  23.     private static boolean isValid(int row, int col, int[][] chamber) {
  24.         return row >= 0 && col >= 0 && row < chamber.length && col < chamber[row].length;
  25.     }
  26.  
  27.     private static int[][] reverseDiagonalsOfMatrix(int[][] matrix, int rows, int cols){
  28.         ArrayDeque<Integer> integers = new ArrayDeque<>();
  29.  
  30.         for (int row = 0; row < rows; row++) {
  31.             int count = 0;
  32.             while (isValid(row - count, count, matrix)){
  33.                 int tempRow = row - count;
  34.                 integers.offer(matrix[tempRow][count]);
  35.                 count++;
  36.             }
  37.         }
  38.  
  39.         for (int col = 1; col < cols; col++) {
  40.             int r = matrix.length - 1;
  41.             int count = 0;
  42.             while (isValid(r - count, col + count, matrix)){
  43.                 int tempCol = col + count;
  44.                 int tempRow = r - count;
  45.                 integers.offer(matrix[tempRow][tempCol]);
  46.                 count++;
  47.             }
  48.         }
  49.  
  50.         int[][] newMatrix = new int[cols + rows - 1][];
  51.         int count = 1;
  52.  
  53.         for (int row = cols + rows - 2; row >= 0; row--) {
  54.             int[] arr = new int[count];
  55.             for (int i = 0; i < count; i++) {
  56.                 arr[i] = integers.poll();
  57.             }
  58.  
  59.             newMatrix[row] = arr;
  60.  
  61.             if (count < matrix.length && row > matrix.length - 1){
  62.                 count++;
  63.             }else if(row < matrix.length){
  64.                 count--;
  65.             }
  66.         }
  67.  
  68.         return newMatrix;
  69.  
  70.     }
  71.  
  72.     private static int[][] buildMatrix(int rows, int cols, Scanner scanner) {
  73.         int[][] matrix = new int[rows][cols];
  74.         for (int r = 0; r < rows; r++) {
  75.             matrix[r] = Arrays.stream(scanner.nextLine().split("\\s+"))
  76.                     .mapToInt(Integer::parseInt)
  77.                     .toArray();
  78.         }
  79.  
  80.         return matrix;
  81.     }
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement