#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
/*
问题:
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
For example,
Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].
分析:
spiral order:螺旋顺序。题目意思就是将矩阵中的元素按照螺旋顺序输出。
举例:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
螺旋遍历的起始结点初始为元素1对应坐标(0,0),当外层遍历完了之后,下一次遍历的点为13对应坐标为(1,1)
每当最外层的遍历结束后,下一次遍历
设当前层遍历起点为(i,i),则当前层遍历结束点为(i+1 , i),下一层遍历起点为(i+1 , i+1)
1】如果发现当前层的下一层遍历结束点坐标不存在,说明只有一行元素,遍历完这一行就结束;
2】如果发现当前层的下一层遍历结束点坐标存在,遍历完这一层后继续遍历
难点在于走到边界的时候如何继续走:
每一次遍历时的矩阵行长度为len ,下一次长度为len-2(扣除左右两侧),
1】横坐标达到右边界,需要向下走,接下来纵坐标不变,横坐标累加
2】纵坐标达到下边界,需要向左走,接下来横坐标不变,纵坐标累减
3】横坐标达到左边界,需要向上走,接下来纵坐标不变,横坐标累减
4】纵坐标达到上边界,需要向右走,接下来横坐标不变,纵坐标累加
一个外圈遍历结束后,需要使得横坐标左边界+1,右边界减1;纵坐标上边界+1,下边界减1
关键更新边界放在哪里?或者设置一个访问数组,如果碰到元素已经访问过,说明外层遍历过,则更新边界.
另一个办法设定当前要遍历的终点坐标,一旦遍历到终点坐标,就更新边界,但是这样每次都需要判断
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
输入:
5(行数) 5(列数)
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
3 3
1 2 3
8 9 4
7 6 5
2 3
1 2 3
6 5 2
2 1
1
2
1 2
1 2
1 2
2 3
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6
1 2
1 2
2 3
关键:
1 向右遍历结束后,起始行号累加;
向下遍历结束后,结束列号累减;
向左遍历之前,需要判断此时是否还是原来的那一行,为了避免重复,需要起始行号 <= 结束行号时,
才做处理;遍历完成,结束行号累减
while( xMin <= xMax && yMin <= yMax )
{
//向右
for(y = yMin ; y <= yMax ; y++)
{
result.push_back(matrix.at(xMin).at(y));
}
xMin++;//这一行遍历结束,因此新的行需要xMin++
//更新yMax为yMax-1;,当前向右遍历的行遍历完成后,当前层后续向下,向左,向上不会再用到yMax,则此时更新,
//确保下一层遍历时,发生yMax已经更新
//说明走到右边界,接下来向下
for(x = xMin ; x <= xMax ; x++)
{
result.push_back(matrix.at(x).at(yMax));
}
yMax--;//列号减1
//说明走到最下方,接下来向左,如果只有一行元素,那么会造成又返回到原来遍历过的元素的情况,因此需要判断当前行号是否正确
if(xMin <= xMax)
{
for( y = yMax ; y >= yMin ; y--)
{
result.push_back(matrix.at(xMax).at(y));
}
}
*/
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> result;
if(matrix.empty())
{
return result;
}
int xMax = matrix.size() - 1;//这里的xMax表示高度
//设定起始结点和终点,左边界,右边界,上边界,下边界
int xMin = 0;
int yMax = matrix.at(0).size() - 1;//这里的yMax表示宽度
int yMin = 0;
int x = 0;
int y = 0;
while( xMin <= xMax && yMin <= yMax )
{
//向右
for(y = yMin ; y <= yMax ; y++)
{
result.push_back(matrix.at(xMin).at(y));
}
xMin++;//这一行遍历结束,因此新的行需要xMin++
//更新yMax为yMax-1;,当前向右遍历的行遍历完成后,当前层后续向下,向左,向上不会再用到yMax,则此时更新,
//确保下一层遍历时,发生yMax已经更新
//说明走到右边界,接下来向下
for(x = xMin ; x <= xMax ; x++)
{
result.push_back(matrix.at(x).at(yMax));
}
yMax--;//列号减1
//说明走到最下方,接下来向左,如果只有一行元素,那么会造成又返回到原来遍历过的元素的情况,因此需要判断当前行号是否正确
if(xMin <= xMax)
{
for( y = yMax ; y >= yMin ; y--)
{
result.push_back(matrix.at(xMax).at(y));
}
}
xMax--;//最下面一行已经遍历过,行号减1
//判断是否只有一列
if(yMin <= yMax)
{
for(x = xMax ; x >= xMin ; x--)
{
result.push_back(matrix.at(x).at(yMin));
}
}
yMin++;
}
return result;
}
};
void print(vector<int>& result)
{
if(result.empty())
{
cout << "no result" << endl;
return;
}
int size = result.size();
for(int i = 0 ; i < size ; i++)
{
cout << result.at(i) << " " ;
}
cout << endl;
}
void process()
{
int row;
int col;
int value;
vector< vector<int> > matrix;
Solution solution;
vector<int> result;
while(cin >> row >> col)
{
matrix.clear();
for(int i = 0 ; i < row ; i++)
{
vector<int> nums;
for(int j = 0 ; j < col ; j++)
{
cin >> value;
nums.push_back(value);
}
matrix.push_back(nums);
}
result = solution.spiralOrder(matrix);
print(result);
}
}
int main(int argc , char* argv[])
{
process();
getchar();
return 0;
}