一、问题链接
CSP 201412-2 Z字形扫描
二、参考代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
vector<vector<int>> v(n,vector<int>(n));
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
cin>>v[i][j];
}
}
int i=0,j=0;
int a;
while(1) {
while(1) {
a=v[i][j];
cout<<v[i][j]<<" ";
if(i==n-1 and j==n-1) return 0;
i--;
j++;
if(j==n) {
j--;
i+=2;
break;
}
if(i==-1) {
i++;
break;
}
}
while(1) {
a=v[i][j];
cout<<v[i][j]<<" ";
if(i==n-1 and j==n-1) return 0;
i++;
j--;
if(i==n) {
i--;
j+=2;
break;
}
if(j==-1) {
j++;
break;
}
}
}
return 0;
}