Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.87 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class Ex05MatrixShuffling {
  6.     private static BufferedReader reader;
  7.     private static String[][] matrix;
  8.     private static int rows;
  9.     private static int cols;
  10.  
  11.     static {
  12.         reader = new BufferedReader(new InputStreamReader(System.in));
  13.     }
  14.  
  15.     public static void main(String[] args) throws IOException {
  16.         readInput();
  17.  
  18.         String input;
  19.         while (! "END".equals(input = reader.readLine())) {
  20.             String[] tokens = input.split("\\s+");
  21.             if (! input.startsWith("swap") || tokens.length != 5) {
  22.                 System.out.println("Invalid input!");
  23.                 continue;
  24.             }
  25.  
  26.             int rowOne = Integer.parseInt(tokens[1]);
  27.             int colOne = Integer.parseInt(tokens[2]);
  28.             int rowTwo = Integer.parseInt(tokens[3]);
  29.             int colTwo = Integer.parseInt(tokens[4]);
  30.  
  31.             if (isOutOfRange(rowOne, colOne, rowTwo, colTwo)) {
  32.                 System.out.println("Invalid input!");
  33.                 continue;
  34.             }
  35.  
  36.             String temp = matrix[rowOne][colOne];
  37.             matrix[rowOne][colOne] = matrix[rowTwo][colTwo];
  38.             matrix[rowTwo][colTwo] = temp;
  39.             System.out.print(getMatrixString());
  40.         }
  41.     }
  42.  
  43.     private static boolean isOutOfRange(int rowOne, int colOne, int rowTwo, int colTwo) {
  44.         if (rowOne < 0 || rowOne >= rows) {
  45.             return true;
  46.         } else if (rowTwo < 0 || rowTwo >= rows) {
  47.             return true;
  48.         } else if (colOne < 0 || colOne >= cols) {
  49.             return true;
  50.         } else if (colTwo < 0 || colTwo >= cols) {
  51.             return true;
  52.         }
  53.         return false;
  54.     }
  55.  
  56.     private static void readInput() throws IOException {
  57.         String[] size = reader.readLine().split("\\s+");
  58.         rows = Integer.parseInt(size[0]);
  59.         cols = Integer.parseInt(size[1]);
  60.         matrix = new String[rows][cols];
  61.  
  62.         fillMatrix();
  63.     }
  64.  
  65.     private static void fillMatrix() throws IOException {
  66.         for (int row = 0; row < rows; row++) {
  67.             String[] tokens = reader.readLine().split("\\s+");
  68.             for (int col = 0; col < cols; col++) {
  69.                 matrix[row][col] = tokens[col];
  70.             }
  71.         }
  72.     }
  73.  
  74.     private static String getMatrixString() {
  75.         StringBuilder output = new StringBuilder();
  76.         for (int row = 0; row < rows; row++) {
  77.             StringBuilder rowOutput = new StringBuilder();
  78.             for (int col = 0; col < cols; col++) {
  79.                 rowOutput.append(matrix[row][col])
  80.                         .append(" ");
  81.             }
  82.             output.append(rowOutput.toString().trim())
  83.                     .append(System.lineSeparator());
  84.         }
  85.         return output.toString();
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement