AcWing 756. 蛇形矩阵
原题链接
简单
作者:
胜利庄老实人
,
2021-03-09 13:52:14
,
所有人可见
,
阅读 222
#include<iostream>
using namespace std;
int q[100][100];
int main(){
int n,m;
cin >> n >> m;
int dx[] = {0, 1, 0, -1},dy[] = {1, 0, -1, 0};
for(int x = 0, y = 0, d = 0, k = 1; k <= n * m; ++k){
q[x][y] = k;
int a = x + dx[d], b = y + dy[d];
if(a < 0 || a >= n || b < 0 || b >= m || q[a][b]){
d = (d + 1) % 4;
a = x + dx[d], b = y + dy[d];
}
x = a,y = b;
}
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j)
cout << q[i][j] << " ";
cout << endl;
}
return 0 ;
}