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

  • 相关阅读:
    vue input输入框长度限制
    vue中input输入框的模糊查询实现
    腾讯云服务器配置node环境
    axios中的this指向问题
    腾讯云服务器 ubuntu 设置允许root用户登录
    nodejs+express+mongodb写api接口的简单尝试
    通过fromdata实现上传文件
    阿姆斯特朗数
    Mac上webstorm与git仓库建立连接
    iOS学习——属性引用self.xx与_xx的区别
  • 原文地址:https://www.cnblogs.com/morvenhuang/p/506057.html
Copyright © 2011-2022 走看看