解题思路:
先固定一个中心n,上面为n-1,左边为n-2,右边为n-3,下面为n-4,
可以构造出一个类似于
的三角形
从底下递归搜回来即可
#include <bits/stdc++.h>
using namespace std;
/* freopen("k.in", "r", stdin);
freopen("k.out", "w", stdout); */
// clock_t c1 = clock();
// std::cerr << "Time:" << clock() - c1 <<"ms" << std::endl;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define de(a) cout << #a << " = " << a << endl
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define ls ((x) << 1)
#define rs ((x) << 1 + 1)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
typedef vector<int, int> VII;
#define inf 0x3f3f3f3f
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll MAXN = 4e5 + 7;
const ll MAXM = 5e5 + 7;
const ll MOD = 1e9 + 7;
const double eps = 1e-6;
const double pi = acos(-1.0);
int mp[2005][2005];
void dfs(int x, int y, int val)
{
if (val <= 0)
return;
if (mp[x][y - 1] != val - 1)
dfs(x, y - 1, val - 1);
if (mp[x - 1][y] != val - 2)
dfs(x - 1, y, val - 2);
if (mp[x + 1][y] != val - 3)
dfs(x + 1, y, val - 3);
if (mp[x][y + 1] != val - 4)
dfs(x, y + 1, val - 4);
mp[x][y] = val;
printf("%d %d %d
", x, y, val);
return;
}
int main()
{
int n;
while (cin >> n)
{
memset(mp, 0, sizeof(mp));
dfs(1000, 1000, n);
}
return 0;
}