记忆化搜索 POJ1579

#include <cstring>
#include <iostream>

using namespace std;

int s[21][21][21];

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);
    else if(a<b&&c<c)
    {
        if(s[a][b][c]!=0)
            return s[a][b][c];
        else
        { 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];
        }
    }
    else
    {
        if(s[a][b][c]!=0)
            return s[a][b][c];
        else
        {
            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);
            return s[a][b][c];
        }
    }
}

int main()
{
    int a,b,c;
    memset(s,0,sizeof(s));
    while(cin>>a>>b>>c&&(a!=-1||b!=-1||c!=-1))
    cout<<"w("<<a<<", "<<b<<", "<<c<<") = "<<w(a,b,c)<<endl;
    return 0;
}

posted @ 2015-07-22 19:09  相儒以沫  阅读(208)  评论(0编辑  收藏  举报