AcWing 753. 平方矩阵 I
原题链接
困难
作者:
SUMSHINE
,
2021-02-05 11:47:53
,
所有人可见
,
阅读 333
# include <iostream>
# include <cstring>
using namespace std;
const int N = 110;
int f[N][N];
int main()
{
int n;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
while(cin >> n, n)
{
memset(f, 0, sizeof(f));
int x = 1, y = 1, cnt = 1, a, b, d = 1;
for(int i=1; i<=n*n; ++i)
{
f[x][y] = cnt;
a = x + dx[d], b = y + dy[d];
if(a <= 0 || a > n || b <= 0 || b > n || f[a][b])
{
if(a == b)
cnt++;
d = (d + 1) % 4;
a = x + dx[d], b = y + dy[d];
}
x = a, y = b;
}
for(int i=1; i<=n; ++i)
{
for(int j=1; j<=n; ++j)
cout << f[i][j] << ' ';
cout << endl;
}
cout << endl;
}
return 0;
}