Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in ZigZag-order.
给你一个包含 m x n 个元素的矩阵 (m 行, n 列), 求该矩阵的之字型遍历。
Example
Given a matrix:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10, 11, 12]
]
return [1, 2, 5, 9, 6, 3, 4, 7, 10, 11, 8, 12]
void print_matrix(vector<vector<int> >matrix){
int m=matrix.size(),n=matrix[0].size();
int ct=m*n;
int i=0,j=0;
cout<<matrix[0][0]<<endl;
while(ct!=0){
while(i>0&&j+1<m){
cout<<matrix[--i][++j]<<endl;;
}
if(j+1<m){
cout<<matrix[i][++j]<<endl;;
}else{
if(i+1<n){
cout<<matrix[++i][j]<<endl;;
}
}
while(i+1<n&&j>0){
cout<<matrix[++i][--j]<<endl;;
}
if(i+1<n){
cout<<matrix[++i][j]<<endl;;
}else{
if(j+1<m){
cout<<matrix[i][++j]<<endl;;
}
}
}
}