TLE!

P1464 Function

love20110429 @ 2024-02-02 21:01:25

#include <iostream>
#include <cstdio>
using namespace std;
int w(int a,int b,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()
{
    int a,b,c,tot=0;
    cin>>a>>b>>c;
    while (!(a==-1&&b==-1&&c==-1)){
        if (!tot){
            tot++;
        }else{
            cin>>a>>b>>c;
            if (a==-1&&b==-1&&c==-1){
                return 0;
            }
        }
        printf("w(%d, %d, %d) = %d\n",a,b,c,w(a,b,c));
    }
    return 0;
}

by him0715 @ 2024-02-11 11:45:58

//你没有使用记忆化搜索
//最后的输出有问题
#include <bits/stdc++.h>
using namespace std;
long long s[25][25][25];//记忆化搜索
long long w(long long a,long long b,long long c){
    if (a<=0||b<=0||c<=0)return 1;
    else if(a>20||b>20||c>20)return w(20,20,20);
    else if(s[a][b][c]!=0)return s[a][b][c];
    else if(a<b&&b<c)
        return s[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
    return s[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(){
    long long a,b,c;
    //这里的tot删掉了
    while(true){
        cin>>a>>b>>c;
        if (a==-1&&b==-1&&c==-1)return 0;
        cout<<"w("<<a<<", "<<b<<", "<<c<<") = "<<w(a,b,c)<<endl;//你把这里输出改一下
    }
    return 0;
}

by qusia_MC @ 2024-02-12 19:01:53

没看懂tot啥意思,不过你还是听这位大佬的用记忆化搜索吧


|