内部螺旋矩阵Java

时间:2016-03-09 19:10:59

标签: java algorithm matrix

给定N * N矩阵;我想以螺旋形式打印它,这也是内心深处。

例如

给出一个矩阵

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

我想打印为7 6 10 11 12 8 4 3 2 1 5 9 13 14 15 16

感谢任何帮助。感谢。

编辑 -

这就是我所做的 -

我发现基于N的起始节点是偶数或奇数。 我已经在每个周期中找到了基于行,列索引的遍历关系 -     左= -1,-3,-5 ......     下= + 1,+ 3,+ 5 ......     对= + 2,+ 4,+ 6 ......     Up = -2,-4,-6 ......

但现在我正在努力将其整合到代码中。

4 个答案:

答案 0 :(得分:0)

您可能希望将矩阵放入二维数组中,如下所示:

int[][] matrix = {{ 1, 2, 3, 4},
                  { 5, 6, 7, 8},
                  { 9,10,11,12},
                  {13,14,15,16}};

然后,使用一些循环来创建螺旋算法。典型的方形螺旋算法从一个点开始,然后沿x方向移动,转动90度并沿x方向移动,然后将x递增2,然后重复直到达到外边缘。

你的外环很可能看起来像这样:

for (int i=1; i<matrix.length; i+=2){
    ...
}

另请注意,程序的移动次数应始终为matrix.length*2-1:因此您的循环也可能如下所示:

for (int i=0; i<matrix.length*2-1; i++){
    ...
}

答案 1 :(得分:0)

最后我找到了解决方案。请告诉我任何优化的解决方案。你去了 -

"TagIDNum11"

}

答案 2 :(得分:0)

一般的想法是将其视为棋盘格,其中所有正方形都涂成白色 首先,将黑板上的方块画在方块中间。 你对左边的广场做同样的事 从那里,你需要知道你是否继续向左画画,或者你是否向下画画。如果当前位置的正方形已经涂成黑色,则继续向左。否则,你会失败 要知道方块是否已经涂成黑色,你需要一个面具。

考虑到这一点,这就是我提出的,只使用一个循环。

我试图对代码进行充分的评论,所以看起来可能太长了......无论如何,我认为它仍然可以缩短。

import java.util.*;

public class SpiralMatrix{

    public static final int UP = 0;
    public static final int LEFT = 1;
    public static final int RIGHT = 2;
    public static final int DOWN = 3;

     public static void main(String []args){        

        // The input matrix
        int[][] matrix= {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};

        // The input matrix  dimension
        int matrixLength = matrix.length;

        //The number of elemets of the input matrix
        int numberOfElements = matrixLength * matrixLength;

        // The output is a single dimensional array containing the elements of the input matrix as a spiral
        int[] spiralMatrix = new int[numberOfElements] ;

        // The matrix mask help to decide which is the next direction or the next element to pick from the input matrix
        // All the values of the mask are initialized to zero. 
        int[][] mask = new int[matrixLength][matrixLength];

                //The first element of the output(the spiral) is always the middle element of the input matrix
    int rowIndex = 0;
    int colIndex = 0;

    if(matrixLength%2 == 0){
        rowIndex = matrix.length/2 - 1;
    } else {
        rowIndex = matrix.length/2;
    }
    colIndex = matrix.length/2;

    // Each time an element from the input matrix is added to the output spiral, the corresponding element in the mask is set to 1
    spiralMatrix[0] = matrix[rowIndex][colIndex];
    mask[rowIndex][colIndex] = 1; 

        // The first direction is always to the left
        int nextDirection = LEFT;

        // This is a counter to loop through all the elements of the input matrix only one time
        int i = 0;

        while(i < numberOfElements - 1){    
            i++;

            // Check which direction to go (left, down, right or up)
            switch(nextDirection){

                case LEFT :
                    // From the input matrix, take the number at the left of the current position(which is the middle of the input matrix) and add it to the spiral
                    colIndex -=1;
                    spiralMatrix[i] = matrix[rowIndex][colIndex];

                    //Update the mask
                    mask[rowIndex][colIndex] = 1;

                    // Decide which direction(or element in the input matrix) to take next.
                    // After moving to the left, you only have two choices : keeping the same direction or moving down
                    // To know which direction to take, check the mask
                    if(mask[rowIndex+1][colIndex] == 1){
                        nextDirection = LEFT;
                    }else{
                        nextDirection = DOWN;
                    }
                    break;

                case DOWN : 
                    rowIndex +=1;
                    spiralMatrix[i] = matrix[rowIndex][colIndex];
                    mask[rowIndex][colIndex] = 1;
                    if(mask[rowIndex][colIndex+1] == 1){
                        nextDirection = DOWN;
                    }else{
                        nextDirection = RIGHT;
                    }
                    break;

                case RIGHT : 
                    colIndex +=1;
                    spiralMatrix[i] = matrix[rowIndex][colIndex];
                    mask[rowIndex][colIndex] = 1;
                    if(mask[rowIndex-1][colIndex] == 1){
                        nextDirection = RIGHT;
                    }else{
                        nextDirection = UP;
                    }
                    break;

                case UP : 
                    rowIndex -=1;
                    spiralMatrix[i] = matrix[rowIndex][colIndex];
                    mask[rowIndex][colIndex] = 1;
                    if(mask[rowIndex][colIndex-1] == 1){
                        nextDirection = UP;
                    }else{
                        nextDirection = LEFT;
                    }
                    break;  
            }
         }

         System.out.println(Arrays.deepToString(matrix));
         System.out.println(Arrays.deepToString(mask));
         System.out.println(Arrays.toString(spiralMatrix));
     }

}

您可以调试它以查看输入矩阵,掩码和输出在每次迭代时如何演变。我只评论了第一个开关盒,但请告诉我是否有必要进一步解释。 希望它有所帮助。

答案 3 :(得分:0)

如果仅查看螺旋的1维运动,您将看到图案。例如。在垂直方向上,您向上移动1,然后向下2,然后向上移动3,然后向下4,依此类推。然后在水平和垂直尺寸之间交替。

这里是一个例子:

int[][] matrix = {{ 1, 2, 3, 4},
        { 5, 6, 7, 8},
        { 9,10,11,12},
        {13,14,15,16}};
int matrixLength = matrix.length * matrix[0].length;
int y = matrix.length/2 - 1;
int x = matrix[0].length/2 - 1;
System.out.println(matrix[y][x]);

boolean isDirectionDown = true;
boolean isDirectionRight = true;
boolean isMoveHorizontal = true;
int xLength = 1;
int yLength = 1;
int stepCount = 0;
outer: while (true) {
    if (isMoveHorizontal) {
        for (int i = 0; i < xLength; i++) {
            if (stepCount == matrixLength-1) {
                break outer;
            }
            x += (isDirectionRight) ? 1 : -1;
            stepCount++;
            System.out.println(matrix[y][x]);
        }
        xLength++;
        isDirectionRight = !isDirectionRight;
    }
    else {
        for (int i = 0; i < yLength; i++) {
            if (stepCount == matrixLength-1) {
                break outer;
            }
            y += (isDirectionDown) ? 1 : -1;
            stepCount++;
            System.out.println(matrix[y][x]);
        }
        yLength++;
        isDirectionDown = !isDirectionDown;
    }
    isMoveHorizontal = !isMoveHorizontal;
}