1

编写一个类,给定一个正整数 N,创建并返回一个由整数组成的方阵 NxN,以螺旋形式显示从 1 到 n^2 的数字

n=4

在我的课上我有四个方法,其中三个是方向,而spiral()方法应该把每个数字放在正确的位置

public static int[][] spiral(int n) {
    int[][] res;
    int i,j;
    int num;
    int direction;

    /** variables initializzazion */
    res=new int[n][n];
    i=0;
    j=0;
    res[i][j]=1;
    direction=0;


    for(num=2; num<=n*n; num++) {
        direction = updateDirection(direction, i, j, n);
        if ((direction==1) || (direction==3))
            i=updateRow(i, direction);
        else
            j=updateColoumn(j, direction);
        res[i][j]=num;
    }
    return res;
}

可悲的是,当我尝试运行它时,我得到一个ArrayIndexOutOfBoundException似乎是由res[i][j]=1;.

如何修复它以使数组仍然从 1 开始并上升到 N*N?

编辑:添加updateDirection()方法

为了更好地理解此方法,请查看此图像:

updateDirection 方法

public static int updateDirection(int direction, int i, int j, int n) {

    /** if on the secondary diagonal direction is 1 or 3 */
    if(i+j==n-1)
        direction++;
    /** if on the lower half of the main diagonal, direction is 2 */
    if(i==j && j+j>=n)
        direction++;
    /** if on the row below the higher half of the main diagonal, direction is 0 */
    if(i==j+1 && i+j<n)
        direction++;
    /** in other cases, direction doesn't change */

    return direction%4;
    }

Edit2:这是我的测试方法:

public static void testSpiral(){
    for(int n=0; n<=5; n++)
        System.out.println(Arrays.deepToString(spiral(n)));
}

Edit3 :updateRow()updateColoumn()添加了方法:

public static int updateRow(int i, int direction) {
    int res;
    if(direction==1)
        res=i+1; //moves from top to bottom
    else
        res = i-1; //moves from bottom to top
    return res;
}

public static int updateColoumn(int j, int direction){
    int res;
    if(direction==0)
        res=j+1; //moves from left to right
    else
        res=j-1; //moves from right to left
    return res;
4

3 回答 3

2

testSpiral方法中,您正在启动for循环0,因此数组res是使用 size 创建的0

当您尝试设置时,res[i][j] = 1您正在尝试访问数组中的第一个元素,但没有。

只需for开始i=1

public static void testSpiral(){
    for(int n=1; n<=5; n++)
        System.out.println(Arrays.deepToString(spiral(n)));
}
于 2016-06-20T18:22:12.513 回答
1

你不需要上课。该函数以顺时针方向返回螺旋矩阵。我希望我能帮上忙。

int[][] spiralNumbers(int n) {
    int[][] matrix = new int[n][n];
    for (int step = 0, a = 0, size; step < n/2; step++) {
        size = (n - step * 2 - 1);
        for (int i = 0, chunk, chunkIndex, chunkOffset; i < 4 * size; i++) {
            chunk = i / size;
            chunkIndex = i % size;
            chunkOffset = n - step - 1;
            switch (chunk) {
                case 0:
                    matrix[step][chunkIndex + step] = a+1;
                    break;
                case 1:
                    matrix[chunkIndex + step][chunkOffset] = a+1;
                    break;
                case 2:
                    matrix[chunkOffset][chunkOffset - chunkIndex] = a+1;
                    break;
                case 3:
                    matrix[chunkOffset - chunkIndex][step] = a+1;
                    break;
                default:
                    throw new IndexOutOfBoundsException();
            }
            a++;
        }
        if (n % 2 == 1) {
            matrix[n/2][n/2] = n * n;
        }
    }
    return matrix;
}
于 2018-10-17T20:29:03.367 回答
0

这可能会或可能不会做你想要的。我认为弄清楚它的作用可能会教给你很多东西。

import java.util.Arrays;

public class Spiral {

        static final int TEST_X = 5;
        static final int TEST_Y = 5; 
        static enum Direction {UP, DOWN, LEFT, RIGHT};

        public static void main( String ... args ){
            for( int i = TEST_X - 1; i > 0; i--) { 
                for ( int j = TEST_Y - 1 ; j > 0; j-- ){
                    int arr[][] = spiral(i,j);

                    for( int[] inner : Arrays.asList(arr))
                        System.out.println(Arrays.toString(inner));

                    System.out.println("############\n");
                }
            }
        }

        public static int[][] spiral( int rows, int cols ) { 
            if( cols <= 0 || rows <= 0 ){
                throw new IllegalArgumentException("Spiral requires dimensions greater than 0.");
            }
            int arr[][] = new int[rows][cols];

            int     count             = 1; 
            int     cur_col             = -1; 
            int     cur_row             = 0;
            int     consecutive_turns = 0;
            Direction dir             = Direction.RIGHT;


            while( true ) {

                if( consecutive_turns == 4)
                       return arr;

                switch( dir ){
                case RIGHT : 

                    if( cur_col + 1 == cols || arr[cur_row][cur_col + 1] > 0){ 

                        dir = Direction.DOWN;
                        consecutive_turns++;

                    } else {
                        consecutive_turns = 0;
                        cur_col++;
                        arr[cur_row][cur_col] = count;
                        count++;
                    }
                    break;

                case LEFT :

                    if( cur_col - 1 < 0 || arr[cur_row][cur_col - 1] > 0 ){
                        dir = Direction.UP;
                        consecutive_turns++;
                    } else {
                        consecutive_turns = 0;
                        cur_col--;
                        arr[cur_row][cur_col] = count;
                        count++;
                    }
                    break; 

                case UP : 

                    if( cur_row - 1 < 0 || arr[cur_row - 1][cur_col] > 0 ){
                        dir = Direction.RIGHT;
                        consecutive_turns++;
                    } else {
                        consecutive_turns = 0;
                        cur_row--;
                        arr[cur_row][cur_col] = count;
                        count++;
                    }
                    break; 

                case DOWN : 

                    if( cur_row + 1 == rows || arr[cur_row + 1][cur_col] > 0 ){
                        dir = Direction.LEFT;
                        consecutive_turns++;
                    } else {
                        consecutive_turns = 0;
                        cur_row++;
                        arr[cur_row][cur_col] = count;
                        count++;
                    }
                    break;

                default : 
                    System.err.println("The end has come!");
                    System.exit(1); 

                }
            }
        }
    }
于 2016-06-20T18:21:16.553 回答