Skip to content

Latest commit

 

History

History
206 lines (164 loc) · 4.66 KB

16935.md

File metadata and controls

206 lines (164 loc) · 4.66 KB
package algo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Main {
    static int n;
    static int m;
    static int[][] map;
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] start = reader.readLine().split(" ");
        n = Integer.parseInt(start[0]);
        m = Integer.parseInt(start[1]);
        int r = Integer.parseInt(start[2]);

        map = new int[n][m];

        for (int i = 0; i < n; i++) {
            String[] line = reader.readLine().split(" ");
            for (int j = 0; j < m; j++) {
                map[i][j] = Integer.parseInt(line[j]);
            }
        }


        String[] commands = reader.readLine().split(" ");
        for (int i = 0; i < r; i++) {
            int command = Integer.parseInt(commands[i]);
            switch (command){
                case 1:
                    upDown();
                    break;
                case 2:
                    leftRight();
                    break;
                case 3:
                    right();
                    break;
                case 4:
                    left();
                    break;
                case 5:
                    shuffle1();
                    break;
                case 6:
                    shuffle2();
                    break;
            }
        }



        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                System.out.print(map[i][j] + " ");
            }
            System.out.println();
        }


    }

    private static void shuffle2() {
        int[][] newMap = new int[n][m];

        // 1-> 4
        for (int i = 0; i < n / 2; i++) {
            for (int j = 0; j < m / 2; j++) {
                newMap[n/2+i][j] = map[i][j];
            }
        }

        // 4->3
        for (int i = n / 2; i < n; i++) {
            for (int j = 0; j < m / 2; j++) {
                newMap[i][j+m/2] = map[i][j];
            }
        }

        // 3->2
        for (int i = n / 2; i < n; i++) {
            for (int j = m / 2; j < m; j++) {
                newMap[i-n/2][j] = map[i][j];
            }
        }

        // 2->1

        for (int i = 0; i < n/2; i++) {
            for (int j = m / 2; j < m; j++) {
                newMap[i][j-m/2] = map[i][j];
            }
        }

        copyMap(newMap);
    }

    private static void shuffle1() {
        int[][] newMap = new int[n][m];

        // 1-> 2
        for (int i = 0; i < n / 2; i++) {
            for (int j = 0; j < m / 2; j++) {
                newMap[i][m / 2 + j] = map[i][j];
            }
        }

        // 2->3
        for (int i = 0; i < n/2; i++) {
            for (int j = m / 2; j < m; j++) {
                newMap[n / 2 + i][j] = map[i][j];
            }
        }

        // 3->4
        for (int i = n / 2; i < n; i++) {
            for (int j = m / 2; j < m; j++) {
                newMap[i][j-m/2] = map[i][j];
            }
        }

        // 4->1

        for (int i = n / 2; i < n; i++) {
            for (int j = 0; j < m / 2; j++) {
                newMap[i-n/2][j] = map[i][j];
            }
        }

        copyMap(newMap);
    }

    private static void left() {
        int[][] newMap = new int[m][n];


        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                newMap[m-1-j][i] = map[i][j];
            }

        }

        int temp = n;
        n = m;
        m = temp;
        copyMap(newMap);
    }

    private static void right() {
        int[][] newMap = new int[m][n];
        int index = n - 1;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                newMap[j][index] = map[i][j];
            }
            index--;
        }


        int temp = n;
        n = m;
        m = temp;
        copyMap(newMap);
    }

    private static void copyMap(int[][] newMap) {
        map = new int[n][m];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                map[i][j] = newMap[i][j];
            }
        }
    }


    private static void upDown() {
        for (int i = 0; i < n / 2; i++) {
            for (int j = 0; j < m; j++) {
                swap(i,j,n-1-i,j,map);
            }
        }
    }

    private static void leftRight() {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m / 2; j++) {
                swap(i,j,i,m-1-j,map);
            }
        }
    }

    private static void swap(int i, int j, int i1, int j1, int[][] map) {
        int temp = map[i][j];
        map[i][j] = map[i1][j1];
        map[i1][j1] = temp;
    }
}