看你们都记忆化搜索,萌新(我)不懂,tle零分求助

P1464 Function

xawmh2023 @ 2023-12-16 21:17:27

#include <iostream>
using namespace std;
int w(long long int a,long long int b,long long int 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  w(a,b,c-1)+w(a,b-1,c-1-w(a,b-1,c));
    return 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()
{
    long long int a,b,c;
    while(true)
    {
        cin >> a >> b >> c;
        if(a == -1 && b == -1 && c == -1)
            break;
        cout << "w(" << a << ", " << b << ", " << c << ") = ";
        cout << w(a, b, c) << endl;
     }
    return 0;
}

样例已过,求讲解!!!看到了记得@我哦!谢谢


by No_Rest @ 2023-12-16 21:23:00

@xawmh2023 记忆化搜索就是每次计算后用一个数组来记录答案,这样下一次计算时就直接调用数组内存好的答案就行了,避免了不必要的重复计算。


by xawmh2023 @ 2023-12-17 09:35:47

谢谢


|