150 转圈打印矩阵
Table of Contents

ac

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static Scanner sc = new Scanner(System.in);

    public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    public static long mod = (long)Math.pow(2,29);

    public static void main(String[] args) throws Exception {
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] arr = new int[n][m];
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++) {
                int val = sc.nextInt();
                arr[i][j] = val;
            }
        }
        boolean flag = true;

        int staI = 0;
        int staJ = 0;
        int endI = n-1;
        int endJ = m-1;
        while (staI <= endI && staJ <= endJ){
            if(staI == endI){
                for(int j=staJ; j<=endJ; j++){
                    int val = arr[staI][j];
                    if(flag){
                        System.out.print(val);
                        flag = false;
                    }else {
                        System.out.print(" " + val);
                    }
                }
            }else if(staJ == endJ){
                for(int i=staI; i<=endI; i++){
                    int val = arr[i][staJ];
                    if(flag){
                        System.out.print(val);
                        flag = false;
                    }else {
                        System.out.print(" " + val);
                    }
                }
            }else {
                // 环形打印
                for (int j = staJ; j <= endJ; j++) {
                    int val = arr[staI][j];
                    if (flag) {
                        System.out.print(val);
                        flag = false;
                    }
                    else {
                        System.out.print(" " + val);
                    }
                }
                for (int i = staI + 1; i < endI; i++) {
                    int val = arr[i][endJ];
                    if (flag) {
                        System.out.print(val);
                        flag = false;
                    }
                    else {
                        System.out.print(" " + val);
                    }
                }
                for (int j = endJ; j >= staJ; j--) {
                    int val = arr[endI][j];
                    if (flag) {
                        System.out.print(val);
                        flag = false;
                    }
                    else {
                        System.out.print(" " + val);
                    }
                }
                for (int i = endI - 1; i > staI; i--) {
                    int val = arr[i][staJ];
                    if (flag) {
                        System.out.print(val);
                        flag = false;
                    }
                    else {
                        System.out.print(" " + val);
                    }
                }
            }
            staI++;
            staJ++;
            endI--;
            endJ--;
        }
        System.out.println();
    }

}
/*
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10


4 3
1 2 3
5 6 7
9 10 11
13 14 15

4 2
1 2
5 6
9 10
13 14

4 1
1
5
9
13

2 4
1 2 3 4
5 6 7 8

3 4
1 2 3 4
5 6 7 8
9 10 11 12
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static Scanner sc = new Scanner(System.in);

    public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    public static long mod = (long)Math.pow(2,29);

    public static void main(String[] args) throws Exception {
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] arr = new int[n][m];
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++) {
                int val = sc.nextInt();
                arr[i][j] = val;
            }
        }
        List<Integer> ans = new ArrayList<>();

        int staI = 0;
        int staJ = 0;
        int endI = n-1;
        int endJ = m-1;
        while (staI <= endI){
            ans.addAll(printOne(arr, staI, staJ, endI, endJ));
            staI++;
            staJ++;
            endI--;
            endJ--;
        }
        System.out.print(ans.get(0));
        for(int i=1;i<n*m;i++){
            System.out.print(" " + ans.get(i));
        }
        System.out.println();
    }

    static List<Integer> printOne(int[][] arr, int staI, int staJ, int endI, int endJ){
        List<Integer> ans = new ArrayList<>();
        if(staI == endI){
            for(int j=staJ; j<=endJ; j++){
                ans.add(arr[staI][j]);
            }
            return ans;
        }
        if(staJ == endJ){
            for(int i=staI; i<=endI; i++){
                ans.add(arr[i][staJ]);
            }
            return ans;
        }
        // 环形打印
        for(int j=staJ;j<=endJ;j++){
            ans.add(arr[staI][j]);
        }
        for(int i=staI+1;i<endI;i++){
            ans.add(arr[i][endJ]);
        }
        for(int j=endJ;j>=staJ;j--){
            ans.add(arr[endI][j]);
        }
        for(int i=endI-1;i>staI;i--){
            ans.add(arr[i][staJ]);
        }
        return ans;
    }

}
/*
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10


4 3
1 2 3
5 6 7
9 10 11
13 14 15

4 2
1 2
5 6
9 10
13 14

4 1
1
5
9
13

2 4
1 2 3 4
5 6 7 8

3 4
1 2 3 4
5 6 7 8
9 10 11 12
*/