NOIp2015 提高组 D1T1 神奇的幻方
\(\large\mathcal{Solution}\)
大水题,依据题意模拟即可。
得分:\(100 / 100.\)
\(\large\mathcal{Code}\)
#include <bits/stdc++.h>
#define reg register
using namespace std;
const int N = 40;
int n, lx, ly;
int a[N][N];
int main()
{
scanf("%d", &n);
a[1][n / 2 + 1] = 1; lx = 1; ly = n / 2 + 1;
for (reg int i = 2; i <= n * n; ++ i)
{
if (lx == 1 && ly != n) a[n][ly + 1] = i, lx = n, ly = ly + 1;
else if (lx != 1 && ly == n) a[lx - 1][1] = i, lx = lx - 1, ly = 1;
else if (lx == 1 && ly == n) a[lx + 1][ly] = i, lx = lx + 1;
else
{
if (!a[lx - 1][ly + 1]) a[lx - 1][ly + 1] = i, lx = lx - 1, ly = ly + 1;
else a[lx + 1][ly] = i, lx = lx + 1;
}
}
for (reg int i = 1; i <= n; ++ i, puts(""))
for (reg int j = 1; j <= n; ++ j)
printf("%d ", a[i][j]);
return 0;
}
\(\Huge{To be continued...}\)