从种子点开始递归地打印矩阵的每个元素

时间:2015-08-13 13:56:36

标签: java recursion matrix

给定起始点和任何维度的矩阵,我想打印矩阵的每个元素。它不必按任何特定顺序排列,但我应该能够触及矩阵中的每个元素。

我该怎么做?这是我尝试过的,并获得输出。是否有更优雅的方式递归执行此操作?

public class RecurseFromSeed
{
    static int rows = 4;
    static int cols = 5;

    static int seedx = 2;
    static int seedy = 2;

    public static void main(String[] args)
    {
        int a[][] = {
                {1,2,3,4,5},
                {6,7,8,9,10},
                {11,12,13,14,15},
                {16,17,18,19,20}
        };

        fun(a, seedx, seedy, 1);
    }

    static void fun(int[][] a,int x, int y, int count)
    {
        /* printing the upper left portion */
        if(count==1)
        {
            if(y < 0)
                return;
            if(x < 0)
                return;

            System.out.print(a[x][y] + " ");
            fun(a, x, y-1, count);
            fun(a, x-1, y, count);
            if(x==seedx && y==seedy)
            {
                System.out.println("reached (seedx,seedy) at count=="+count);
                count++;
            }
        }

        /* printing the bottom left portion */
        if(count == 2)
        {
            if(y < 0)
                return;
            if(x == rows)
                return;

            System.out.print(a[x][y] + " ");
            fun(a, x, y-1, count);
            fun(a, x+1, y, count);
            if(x==seedx && y==seedy)
            {
                System.out.println("reached (seedx,seedy) at count=="+count);
                count++;
            }
        }

        /* printing the upper right portion */
        if(count == 3)
        {
            if(y == cols)
                return;
            if(x < 0)
                return;

            System.out.print(a[x][y] + " ");
            fun(a, x, y+1, count);
            fun(a, x-1, y, count);
            if(x==seedx && y==seedy)
            {
                System.out.println("reached (seedx,seedy) at count=="+count);
                count++;
            }
        }

        /* printing the bottom right portion */
        if(count == 4)
        {
            if(y == cols)
                return;
            if(x == rows)
                return;

            System.out.print(a[x][y] + " ");
            fun(a, x, y+1, count);
            fun(a, x+1, y, count);
            if(x==seedx && y==seedy)
            {
                System.out.println("reached (seedx,seedy) at count=="+count);
                count++;
            }
        }
    }
}

编辑:见下面的输出

13 12 11 6 1 7 6 1 2 1 8 7 6 1 2 1 3 2 1 reached (seedx,seedy) at count==1
13 12 11 16 17 16 18 17 16 reached (seedx,seedy) at count==2
13 14 15 10 5 9 10 5 4 5 8 9 10 5 4 5 3 4 5 reached (seedx,seedy) at count==3
13 14 15 20 19 20 18 19 20 reached (seedx,seedy) at count==4

1 个答案:

答案 0 :(得分:0)

轻松满足以下要求:

  • 打印起点
  • 以您想要的任何方式打印整个矩阵,但是当您到达起点时,请跳过它。

例如:

System.out.print( a[seedx][seedy] + " " );
for ( i=0; i<rows; i++ )
    for ( j=0; j<cols; j++ )
        if ( i!=seedx || j!=seedy )
            System.out.print( a[i][j] + " " );

递归:

static void fun(int[][] a,int x, int y, int count) {
    if ( count == 1 ) {
        System.out.print( a[seedx][seedy] + " " );
        fun( a, 0, 0, 0 );
    } else {
        if ( x!=seedx || y!=seedy )
            System.out.print( a[seedx][seedy] + " " );
        y++;
        if ( y == cols ) {
            y = 0;
            x++;
        }
        if ( x < rows )
            fun( a, x, y, count );
    }
}