算法基础系列之三:螺旋形矩阵

如何打印出如下这样的螺旋形的矩阵:

1         2  3

8   9  4

7   6  5

 

方法一:

static void SpiralMatrix(int count)

{

    int[,] iarray = new int[count, count];

    for (int i = 0; i < count; i++)

    {

        for (int j = 0; j < count; j++)

        {

            iarray[i, j] = 0;

        }

    }

 

    iarray[0, 0] = 1;

    int row = 0;

    int col = 0;

    int temprowsub = 0;

    int tempcolsub = 1;

 

    for (int i = 0; i < count * count; i++)

    {

        if (tempcolsub == 1)//right

        {

            if (col + 1 <= count - 1 && iarray[row, col + 1]==0)//right

            {

                iarray[row, col + 1] = iarray[row, col] + 1;

                col++;

            }

            else if (row + 1 <= count - 1 && iarray[row + 1, col] == 0)//down

            {

                iarray[row + 1, col] = iarray[row, col] + 1;

                temprowsub = 1;

                tempcolsub = 0;

                row++;

            }

            else

            {

                break;

            }

        }

        else if (tempcolsub == -1)//left

        {

            if (col - 1 >= 0 && iarray[row, col - 1] == 0)//left

            {

                iarray[row, col - 1] = iarray[row, col] + 1;

                col--;

            }

            else if (row - 1 >= 0 && iarray[row - 1, col] == 0)//up

            {

                iarray[row - 1, col] = iarray[row, col] + 1;

                temprowsub = -1;

                tempcolsub = 0;

                row--;

            }

            else

            {

                break;

            }

        }

        if (temprowsub == -1)//up

        {

            if (row-1 >=0 && iarray[row-1, col] == 0)//up

            {

                iarray[row-1, col] = iarray[row, col] + 1;

                row--;

            }

            else if (col + 1 <= count - 1 && iarray[row, col + 1] == 0)//right

            {

                iarray[row, col + 1] = iarray[row, col] + 1;

                temprowsub = 0;

                tempcolsub = 1;

                col++;

            }

            else

            {

                break;

            }

        }

        if (temprowsub == 1)//down

        {

            if (row + 1 <= count - 1 && iarray[row+1, col] == 0)//down

            {

                iarray[row+1, col] = iarray[row, col] + 1;

                row++;

            }

            else if (col - 1 >= 0 && iarray[row, col - 1] == 0)//left

            {

                iarray[row, col - 1] = iarray[row, col] + 1;

                temprowsub = 0;

                tempcolsub = -1;

                col--;

            }

            else

            {

                break;

            }

        }

    }

 

//TO DO:OUTPUT

……

}

 

方法二:

static void SpiralMatrix(int count)

{

    int round = count - 1;

    int[,] matrix = new int[count, count];

    int num = 1;

 

    for (int r = 0; r < round && num <= count * count; r++)

    {

        for (int tj = r; tj <= round - r; tj++)//top

            matrix[r, tj] = num++;

        for (int ri = r + 1; ri <= round - r; ri++) //right

            matrix[ri, round - r] = num++;

        for (int bj = round - r - 1; bj >= r; bj--)//bottom

            matrix[round - r, bj] = num++;

        for (int li = round - r - 1; li > r; li--)//left

            matrix[li, r] = num++;

    }

//TO DO:OUTPUT

……

}

 

方法一是菜鸟我写的,有点类似爬迷宫,代码有点烦。后来在CSDN看到一个比较简洁的(方法二),也抄上来。

 

 

posted @ 2006-09-16 16:48  后厂村思维导图馆  阅读(1077)  评论(0编辑  收藏  举报