This article describes the Java implementation of outputting a digital matrix function clockwise or counterclockwise. Share it for your reference, as follows:
Topic: Print a numeric matrix starting from 1 from the outside to the top left corner according to the specified length and width and output direction. The following figure
The code and comments are as follows:
public class NumberMatrix { public static void main(String[] args) { int width = 25; int height = 12; boolean clockwise = false; System.out.println("Wulin.com test results:"); outputMatrix(width, height, clockwise); } /** * Print a number matrix starting from 1 from the outside to the inside according to the specified length and width and output direction, and the starting position of the matrix is in the upper left corner. * * @param width matrix width * @param height matrix height * @param clockwise Whether it is clockwise*/ private static void outputMatrix(int width, int height, boolean clockwise) { // First judge the number of digits of the largest number to determine how the output is aligned int numLength = (int) Math.log10(width * height) + 1; // Determine the format of the output (maximum number of digits + 1 space) String format = "%" + (numLength + 1) + "d"; // Define the two-dimensional array to be output, note that the dimensions are from high to low// At this time, matrix The values of all elements in it are 0 int[][] matrix = new int[height][width]; // Define a position pointer and a counter, the position pointer is moved, and the counter is responsible for incrementing. The incremented number // is filled into the matrix. When width * height numbers are filled, this matrix is completed. // Note that the first element of the position pointer here corresponds to the first dimension y of the matrix, and the second element corresponds to the second dimension x. int[] pointer = {0, 0}; int counter = 1; // Define the current direction of movement: 1, 2, 3, and 4 represent upper, right, lower, and left respectively. // The clockwise starting direction is right, and the counterclockwise starting direction is bottom. int direction = clockwise ? 2 : 3; // Start loop filling, each filling is three steps for (int i = 1, max = width * height; i <= max; i++) { // 1. Fill content int y = pointer[0]; int x = pointer[1]; matrix[y][x] = counter; // 2. The counter is incremented by counter += 1; // 3. Move to the next position, because this place is more complicated, open a method to implement direction = move(matrix, width, height, pointer, direction, clockwise); } // After the matrix is filled, you can loop the output in the normal way for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { System.out.printf(format, matrix[y][x]); } System.out.println(); // Output line break after one line is completed} } /** * Move * * @param matrix matrix in the matrix, used to determine whether the next position in the forward direction has been filled with numbers. If so, turn to * @param width Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix, if so, turn to * @param width Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix Matrix The value inside will change after calling this method, unless the method returns 0 * @param direction The current direction of the pointer is moving * @param clockwise Whether it is to turn clockwise * * @return The new direction after moving (may be the same or different from the original direction). If you cannot continue moving, return 0 */ private static int move(int[][] matrix, int width, int height, int[] pointer, int direction, boolean clockwise) { // Try to move to newPointer int[] newPointer = moveDirectly(pointer, direction); // Check whether newPointer is legal. If legal, assign it to pointer and maintain the original direction. The method is completed if (isValid(newPointer, matrix, width, height)) { System.arraycopy(newPointer, 0, pointer, 0, 2); return direction; } // Turn and move again from pointer in a new direction direction = turn(direction, clockwise); newPointer = moveDirectly(pointer, direction); // Check whether newPointer is legal (as before) if (isValid(newPointer, matrix, width, height)) { System.arraycopy(newPointer, 0, pointer, 0, 2); return direction; } // You cannot move forward or turn, so you cannot continue to move. return 0; } // Determine whether the specified position in the matrix can be filled with private static boolean isValid(int[] newPointer, int[][] matrix, int width, int height) { // The position cannot exceed the matrix range if (newPointer[0] >= height || newPointer[0] < 0 || newPointer[1] >= width || newPointer[1] < 0) { return false; } // The content of the position should be empty if (matrix[newPointer[0]][newPointer[1]] != 0) { return false; } return true; } // Steer. According to our definition of direction, clockwise is +1, counterclockwise is -1 private static int turn(int direction, boolean clockwise) { int newDirection = clockwise ? direction + 1 : direction - 1; if (newDirection > 4) { newDirection = 1; } else if (newDirection < 1) { newDirection = 4; } return newDirection; } /** * Move in the specified direction and return to the new position* * @param pointer Current position* @param direction Direction* * @return New position*/ private static int[] moveDirectly(int[] pointer, int direction) { int y = pointer[0]; int x = pointer[1]; switch (direction) { case 1: return new int[]{y - 1, x}; case 2: return new int[]{y, x + 1}; case 3: return new int[]{y + 1, x}; case 4: return new int[]{y, x - 1}; } throw new IllegalArgumentException("Incorrect direction: " + direction); }}Running results:
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.