洛谷P1464


title: "记忆化搜索"
author: Sun-Wind
date: February 5,2022

搜索优化后是记忆化搜索,再优化就是dp了

#include <iostream>
#include <utility>
using namespace std;
typedef long long ll;
#define fi(i, a, b) for (int i = a; i <= b; ++i)
#define fr(i, a, b) for (int i = a; i >= b; --i)
#define x first
#define y second
#define sz(x) ((int)(x).size())
#define pb push_back
using pii = pair<int, int>;
//#define DEBUG
ll record[25][25][25];
ll w(ll a, ll b, ll c)
{
    if (a <= 24 && b <= 24 && c <= 24 && a >= 0 && b >= 0 && c >= 0 && record[a][b][c])
        return record[a][b][c];
    if (a <= 0 || b <= 0 || c <= 0)
        return 1;
    if (a > 20 || b > 20 || c > 20)
        return w(20, 20, 20);
    if (a < b && b < c)
        return record[a][b][c] = w(a, b, c - 1) + w(a, b - 1, c - 1) - w(a, b - 1, c);
    return record[a][b][c] = w(a - 1, b, c) + w(a - 1, b - 1, c) + w(a - 1, b, c - 1) - w(a - 1, b - 1, c - 1);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll a, b, c;
    while (cin >> a >> b >> c && !(a == -1 && b == -1 && c == -1))
    {
        cout << "w(" << a << ", " << b << ", " << c << ")"
             << " = " << w(a, b, c) << endl;
    }
#ifdef DEBUG
    //freopen(D:\in.txt,r,stdin);
#endif
    return 0;
}
上一篇:一类骰子游戏中的概率计算


下一篇:foldByKey和combineByKey