您的位置:首页 > 其它

POJ 1597 Function Run Fun

2017-05-05 18:27 274 查看
记忆化搜索。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<string>
#include<iostream>
using namespace std;

struct X{
int x,y,z;
X(int xx,int yy,int zz)
{
x=xx;
y=yy;
z=zz;
}
bool operator < (const X &a) const {
if(x!=a.x) return x>a.x;
if(y!=a.y) return y>a.y;
return z>a.z;
}
};
int a,b,c;
map<X,bool>f;
map<X,int>w;

int work(int x,int y,int z)
{
if(f[X(x,y,z)]) return w[X(x,y,z)];

f[X(x,y,z)]=true;
if(x <= 0 || y <= 0 || z <= 0) w[X(x,y,z)]=1;
else if(x > 20 || y > 20 || z > 20) w[X(x,y,z)] = work(20,20,20);
else if(a < b && b < c) w[X(x,y,z)] =work(x, y, z-1) + work(x, y-1, z-1) - work(x, y-1, z);
else w[X(x,y,z)] = work(x-1, y, z) + work(x-1, y-1, z) + work(x-1, y, z-1) - work(x-1, y-1, z-1);

return w[X(x,y,z)];
}

int main()
{
while(~scanf("%d%d%d",&a,&b,&c))
{
if(a==-1&&b==-1&&c==-1) break;
printf("w(%d, %d, %d) = %d\n",a,b,c,work(a,b,c));
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: