Advertisement
Ligh7_of_H3av3n

05. Matrix shuffling

May 17th, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. package Uprajnenie;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class MatrixShuffling {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.  
  10.         // Read matrix dimensions
  11.         int rows = scanner.nextInt();
  12.         int cols = scanner.nextInt();
  13.         scanner.nextLine(); // Consume newline
  14.  
  15.         // Read the matrix
  16.         String[][] matrix = new String[rows][cols];
  17.         for (int i = 0; i < rows; i++) {
  18.             String[] rowValues = scanner.nextLine().split("\\s+");
  19.             for (int j = 0; j < cols; j++) {
  20.                 matrix[i][j] = rowValues[j];
  21.             }
  22.         }
  23.  
  24.         // Perform operations
  25.         while (true) {
  26.             String command = scanner.nextLine();
  27.             if (command.equals("END")) {
  28.                 break;
  29.             }
  30.  
  31.             String[] tokens = command.split("\\s+");
  32.             if (tokens.length != 5 || !tokens[0].equals("swap")) {
  33.                 System.out.println("Invalid input!");
  34.                 continue;
  35.             }
  36.  
  37.             try {
  38.                 int row1 = Integer.parseInt(tokens[1]);
  39.                 int col1 = Integer.parseInt(tokens[2]);
  40.                 int row2 = Integer.parseInt(tokens[3]);
  41.                 int col2 = Integer.parseInt(tokens[4]);
  42.  
  43.                 if (!isValidCell(matrix, row1, col1) || !isValidCell(matrix, row2, col2)) {
  44.                     System.out.println("Invalid input!");
  45.                     continue;
  46.                 }
  47.  
  48.                 String temp = matrix[row1][col1];
  49.                 matrix[row1][col1] = matrix[row2][col2];
  50.                 matrix[row2][col2] = temp;
  51.  
  52.                 printMatrix(matrix);
  53.             } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
  54.                 System.out.println("Invalid input!");
  55.             }
  56.         }
  57.     }
  58.  
  59.     // Method to check if a cell is valid
  60.     private static boolean isValidCell(String[][] matrix, int row, int col) {
  61.         return row >= 0 && row < matrix.length && col >= 0 && col < matrix[0].length;
  62.     }
  63.  
  64.     // Method to print the matrix
  65.     private static void printMatrix(String[][] matrix) {
  66.         for (String[] row : matrix) {
  67.             for (String cell : row) {
  68.                 System.out.print(cell + " ");
  69.             }
  70.             System.out.println();
  71.         }
  72.     }
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement