Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class Main {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- int N;
- int M;
- String[] size = reader.readLine().split("\\s+");
- N = Integer.parseInt(size[0]);
- M = Integer.parseInt(size[1]);
- String[][] arr = new String[N][M];
- for (int row = 0; row < N; row++) {
- String[] tokens = reader.readLine().split("\\s+");
- for (int col = 0; col < M; col++) {
- arr[row][col] = tokens[col];
- }
- }
- String[] line;
- int a;
- int b;
- int x;
- int y;
- while (true) {
- line = reader.readLine().split(" ");
- if (line[0].equals("END")) {
- break;
- }
- if (line.length == 5) {
- if (line[0].equals("swap")) {
- a = Integer.parseInt(line[1]);
- b = Integer.parseInt(line[2]);
- x = Integer.parseInt(line[3]);
- y = Integer.parseInt(line[4]);
- if (a >= N || b >= M || x >= N || y >= M) {
- System.out.println("Invalid input!");
- } else {
- String c = arr[a][b];
- arr[a][b] = arr[x][y];
- arr[x][y] = c;
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < M; j++) {
- System.out.print(arr[i][j] + " ");
- }
- System.out.println();
- }
- }
- }
- } else if (!line[0].equals("")) {
- System.out.println("Invalid input!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement