zoukankan      html  css  js  c++  java
  • 算法基础系列之三:螺旋形矩阵

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

    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看到一个比较简洁的(方法二),也抄上来。

  • 相关阅读:
    .Net下的MSMQ(微软消息队列)的同步异步调用
    [收藏]JS获取网页中HTML元素的几种方法分析
    在FireFox下设为首页的解决方法
    如何创建和使用Web Service代理类
    [收藏]61条面向对象设计的经验原则
    [总结]DotNet中用到的加密算法总结
    如何把用SQL语句int型整数转换成二进制数
    彻底杜绝PHP的session,cookie,Cannot modify header错误
    MSN总是报80048820的错误,网上搜的一些资料解决不了,我找到了真正解决办法!
    [收藏]MD5加密的javascript实现
  • 原文地址:https://www.cnblogs.com/morvenhuang/p/506057.html
Copyright © 2011-2022 走看看