square puzzle solution

前端 未结 5 1003
一生所求
一生所求 2021-01-01 02:51

Question: given an integer number n, print the numbers from 1 up to n2 like this:

n = 4

result is:

01 02 03 04
12 13 14 05
11 16 1         


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-01 03:34

    Though your example is in python and this is in Java, I think you should be able to follow the logic:

    public class SquareTest {
    
    public static void main(String[] args) {
        SquareTest squareTest = new SquareTest(4);
        System.out.println(squareTest);
    }
    
    private int squareSize;
    private int[][] numberSquare;
    private int currentX;
    private int currentY;
    private Direction currentDirection;
    
    private enum Direction {
        LEFT_TO_RIGHT, RIGHT_TO_LEFT, TOP_TO_BOTTOM, BOTTOM_TO_TOP;
    };
    
    public SquareTest(int squareSize) {
        this.squareSize = squareSize;
        numberSquare = new int[squareSize][squareSize];
        currentY = 0;
        currentX = 0;
        currentDirection = Direction.LEFT_TO_RIGHT;
        constructSquare();
    }
    
    private void constructSquare() {
        for (int i = 0; i < squareSize * squareSize; i = i + 1) {
            numberSquare[currentY][currentX] = i + 1;
            if (Direction.LEFT_TO_RIGHT.equals(currentDirection)) {
                travelLeftToRight();
            } else if (Direction.RIGHT_TO_LEFT.equals(currentDirection)) {
                travelRightToLeft();
            } else if (Direction.TOP_TO_BOTTOM.equals(currentDirection)) {
                travelTopToBottom();
            } else {
                travelBottomToTop();
            }
        }
    }
    
    private void travelLeftToRight() {
        if (currentX + 1 == squareSize || numberSquare[currentY][currentX + 1] != 0) {
            currentY = currentY + 1;
            currentDirection = Direction.TOP_TO_BOTTOM;
        } else {
            currentX = currentX + 1;
        }
    }
    
    private void travelRightToLeft() {
        if (currentX - 1 < 0 || numberSquare[currentY][currentX - 1] != 0) {
            currentY = currentY - 1;
            currentDirection = Direction.BOTTOM_TO_TOP;
        } else {
            currentX = currentX - 1;
        }
    }
    
    private void travelTopToBottom() {
        if (currentY + 1 == squareSize || numberSquare[currentY + 1][currentX] != 0) {
            currentX = currentX - 1;
            currentDirection = Direction.RIGHT_TO_LEFT;
        } else {
            currentY = currentY + 1;
        }
    }
    
    private void travelBottomToTop() {
        if (currentY - 1 < 0 || numberSquare[currentY - 1][currentX] != 0) {
            currentX = currentX + 1;
            currentDirection = Direction.LEFT_TO_RIGHT;
        } else {
            currentY = currentY - 1;
        }
    }
    
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < squareSize; i = i + 1) {
            for (int j = 0; j < squareSize; j = j + 1) {
                builder.append(numberSquare[i][j]);
                builder.append(" ");
            }
            builder.append("\n");
        }
    
        return builder.toString();
    }
    }
    

提交回复
热议问题