全部WA

P1464 Function

baigu @ 2022-02-19 20:12:58

#include<bits/stdc++.h>
using namespace std;
long long aa[30][30][30];
long long n,m,k;
long long w(long long a,long long b,long long c)
{
    if(a<=0||b<=0||c<=0) return 1;
    if(aa[a][b][c]!=0) return aa[a][b][c];
    if(a>20||b>20||c>20) aa[a][b][c]=w(20,20,20);
    if(a<b&&b<c)
    {
        if(!aa[a][b][c-1])aa[a][b][c-1]=w(a,b,c-1);
        if(!aa[a][b-1][c-1])aa[a][b-1][c-1]=w(a,b-1,c-1);
        if(!aa[a][b-1][c])aa[a][b-1][c]=w(a,b-1,c);
        aa[a][b][c]=aa[a][b][c-1]+aa[a][b-1][c-1]-aa[a][b-1][c];
    }
    else
    {
        if(!aa[a-1][b][c])aa[a-1][b][c]=w(a-1,b,c);
        if(!aa[a-1][b-1][c])aa[a-1][b-1][c]=w(a-1,b-1,c);
        if(!aa[a-1][b][c-1])aa[a-1][b][c-1]=w(a-1,b,c-1);
        if(!aa[a-1][b-1][c-1])aa[a-1][b-1][c-1]=w(a-1,b-1,c-1);
        aa[a][b][c]=aa[a-1][b][c]+aa[a-1][b][c-1]+aa[a-1][b-1][c]-aa[a-1][b-1][c-1];
    }
    return aa[a][b][c];
}
int main() 
{
    while(scanf("%lld%lld%lld",&n,&m,&k))
    {
        memset(aa,0,sizeof(aa));
        if(n==-1&&m==-1&&k==-1)return 0;
        printf("w(%lld, %lld, %lld) = ",n,m,k);
        if(n>20)n=20;
        if(m>20)m=20;
        if(k>20)k=20;
        printf("%lld\n",w(n,m,k));
    }
    return 0;
} 

拿错了呀?????

记忆化搜索有了

long long也加了

样例也过了


by tierzero @ 2022-03-03 17:11:33

下面代码已经ac,写了注释

#include<bits/stdc++.h>
using namespace std;
long long aa[30][30][30];
long long n,m,k;
long long w(long long a,long long b,long long c)
{
    if(a<=0||b<=0||c<=0) return 1;
    // 下面两个if改了一下顺序
    if(a>20||b>20||c>20) return w(20,20,20);//这里数组爆了
    if(aa[a][b][c]!=0) return aa[a][b][c];
    if(a<b&&b<c)
    {
        if(!aa[a][b][c-1])aa[a][b][c-1]=w(a,b,c-1);
        if(!aa[a][b-1][c-1])aa[a][b-1][c-1]=w(a,b-1,c-1);
        if(!aa[a][b-1][c])aa[a][b-1][c]=w(a,b-1,c);
        aa[a][b][c]=aa[a][b][c-1]+aa[a][b-1][c-1]-aa[a][b-1][c]; 
    }
    else
    {
        if(!aa[a-1][b][c])aa[a-1][b][c]=w(a-1,b,c);
        if(!aa[a-1][b-1][c])aa[a-1][b-1][c]=w(a-1,b-1,c);
        if(!aa[a-1][b][c-1])aa[a-1][b][c-1]=w(a-1,b,c-1);
        if(!aa[a-1][b-1][c-1])aa[a-1][b-1][c-1]=w(a-1,b-1,c-1);
        aa[a][b][c]=aa[a-1][b][c]+aa[a-1][b][c-1]+aa[a-1][b-1][c]-aa[a-1][b-1][c-1];

    }
    return aa[a][b][c];
}
int main() 
{
#ifndef ONLINE_JUDGE
    freopen("title.in","r",stdin);
    freopen("title.out","w",stdout);
#endif
    while(scanf("%lld%lld%lld",&n,&m,&k))
    {
        memset(aa,0,sizeof(aa));
        if(n==-1&&m==-1&&k==-1)return 0;
        printf("w(%lld, %lld, %lld) = ",n,m,k);
        /* if(n>20)n=20;
        if(m>20)m=20;
        if(k>20)k=20; */
        //这里赋值有问题,(21,20,19)就变成(20,20,19),本来应该是(20,20,20)
        printf("%lld\n",w(n,m,k));
    }
    return 0;
} 

|