hdu(1331)(通过记忆化搜索减少程序运行时间)

#include <stdio.h>
#include <string.h>
int x[150][150][150];
int w(int a,int b,int c)
{
    if (a<=0||b<=0||c<=0)
    return 1;
    if (x[a][b][c])
    return x[a][b][c];//用x[a][b][c]进行保留数据,为一个技巧
    if (a>20||b>20||c>20)
    return x[a][b][c]=w(20,20,20);
    if (a<b&&b<c)
    return x[a][b][c] =w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
    return x[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()
{
    int a,b,c;
    memset(x,0,sizeof(x));
    while(scanf("%d%d%d",&a,&b,&c)!=EOF)
    {
        if (a==-1&&b==-1&&c==-1)
        break;
        if (a<=0||b<=0||c<=0)
       {printf ("w(%d, %d, %d) = %d\n",a,b,c,1);continue;}
        if (a>20||b>20||c>20)
        {printf ("w(%d, %d, %d) = %d\n",a,b,c,w(20,20,20));continue;}
        else
        {printf ("w(%d, %d, %d) = %d\n",a,b,c,w(a,b,c));continue;}
    }
    return 0;
}

 

posted @ 2013-04-23 19:08  一线添  阅读(147)  评论(0编辑  收藏  举报