zoukankan      html  css  js  c++  java
  • 螺旋矩阵算法

    最近看到博客里有人介绍螺旋矩阵,忍不住啊~

    以下面的4*4螺旋矩阵为例

    1  2  3    4

    12  13   14  5

    11  16   15  6

    10  9   8   7

    我们可以理解为一个二维数组,从四个方向(从左至右,从上至下,从右至左,从下至上)依次递增赋值(超出数组边界或遇到已赋值的数组元素则改变赋值方向,直至完成全部数组元素的赋值)。

    初始时,赋值方向为从左至右:

        /*
         * 0 turn right
         * 1 turn down
         * 2 turn left
         * 3 turn up
         */
        int dir = 0;

    改变赋值方向:

    dir = (dir + 1)%4;

    全部代码如下:

    ///////////////////////////////////////////////////////////
    // Copyright (c) 2013, ShangHai xxxx Inc.
    //
    // FileName: spiralmatrix-2.cpp
    //
    // Description:
    //
    // Created: 2013年10月13日 星期日 19时42分47秒
    // Revision: Revision: 1.0
    // Compiler: g++
    //
    ///////////////////////////////////////////////////////////
    #include <stdio.h>
    #include <string.h>
    
    const int MAX_SIZE = 10;
    
    int main()
    {
        int array[MAX_SIZE][MAX_SIZE];
        memset(array, 0, sizeof(array));
    
        int x = 0;
        int y = 0;
        int totalStep = MAX_SIZE*MAX_SIZE;
    
        /*
         * 0 turn right
         * 1 turn down
         * 2 turn left
         * 3 turn up
         */
        int dir = 0;
    
        int count = 1;
        while(1)
        {
            array[x][y] = count;
    
            if(0 == dir)
            {
                if( ( (y+1) == MAX_SIZE) || (array[x][y+1] != 0) )
                {
                    dir = (dir+1)%4;
                    x = x + 1;
                }
                else
                {
                    y = y + 1;
                }
    
            }
            else if(1 == dir)
            {
                if( ( (x+1) == MAX_SIZE) || (array[x+1][y] != 0) )
                {
                    dir = (dir + 1)%4;
                    y = y -1;
                }
                else
                {
                    x = x + 1;
                }
            }
            else if(2 == dir)
            {
                if( ( (y-1) < 0) || (array[x][y-1] != 0) )
                {
                    dir = (dir + 1)%4;
                    x = x - 1;
                }
                else
                {
                    y = y - 1;
                }
            }
            else if(3 == dir)
            {
                if( ( (x-1) < 0) || (array[x-1][y] != 0) )
                {
                    dir = (dir + 1)%4;
                    y = y + 1;
                }
                else
                {
                    x = x - 1;
                }
            }
    
            //break or add moveStep
            if(count == totalStep)
                break;
            else
                count++;
        }
    
        //print
        for(int i = 0; i < MAX_SIZE; i++)
        {
            for(int j = 0; j < MAX_SIZE; j++)
                printf("%3d", array[i][j]);
            printf("
    ");
        }
    
        return 0;
    }

    结果如下:

    ./spiralmatrix-2 
      1  2  3  4  5  6  7  8  9 10
     36 37 38 39 40 41 42 43 44 11
     35 64 65 66 67 68 69 70 45 12
     34 63 84 85 86 87 88 71 46 13
     33 62 83 96 97 98 89 72 47 14
     32 61 82 95100 99 90 73 48 15
     31 60 81 94 93 92 91 74 49 16
     30 59 80 79 78 77 76 75 50 17
     29 58 57 56 55 54 53 52 51 18
     28 27 26 25 24 23 22 21 20 19
  • 相关阅读:
    软件工程课堂二
    软件工程第二周总结
    软件工程第一周开课博客
    软件工程课堂一
    开学第一次考试感想
    以Function构造函数方式声明函数
    document.scrollingElement
    标识符
    变量声明语句的提升
    用that代替this
  • 原文地址:https://www.cnblogs.com/yangtze736-2013-3-6/p/3367197.html
Copyright © 2011-2022 走看看