1. 程式人生 > 實用技巧 >HDU 多校第四場 Last Problem

HDU 多校第四場 Last Problem

Last Problem

解題思路:

先固定一箇中心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\n", x, y, val);
    return;
}
int main()
{
    int n;
    while (cin >> n)
    {
        memset(mp, 0, sizeof(mp));
        dfs(1000, 1000, n);
    }
    return 0;
}