// main.cpp
// zigzag
//
// Created by lee on 09/10/2018.
// Copyright © 2018 lee. All rights reserved.
#include <iostream>
#include <vector>
using namespace std;
void printline(vector<vector<int>>& arr, int x1, int y1, int x2, int y2){
vector<int> res;
while (x1 <= x2 && y1 >= y2){
if ((x1+y1) % 2 == 0){
res.push_back(arr[x1++][y1--]);
}
else{
res.push_back(arr[x2--][y2++]);
}
}
for (auto i : res) cout << i <<" ";
}
void zigzag(vector<vector<int>>& arr){
int rows = (int) arr.size()-1;
int cols = (int) arr[0].size()-1;
int A[1][2] = {0,0};
int B[1][2] = {0,0};
while (A[0][0] != rows +1){
printline(arr, A[0][0],A[0][1],B[0][0],B[0][1]);
A[0][0] = A[0][1] == cols? A[0][0] +1: A[0][0];
A[0][1] = A[0][1] == cols? A[0][1] : A[0][1]+1;
B[0][1] = B[0][0] == rows? B[0][1]+1 : B[0][1];
B[0][0] = B[0][0] == rows? B[0][0] : B[0][0]+1;
}
}
int main(int argc, const char * argv[]) {
zigzag(arr);
cout << endl;
return 0;
}
近期评论