desislava_topuzakova

05. Matrix Suffling

May 25th, 2021
1,407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Demo {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         //вход
  7.         //валидация: 1. команда swap   2. брой на координати   3. коордианти
  8.         //разменям елементите
  9.         int [] dimensions =  Arrays.stream(scanner.nextLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();
  10.         int rows = dimensions[0];
  11.         int cols = dimensions[1];
  12.  
  13.  
  14.         String[][] matrix = new String[rows][cols];
  15.         fillMatrix(scanner, matrix, rows, cols);
  16.  
  17.         String command = scanner.nextLine();
  18.         while(!command.equals("END")) {
  19.             //ако е невалидна -> Invalid input! -> нова команда
  20.             //ако е валидна -> разменяме
  21.             if(!validateCommand(command, rows, cols)) {
  22.                 //невалидна
  23.                 System.out.println("Invalid input!");
  24.             } else {
  25.                 //валидна
  26.                 String [] tokens = command.split(" ");
  27.                 int rowFirst = Integer.parseInt(tokens[1]);
  28.                 int colFirst = Integer.parseInt(tokens[2]);
  29.                 int rowSecond = Integer.parseInt(tokens[3]);
  30.                 int colSecond =Integer.parseInt(tokens[4]);
  31.  
  32.                 String element1 = matrix[rowFirst][colFirst];
  33.                 String element2 = matrix[rowSecond][colSecond];
  34.  
  35.                 matrix[rowFirst][colFirst] = element2;
  36.                 matrix[rowSecond][colSecond] = element1;
  37.  
  38.                 printMatrix(matrix, rows, cols);
  39.             }
  40.             command = scanner.nextLine();
  41.         }
  42.  
  43.     }
  44.  
  45.     private static boolean validateCommand(String command, int rows, int cols) {
  46.         //swap 0 0 1 1
  47.         String [] tokens = command.split(" "); //["swap", "0", "0", "1", "1"]
  48.         // 1. команда swap
  49.         String commandName = tokens[0];
  50.         if (!commandName.equals("swap")) {
  51.             return false;
  52.         }
  53.         //2. брой на координати
  54.         if(tokens.length != 5) {
  55.             return false;
  56.         }
  57.         // 3. коордианти
  58.         int rowFirst = Integer.parseInt(tokens[1]);
  59.         int colFirst = Integer.parseInt(tokens[2]);
  60.         int rowSecond = Integer.parseInt(tokens[3]);
  61.         int colSecond =Integer.parseInt(tokens[4]);
  62.  
  63.         if(rowFirst < 0 || rowFirst >= rows || colFirst < 0 ||
  64.                 colFirst >= cols || rowSecond < 0 || rowSecond >= rows || colSecond < 0 || colSecond >= cols) {
  65.             return false;
  66.         }
  67.  
  68.         return true;
  69.  
  70.  
  71.     }
  72.  
  73.     //fill matrix
  74.     private static void fillMatrix(Scanner scanner, String[][] matrix, int rows, int cols) {
  75.         for (int row = 0; row < rows; row++) {
  76.             matrix[row] = scanner.nextLine().split("\\s+");
  77.         }
  78.     }
  79.  
  80.     //print matrix
  81.     public static void printMatrix (String [][] matrix, int rows, int cols) {
  82.         for (int row = 0; row < rows; row++) {
  83.             for (int col = 0; col < cols; col++) {
  84.                 System.out.print(matrix[row][col] + " ");
  85.             }
  86.             System.out.println();
  87.         }
  88.     }
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment