Z字形扫描
#include<cstdio> #include<iostream> #include<algorithm> #include<vector> #include<string> #include<queue> #include<map> #include<stack> using namespace std; int a[505][505]; int main() { int n; cin>>n; for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin>>a[i][j]; int i=0,j=0,flag1=0,flag2=0; cout<<a[i][j]<<" "; //坑点:i,j 对应的行列搞乱了 i对应行 ,j对应列 (少用x,y) while(1) { if(i==n-1 && j==n-1) { break; } //cout<<x<<" "<<y<<endl; if(flag1 == 0) //考虑右移 { if(j>=n-1) //不能右移,只能下移 { i++; flag1 = 1; cout<<a[i][j]<<" "; } else //右移 { j++; flag1 = 1; cout<<a[i][j]<<" "; } } else if(flag1 == 1) //考虑下移 { if(i>=n-1) { j++; flag1 = 0; cout<<a[i][j]<<" "; } else { i++; flag1 = 0; cout<<a[i][j]<<" "; } } if(flag2 == 0) //左下 { while(1) { if(i==n-1) break; if(j==0) break; i++; j--; cout<<a[i][j]<<" "; } flag2 = 1; } else if(flag2 == 1) //右上 { while(1) { if(i==0) break; if(j==n-1) break; i--; j++; cout<<a[i][j]<<" "; } flag2 = 0; } } return 0; }