简单的实现螺旋矩阵
来源:互联网 发布:网络运营推广 编辑:程序博客网 时间:2024/06/03 14:58
16 15 14 13
5 4 3 12
6 1 2 11
7 8 9 10
const std::vector<std::vector<int>>/*&*/ CAlgorithm::screwMatrix(const int& width, const int&height ){int differ = width - height;if (!(differ == 0 || differ == 1) || width + height < 2){std::vector<std::vector<int>> buff;return buff;}std::vector<int> buff1(width,0);/*static*/ std::vector<std::vector<int>> buff0(height,buff1);int num = 1, start = width / 2 - 1, max = width * height;int j, count, x, y;for (int i = 2; i <= width + 1; i += 2){j = 1;count = 1;//第一次取最里面的2X2的矩阵左上角点为初始点,之后每次把初始点斜向上移一格if (width <= 2){if (height == 1){x = -1;y = 0;}else {x = y = 0;}}else{if (2 == i){if (width % 2 == 0){if (height % 2 == 0){x = start;y = x;}else{x = start - 1;y = start;}}else{x = start;y = start +1;}}else{x -= 1;y -= 1;}}while (j < i){switch(count){case 1: ++x;break;case 2: ++y;break;case 3: --x;break;case 4: --y;break;}buff0[x][y] = num++;if (num > max){break;}j++;if (j >= i && count < 4){count++;j = 1;}}}for (int i = 0; i < height; i++)for(int j = 0; j < width; j++){int temp = buff0[i][j];if(temp < 10)std::cout<<" "<<temp<<" ";elsestd::cout<<temp<<" ";if(j == width - 1)std::cout<<std::endl;}return buff0;}
- 简单的实现螺旋矩阵
- 简单的螺旋矩阵的实现
- 螺旋矩阵的实现
- 笔试题里螺旋矩阵的C++简单实现
- 螺旋矩阵的Java实现
- 我的螺旋矩阵实现
- 螺旋矩阵的C实现
- 模拟法简单实现螺旋矩阵
- 蛇形矩阵,回环矩阵,数字螺旋矩阵的PHP实现
- n*n螺旋矩阵的实现
- 001螺旋矩阵的java实现
- 螺旋矩阵的两种JAVA实现
- python实现螺旋矩阵的填充
- 矩阵的螺旋排列 Python实现
- 螺旋矩阵(c实现)
- 螺旋矩阵编程实现
- java 实现螺旋矩阵
- java实现螺旋矩阵
- LeetCode-Subsets
- 安卓开发之常见死机问题--log分析
- 汇编语言
- 【转载】SG函数2(排名不分先后)
- Java Thread学习之七
- 简单的实现螺旋矩阵
- Linux下安装源码包详解(tar.gz)
- win7下创建软链接(mklink命令)
- 自定义 UIPopoverController
- discuz 学习心得
- short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?
- vs2010无可用源
- 开启线程thread
- Pano2VR使用心得