POJ 1597 Function Run Fun
记忆化搜索。
#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; }