AcWing 3208. Z字形扫描
原题链接
简单
作者:
牛奶小柒Luke
,
2021-02-16 22:12:52
,
所有人可见
,
阅读 255
#include <iostream>
using namespace std;
const int N = 500;
int n;
int m[N][N];
int row = 0,col = 0,d = 1;
int main(){
cin >> n;
for(int i = 0;i < n;++i){
for(int j = 0;j < n;++j){
cin >> m[i][j];
}
}
int res = 0;
while(res < n * n){
switch(d){
case 1:
cout << m[row][col++] << ' ';
res++;
if(row == 0) d = 2;
else if(row == n - 1) d = 4;
break;
case 2:
cout << m[row++][col--] << ' ';
res++;
if(col == 0 && row != n - 1) d = 3;
else if(row == n - 1) d = 1;
break;
case 3:
cout << m[row++][col] << ' ';
res++;
if(col == 0) d = 4;
else d = 2;
break;
case 4:
cout << m[row--][col++] << ' ';
res++;
if(row == 0 && col != n - 1) d = 1;
else if(col == n - 1) d = 3;
break;
}
}
return 0;
}