Z字形扫描
from CCF-CSP 2014-12-2
Time limit:2s
Memory limit:256MB
模拟一下扫描标记的移动,注意扫描超过了副对角线过后扫描转移会改变就可以了
ac代码:
#include<bits/stdc++.h>
using namespace std;
int m,n,x[505][505],i,j,k,row,col;
void solve1(){ //向左下
while(col >= 1 && row <= n)
cout<<" "<<x[row][col],++row,--col;
--row,++col;
if(row < n) //为下次右上做准备,如果还未到对角,那么向下扫描,否则向右扫描
++row;
else
++col;
}
void solve2(){ //处理方式同上
while(row >= 1 && col <= n)
cout<<" "<<x[row][col],++col,--row;
--col,++row;
if(col < n)
++col;
else
++row;
}
int main(){
cin>>n;
for(i = 1;i <= n;++i)
for(j = 1;j <= n;++j)
cin>>x[i][j];
cout<<x[1][1],m = n * 2 - 2,row = 1,col = 2; //初始化
for(k = 1;k <= m;++k)
if(k & 1)
solve1();
else
solve2();
return 0;
}