desislava_topuzakova

03. Diagonal Difference

May 25th, 2021
1,756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.28 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.                 command = scanner.nextLine();
  25.                 continue;
  26.             } else {
  27.                 //валидна
  28.                 String [] tokens = command.split(" ");
  29.                 int rowFirst = Integer.parseInt(tokens[1]);
  30.                 int colFirst = Integer.parseInt(tokens[2]);
  31.                 int rowSecond = Integer.parseInt(tokens[3]);
  32.                 int colSecond =Integer.parseInt(tokens[4]);
  33.  
  34.                 String element1 = matrix[rowFirst][colFirst];
  35.                 String element2 = matrix[rowSecond][colSecond];
  36.  
  37.                 matrix[rowFirst][colFirst] = element2;
  38.                 matrix[rowSecond][colSecond] = element1;
  39.  
  40.                 printMatrix(matrix, rows, cols);
  41.             }
  42.             command = scanner.nextLine();
  43.         }
  44.  
  45.     }
  46.  
  47.     private static boolean validateCommand(String command, int rows, int cols) {
  48.         //swap 0 0 1 1
  49.         String [] tokens = command.split(" "); //["swap", "0", "0", "1", "1"]
  50.         // 1. команда swap
  51.         String commandName = tokens[0];
  52.         if (!commandName.equals("swap")) {
  53.             return false;
  54.         }
  55.         //2. брой на координати
  56.         if(tokens.length != 5) {
  57.             return false;
  58.         }
  59.         // 3. коордианти
  60.         int rowFirst = Integer.parseInt(tokens[1]);
  61.         int colFirst = Integer.parseInt(tokens[2]);
  62.         int rowSecond = Integer.parseInt(tokens[3]);
  63.         int colSecond =Integer.parseInt(tokens[4]);
  64.  
  65.         if(rowFirst < 0 || rowFirst >= rows || colFirst < 0 ||
  66.                 colFirst >= cols || rowSecond < 0 || rowSecond >= rows || colSecond < 0 || colSecond >= cols) {
  67.             return false;
  68.         }
  69.  
  70.         return true;
  71.  
  72.  
  73.     }
  74.  
  75.     //fill matrix
  76.     private static void fillMatrix(Scanner scanner, String[][] matrix, int rows, int cols) {
  77.         for (int row = 0; row < rows; row++) {
  78.             matrix[row] = scanner.nextLine().split("\\s+");
  79.         }
  80.     }
  81.  
  82.     //print matrix
  83.     public static void printMatrix (String [][] matrix, int rows, int cols) {
  84.         for (int row = 0; row < rows; row++) {
  85.             for (int col = 0; col < cols; col++) {
  86.                 System.out.print(matrix[row][col] + " ");
  87.             }
  88.             System.out.println();
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment