现在的位置: 首页 > 综合 > 正文

poj – 1579 – Function Run Fun(记忆化搜索)

2019年08月31日 ⁄ 综合 ⁄ 共 575字 ⁄ 字号 评论关闭

题意:跟着题目中公式走。

题目链接:http://poj.org/problem?id=1579

——>>简单题。

#include <cstdio>
#include <cstring>

const int MAXN = 20 + 5;

int dp[MAXN][MAXN][MAXN];

void Init()
{
    memset(dp, -1, sizeof(dp));
}

int w(int a, int b, int c)
{
    if (a <= 0 || b <= 0 || c <= 0)
    {
        return 1;
    }
    else if (a > 20 || b > 20 || c > 20)
    {
        return w(20, 20, 20);
    }

    int& ans = dp[a][b][c];

    if (ans != -1)
    {
        return ans;
    }

    if (a < b && b < c)
    {
        ans = w(a, b, c - 1) + w(a, b - 1, c - 1) - w(a, b - 1, c);
    }
    else
    {
        ans = w(a - 1, b, c) + w(a - 1, b - 1, c) + w(a - 1, b, c - 1) - w(a - 1, b - 1, c - 1);
    }

    return ans;
}

int main()
{
    int a, b, c;

    Init();
    while (scanf("%d%d%d", &a, &b, &c) == 3)
    {
        if (a == -1 && b == -1 && c == -1)
        {
            break;
        }
        printf("w(%d, %d, %d) = %d\n", a, b, c, w(a, b, c));
    }

    return 0;
}

抱歉!评论已关闭.