0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 20:36:28 +00:00
OI-codes/Luogu/P5731/P5731.cpp

64 lines
1.7 KiB
C++
Raw Normal View History

// R36471603
2021-11-19 09:01:13 +00:00
#include <bits/stdc++.h>
using namespace std;
int n, k, a[15][15], ax, ay;
int main() {
k = ax = 1;
scanf("%d", &n);
2021-11-19 09:01:13 +00:00
/**
* ax, ay+1 ->
* ax, ay-1 ->
* ax+1, ay ->
* ax-1, ay ->
2022-05-20 01:23:27 +00:00
*
* 0 --- 1 ---------- ay ----------> n
* | +------+------+------+------+------+
* 1 | | | ax-1 | | |
* | | | | ay | | |
* | +------+------+------+------+------+
* a | | ax | ax | ax | |
* x | | ay-1 | ay | ay+1 | |
* | +------+------+------+------+------+
* | | | | ax+1 | | |
* | | | | ay | | |
* | +------+------+------+------+------+
* | | | | | | |
* | | | | | | |
* | +------+------+------+------+------+
* | | | | | | |
* n | | | | | | n
* +------+------+------+------+------+
* n
*/
2021-11-19 09:01:13 +00:00
while (k <= n * n) {
while (ay < n && !a[ax][ay + 1]) { // 右侧 不过边界 && 未填数
a[ax][++ay] = k++;
}
2021-11-19 09:01:13 +00:00
while (ax < n && !a[ax + 1][ay]) { // 下方 不过边界 && 未填数
a[++ax][ay] = k++;
}
2021-11-19 09:01:13 +00:00
while (ay > 1 && !a[ax][ay - 1]) { // 左侧 不过边界 && 未填数
a[ax][--ay] = k++;
}
2021-11-19 09:01:13 +00:00
while (ax > 1 && !a[ax - 1][ay]) { // 上方 不过边界 && 未填数
a[--ax][ay] = k++;
}
}
2021-11-19 09:01:13 +00:00
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
printf("%3d", a[i][j]);
}
printf("\n");
}
return 0;
}