desislava_topuzakova

05. Matrix shuffling

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