201412-2Z字形扫描

/*100分,注意不要滥用++,--,尤其是前置++,--
很容易混乱,最好一个语句中用一次*/
#include<iostream>
using namespace std;
int main(){
int n,i,j;
cin>>n;
int a[n][n];
for(i=0;i<n;i++){
for(j=0;j<n;j++)
cin>>a[i][j];
}
int r = 1,d = 0,xx = 0,xs =0;
cout<<a[0][0]<<" ";
int count = 1;
i = 0;j = 0;
while(count<n*n){
if(r&&j+1<n){
cout<<a[i][j+1]<<" ";
j++;
count++;
if(i<n-1) xx = 1;
else xs = 1;
r = 0;
}

if(xx){
while(i+1<n&&j-1>=0){
cout<<a[i+1][j-1]<<" ";
i++;
j--;
count++;
}
if(i<n-1) {
d = 1;
j = 0;
}
else {
r = 1;
i = n-1;

}
xx = 0;
}
if(d&&i+1<n){
cout<<a[i+1][j]<<" ";
i++;
count++;
if(i-1>=0&&j+1<n) xs = 1;
else xx = 1;
d = 0;
}
if(xs){
while(i-1>=0&&j+1<n){
cout<<a[i-1][j+1]<<" ";
i--;
j++;
count++;
}

if(j<n-1) {
r = 1;
i = 0;
}
else if(j==n-1){
d = 1;
j = n-1;
}
xs = 0;
}
}

}

posted on 2017-03-17 19:59  whitehouse  阅读(83)  评论(0编辑  收藏  举报

导航