package view;
public class MainTest {
private static int CLOCKWISE = 1;
private static int ANTICLOCKWISE = 2;
public static void main(String[] args) {
int[][] matrix = {{1,2,3,4}, {5,6,7,8}, {10,9,0,-1}, {11,13,12,14}};
soutMatrix(matrix,"原始矩阵");
StringBuilder builder = new StringBuilder();
print(matrix, ANTICLOCKWISE, builder);
}
private static void print(int[][] matrix, int direction, StringBuilder builder) {
if(matrix.length == 0){
String out = builder.substring(0, builder.length() - 1);
System.out.println("[" + out + "]");
return;
}
if(direction == CLOCKWISE){
matrix = getTop(matrix, direction, builder);
matrix = getRight(matrix, direction, builder);
matrix = getButtom(matrix, direction, builder);
matrix = getLeft(matrix, direction, builder);
}else {
matrix = getLeft(matrix, direction, builder);
matrix = getButtom(matrix, direction, builder);
matrix = getRight(matrix, direction, builder);
matrix = getTop(matrix, direction, builder);
}
print(matrix, direction, builder);
}
private static int[][] getTop(int[][] matrix, int direction, StringBuilder builder) {
if(matrix.length == 0){
return matrix;
}
soutMatrix(matrix, "上方处理");
int[] arr = matrix[0];
if(direction == CLOCKWISE){
for (int i = 0; i < arr.length; i++) {
builder.append(arr[i]).append(",");
}
}else {
for (int i = arr.length - 1; i > -1; i--) {
builder.append(arr[i]).append(",");
}
}
int[][] newMatrix = new int[matrix.length-1][arr.length];
System.arraycopy(matrix, 1, newMatrix, 0, matrix.length - 1);
return newMatrix;
}
private static int[][] getRight(int[][] matrix, int direction, StringBuilder builder) {
if(matrix.length == 0){
return matrix;
}
soutMatrix(matrix, "右侧处理");
if(direction == CLOCKWISE){
for (int[] ints : matrix) {
builder.append(ints[ints.length - 1] + ",");
}
}else {
for (int i = matrix.length - 1; i > -1; i--) {
builder.append(matrix[i][matrix[i].length - 1] + ",");
}
}
int[][] newMatrix = new int[matrix.length][matrix[0].length - 1];
for (int i = 0; i < matrix.length; i++) {
if (matrix[i].length - 1 >= 0) {
System.arraycopy(matrix[i], 0, newMatrix[i], 0, matrix[i].length - 1);
}
}
return newMatrix;
}
private static int[][] getLeft(int[][] matrix, int direction, StringBuilder builder) {
if(matrix.length == 0){
return matrix;
}
soutMatrix(matrix, "左侧处理");
if(direction == CLOCKWISE){
for (int i = matrix.length - 1; i > -1; i--) {
builder.append(matrix[i][0] + ",");
}
}else {
for (int[] ints : matrix) {
builder.append(ints[0] + ",");
}
}
int[][] newMatrix = new int[matrix.length][matrix[0].length - 1];
for (int i = 0; i < matrix.length; i++) {
if (matrix[i].length - 1 >= 0) {
System.arraycopy(matrix[i], 1, newMatrix[i], 0, matrix[i].length - 1);
}
}
return newMatrix;
}
private static int[][] getButtom(int[][] matrix, int direction, StringBuilder builder) {
if(matrix.length == 0){
return matrix;
}
soutMatrix(matrix, "下方处理");
int[] arr = matrix[matrix.length - 1];
if(direction == CLOCKWISE){
for (int i = arr.length - 1; i > -1; i--) {
builder.append(arr[i]).append(",");
}
}else {
for (int i = 0; i < arr.length; i++) {
builder.append(arr[i]).append(",");
}
}
int[][] newMatrix = new int[matrix.length-1][arr.length];
System.arraycopy(matrix, 0, newMatrix, 0, matrix.length - 1);
return newMatrix;
}
private static void soutMatrix(int[][] matrix, String note){
System.out.println(note);
for (int i = 0; i < matrix.length; i++) {
for (int i1 = 0; i1 < matrix[i].length; i1++) {
if(i1 == matrix[i].length -1){
System.out.print(matrix[i][i1]);
}else {
System.out.print(matrix[i][i1] + ",");
}
}
System.out.println();
}
System.out.println("-------------------------");
}
}