题意:
给你一个矩阵,要你进行z扫描。
之后输出。像题目描述一样来输出。
思路:
用四个方向来表示,
- 向斜上走 x − 1 x-1 x−1, y + 1 y+1 y+1
- 向斜下走 x + 1 x+1 x+1, y − 1 y-1 y−1
- 向右走 x + 0 x+0 x+0, y + 1 y+1 y+1
- 向下走 x + 1 x+1 x+1, y + 0 y+0 y+0
往斜上走到头后,会向右走。
往斜下走到头后,会向下走。
AC
# include <iostream>
# include <cstring>
# include <cstdio>
# include <vector>
# define For(i,x,y) for(int i = (x); i <= (y); i ++ )
using namespace std;
int dx[]={-1,0,1};
int dy[]={ 1,1,0};
//xie right down
int a[1000][1000];
int n;
bool in(int tx, int ty){
if(tx>=1&&tx<=n && ty>=1 && ty <=n)return true;
return false;
}
int main(){
// vector<int>v[n](vector<int>,n);
//int a[][]
cin>>n;
for(int i = 1; i <= n ; i++ ){
for(int j = 1; j <= n; j ++ )cin>>a[i][j];
}
int x, y, cnt = 1;
x = y = 1;
while(cnt<=n*n){
int tx, ty;
printf("%d ", a[x][y]);
//a[x][y] = 0;
cnt++;
tx = x+dx[0];
ty = y + dy[0];
if(!in(tx,ty)){
if(dy[0]>0){
tx = x + dx[1];
ty = y + dy[1];
if(!in(tx,ty)){
tx = x + dx[2];
ty = y + dy[2];
}
}// /
else {
tx = x + dx[2];
ty = y + dy[2];
if(!in(tx,ty)){
tx = x + dx[1];
ty = y + dy[1];
}
}// /
// swap(dx[1],dy[1]);
swap(dx[0],dy[0]);
}
x = tx, y = ty;
}
return 0;
}