Advertisement
Guest User

Untitled

a guest
May 26th, 2021
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. package com.company;
  2.  
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7.  
  8. public class Main {
  9.  
  10.     public static void main(String[] args) throws IOException {
  11.  
  12.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  13.         int N;
  14.         int M;
  15.         String[] size = reader.readLine().split("\\s+");
  16.         N = Integer.parseInt(size[0]);
  17.         M = Integer.parseInt(size[1]);
  18.  
  19.         String[][] arr = new String[N][M];
  20.  
  21.         for (int row = 0; row < N; row++) {
  22.             String[] tokens = reader.readLine().split("\\s+");
  23.             for (int col = 0; col < M; col++) {
  24.                 arr[row][col] = tokens[col];
  25.             }
  26.         }
  27.  
  28.         String[] line;
  29.         int a;
  30.         int b;
  31.         int x;
  32.         int y;
  33.  
  34.         while (true) {
  35.             line = reader.readLine().split(" ");
  36.             if (line[0].equals("END")) {
  37.                 break;
  38.             }
  39.             if (line.length == 5) {
  40.                 if (line[0].equals("swap")) {
  41.                     a = Integer.parseInt(line[1]);
  42.                     b = Integer.parseInt(line[2]);
  43.                     x = Integer.parseInt(line[3]);
  44.                     y = Integer.parseInt(line[4]);
  45.                     if (a >= N || b >= M || x >= N || y >= M) {
  46.                         System.out.println("Invalid input!");
  47.                     } else {
  48.                         String c = arr[a][b];
  49.                         arr[a][b] = arr[x][y];
  50.                         arr[x][y] = c;
  51.                         for (int i = 0; i < N; i++) {
  52.                             for (int j = 0; j < M; j++) {
  53.                                 System.out.print(arr[i][j] + " ");
  54.                             }
  55.                             System.out.println();
  56.                         }
  57.                     }
  58.                 }
  59.             } else if (!line[0].equals("")) {
  60.                 System.out.println("Invalid input!");
  61.             }
  62.         }
  63.     }
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement