美文网首页
蛇形矩阵

蛇形矩阵

作者: 小白小白啦 | 来源:发表于2018-10-07 15:27 被阅读9次

    java实现“之“字型矩阵

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

    思路: 分为左上角、右下角、中间三部分,其中左上角和右下角和为N*N + 1,中间一部分为最长对角线,为(N*N-N)/2 = 6、6+1、 6+2、6+3、6+4。然后只需要把左上角填充完毕就行,一共有四个方向,下、右上、右、左下。OK思路有了接下来是编码

    public class SnakeMatrix {
    
        /**
         * 蛇形矩阵
         * n * n
         * 1  3  4   10
         * 2  5  9   11
         * 6  8  12  15
         * 7  13 14  16
         */
        public static void main(String[] args) {
            int N = 5;
            int[][] matrix= new int[N][N];
            int value = 1;
            if(N % 2 == 0){
                int temp = (N*N - N) / 2;
                //N是偶数, 对角线赋值
               for(int i=N-1; i>=0; i--)
                   matrix[i][N-1-i] = ++temp;
            }else{
                //N是奇数,对角线赋值
                int temp = (N*N - N) / 2;
                for(int i=0; i<N; i++)
                    matrix[i][N-1-i] = ++temp;
            }
    
            final int DOWN = 0;
            final int RIGHT = 1;
            final int UPRIGHT = 2;
            final int LEFTDOWN = 3;
            int dir  = DOWN;
            matrix[0][0] = 1;
            int row = 0;
            int col = 0;
    
            while(value < (N*N - N) / 2){
                switch (dir){
                    case DOWN:
                        row++;
                        if(row <= N-2){
                            value++;
                            matrix[row][col] = value;
                            dir = UPRIGHT;
                            break;
                        }
                    case RIGHT:
                        col++;
                        if(col <= N-2){
                            value++;
                            matrix[row][col] = value;
                            dir = LEFTDOWN;
                            break;
                        }
                    case UPRIGHT:
                        row--;
                        col++;
                        if(row >=0 ){
                            value++;
                            matrix[row][col] = value;
                            dir = UPRIGHT;
                            break;
                        }else{
                            row++;
                            col--;
                            dir = RIGHT;
                            break;
                        }
                    case LEFTDOWN:
                        row++;
                        col--;
                        if(col >= 0){
                            value++;
                            matrix[row][col] = value;
                            dir = LEFTDOWN;
                            break;
                        }else{
                            col++;
                            row--;
                            dir = DOWN;
                            break;
                        }
                }
            }
    
            //根据对称性赋值
            for(int i = 0; i < N; i++)
                for(int j = 0; j < N; j++)
                    if(matrix[i][j] == 0)
                        matrix[i][j] = N*N+1 - matrix[N-1-i][N-1-j];
    
            for(int i = 0; i < N; i++){
                for(int j = 0; j < N; j++){
                    System.out.print(matrix[i][j] + "  ");
                }
                System.out.println();
            }
    
        }
    
    }
    

    java实现螺旋形蛇形矩阵

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

    思路: 和上面一样,方式为右、下、左、上

    public class SnakeMatrix {
    
        /**
         * 蛇形矩阵
         * n * n
         * 1  2  3  4
         * 12 13 14 5
         * 11 16 15 6
         * 10 9  8  7
         */
        public static void main(String[] args) {
            int N = 4;
            int[][] matrix = new int[N][N];
            int value = 1;
            final int RIGHT = 0;
            final int DOWN = 1;
            final int LEFT = 2;
            final int UP = 3;
            int dir = RIGHT;
            int row = 0;
            int col = 0;
            matrix[row][col] = value;
            while (value < N*N){
                switch (dir){
                    case RIGHT:
                        col++;
                        if(col <= N-1 && matrix[row][col] == 0){
                            value++;
                            matrix[row][col] = value;
                            dir = RIGHT;
                            break;
                        }else{
                            col--;
                            dir = DOWN;
                            break;
                        }
                    case DOWN:
                        row++;
                        if(row <= N-1 && matrix[row][col] == 0){
                            value++;
                            matrix[row][col] = value;
                            dir = DOWN;
                            break;
                        }else{
                            row--;
                            dir = LEFT;
                            break;
                        }
                    case LEFT:
                        col--;
                        if(col>=0 && matrix[row][col] == 0){
                            value++;
                            matrix[row][col] = value;
                            dir = LEFT;
                            break;
                        }else{
                            col++;
                            dir = UP;
                            break;
                        }
                    case UP:
                        row--;
                        if(row >= 0 && matrix[row][col] == 0){
                            value++;
                            matrix[row][col] = value;
                            dir = UP;
                            break;
                        }else{
                            row++;
                            dir = RIGHT;
                            break;
                        }
                }
            }
            for(int i = 0; i < N; i++){
                for(int j = 0; j < N; j++){
                    System.out.print(matrix[i][j] + "  ");
                }
                System.out.println();
            }
        }
    
    }
    
    

    参考:蛇形矩阵的java实现

    相关文章

      网友评论

          本文标题:蛇形矩阵

          本文链接:https://www.haomeiwen.com/subject/kislaftx.html