java 螺旋矩阵不起作用

java spiral matrix not working

这是我编写的代码,用于抛出螺旋矩阵,但它抛出了 矩阵不同

2000021 
1970022 
1860123
1714131224 
1600025

代码是:

public class caracol {
   public static void main(String[] args) {
        int[][] matriz = new int[5][5];
        int n=5;
        int nlimite= n-1;
        int inicio = 0;
        int c=1;
        while(c<=(n*n)) {

            for (int i = inicio+2; i<=nlimite-1; i++) //baja
            {
                matriz[i][nlimite-1] = c++; 
            }

            for (int i = inicio+2; i>=inicio+1; i--) ///izquierda
            {
                matriz[nlimite-1][i] = c++;
            }

            for (int i = nlimite-1; i>=inicio+1; i--) //sube
            {
                matriz[i][inicio+1] = c++;
            }


            for (int i = inicio+1; i>=nlimite-1; i++) //derecha
            {
                matriz[inicio+1][i] = c++;
            }

            for (int i = inicio+1; i<=nlimite; i++) //baja
            {
                matriz[i][nlimite] = c++;
            }

            for (int i = nlimite-1; i>=inicio; i--) ///izquierda
            {
                matriz[nlimite-1][i] = c++;
            }

            for (int i = nlimite; i>=inicio; i--) //sube
            {
                matriz[i][inicio] = c++;
            }

            for (int i = inicio; i>=nlimite; i++) //derecha
            {
                matriz[inicio][i] = c++;
            }  

            for (int i = inicio; i<=nlimite; i++) //baja
            {
                matriz[i][nlimite] = c++;
            }
            nlimite=nlimite-1;
            inicio = inicio+1;
        }
        for(int x=0;x<n;x++) { /*Mostrar la matriz en pantalla*/
            System.out.println();
            for(int y=0;y<n;y++) {
                System.out.print(matriz[x][y]);
            }
        }
    }
}

生成的矩阵与照片的矩阵不同。 您必须从矩阵 [2] [2] 开始,但它会在矩阵 [0] [0] 中开始,因为您更改了行和列的索引的位置。

尝试这样的事情:

public class Matrix
{
    // holds the matrix
    private int[][] matrix = new int[ 5 ][ 5 ];
    // fills up the matrix with zeroes   
    public void init()
    {
        for ( int i = 0; i < 5; ++i )
            Arrays.fill( matrix[i], 0 );
    }
    // prints the matrix out
    public void print()
    {
        for ( int i = 0; i < 5; ++i )
        {
            for ( int j = 0; j < 5; ++j )
                System.out.printf( "%3d", matrix[i][j] );
            System.out.println();
        }
    }
    // prints text out according to the matrix
    public void print(String text)
    {
        for ( int i = 0; i < 5; ++i )
        {
            for ( int j = 0; j < 5; ++j )
                System.out.print( text.charAt(matrix[i][j]) );
            System.out.println();
        }
    }
    // fills the matrix with the spiral
    public void generate()
    {
        // variables: co-ordinates, value, direction
        int x = 2, y = 2, val = 0, dir = 0;
        // in a 5×5 matrix we should put in 25 values...
        while ( val < 25 )
        {
            // put this value
            matrix[y][x] = ++val;
            // for debugging
            System.out.printf( "val=%d, x=%d, y=%d, dir=%d%n", val, x, y, dir );
            // calculate next value's position, and check if we must turn
            int turn = -1;
            switch ( dir )
            {
                case 0: // down, checking left
                    ++y;
                    if ( 25 != val ) // the last value would cause error
                        turn = matrix[y][x - 1];
                    break;
                case 1: // left, checking up
                    --x;
                    turn = matrix[y - 1][x];
                    break;
                case 2: // up, checking right
                    --y;
                    turn = matrix[y][x + 1];
                    break;
                case 3: // right, checking down
                    ++x;
                    turn = matrix[y + 1][x];
            }
            // next direction
            if ( 0 == turn )
                dir = ( dir + 1 ) & 3;
        }
    }
    // for testing
    public static void main( String[] args )
    {
        final Matrix m = new Matrix();
        m.init();
        m.generate();
        m.print();
        m.print("HelloWorldTodayIsAGoodDay");
    }
}

将输出:

val=1, x=2, y=2, dir=0
val=2, x=2, y=3, dir=1
val=3, x=1, y=3, dir=2
val=4, x=1, y=2, dir=2
val=5, x=1, y=1, dir=3
val=6, x=2, y=1, dir=3
val=7, x=3, y=1, dir=0
val=8, x=3, y=2, dir=0
val=9, x=3, y=3, dir=0
val=10, x=3, y=4, dir=1
val=11, x=2, y=4, dir=1
val=12, x=1, y=4, dir=1
val=13, x=0, y=4, dir=2
val=14, x=0, y=3, dir=2
val=15, x=0, y=2, dir=2
val=16, x=0, y=1, dir=2
val=17, x=0, y=0, dir=3
val=18, x=1, y=0, dir=3
val=19, x=2, y=0, dir=3
val=20, x=3, y=0, dir=3
val=21, x=4, y=0, dir=0
val=22, x=4, y=1, dir=0
val=23, x=4, y=2, dir=0
val=24, x=4, y=3, dir=0
val=25, x=4, y=4, dir=0
 17 18 19 20 21
 16  5  6  7 22
 15  4  1  8 23
 14  3  2  9 24
 13 12 11 10 25
sAGoo
IoWod
ylHrD
alela
doTdy

编辑:根据 Me Myself 请求添加了文本