zoukankan      html  css  js  c++  java
  • 给定矩阵行数和矩阵列数,顺时针打印矩阵(从0开始)

    //石头 2016 / 3 / 28 21:38 : 32
    #include<iostream>
    #include<vector>
    using namespace std;
    
    class Solution {
    public:
        void printMatrix(vector<vector<int> > &matrix) {
            int count = 0;
            if (matrix.empty())
                return;
            int upEdge = 0;
            int leftEdge = 0;
            int rightEdge = matrix[0].size();
            int downEdge = matrix.size();
            bool end = false;
            char direction = 'r';
            int i = 0;
            int j = 0;
            while (!end)
            {
                switch (direction)
                {
                case 'r':
                    if (j < rightEdge)
                    {
                        while (j < rightEdge)
                        {
                            matrix[i][j] = count++;
                            j++;
                        }
                        upEdge += 1;
                        direction = 'd';
                        i++;
                        j--;
                    }
                    else
                        end = true;
                    break;
                case 'd':
                    if (i < downEdge)
                    {
                        while (i < downEdge)
                        {
                            matrix[i][j] = count++;
                            i++;
                        }
                        direction = 'l';
                        j--;
                        i--;
                        rightEdge -= 1;
                    }
                    else
                        end = true;
                    break;
                case 'l':
                    if (j >= leftEdge)
                    {
                        while (j >= leftEdge)
                        {
                            matrix[i][j] = count++;
                            j--;
                        }
                        direction = 'u';
                        i--;
                        j++;
                        downEdge -= 1;
                    }
                    else
                        end = true;
                    break;
                case 'u':
                    if (i >= upEdge)
                    {
                        while (i >= upEdge)
                        {
                            matrix[i][j] = count++;
                            i--;
                        }
                        leftEdge += 1;
                        direction = 'r';
                        j++;
                        i++;
                    }
                    else
                        end = true;
                    break;
                default:
                    break;
                }
            }
        }
    };
    
    int  main()
    {
        int m = 0;
        int n = 0;
        while (cin >> m >> n)
        {
            if (m != 0 && n != 0)
            {
                vector<vector<int> > maxtrix(m, vector<int>(n, 0));
                class Solution test;
                vector<int> result;
                test.printMatrix(maxtrix);
                for (int i = 0; i < m; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        cout << maxtrix[i][j];
                        if (j != n - 1)
                            cout << " ";
                    }
                    if (i != m - 1)
                        cout << endl;
                }
            }
        }
    }
    手里拿着一把锤子,看什么都像钉子,编程界的锤子应该就是算法了吧!
  • 相关阅读:
    杂记:高僧的炒股境界
    在Windows 7中使用tsmmc远程桌面
    VS2010初体验
    code4fun: one service,one config
    WCF进阶:将编码后的字节流压缩传输
    有高手想换工作的么?
    code4fun:host wcf service just in time
    evey的几张鼠绘
    说说WCF Rest
    外包一类似联众room的项目
  • 原文地址:https://www.cnblogs.com/chess/p/5331169.html
Copyright © 2011-2022 走看看