# re: ZOJ 1168 - Function Run Fun 回复 更多评论
2008-10-08 16:41 by
//更大的问题在于你没更多的保存中间值 ~_~
#include<iostream> //为避免无限的或大量的重复递归,,怎么办??,数组模拟
using namespace std;
const int N=100;
int f[N][N][N];
int recur(int a,int b,int c){
if(a<=0 || b<=0 || c<=0){
f[a][b][c]=1;
}
else if(a>20 || b>20 || c>20){
f[a][b][c]=recur(20,20,20);
}
else if(a<b && b<c){
if(f[a][b][c-1] == 0 )
f[a][b][c-1]=recur(a,b,c-1);
if(f[a][b-1][c-1]== 0)
f[a][b-1][c-1]=recur(a,b-1,c-1);
if(f[a][b-1][c] == 0 )
f[a][b-1][c]=recur(a,b-1,c);
f[a][b][c]=f[a][b][c-1]+f[a][b-1][c-1]-f[a][b-1][c];
}
else{
if(f[a-1][b][c] == 0)
f[a-1][b][c]=recur(a-1,b,c);
if(f[a-1][b][c-1] == 0)
f[a-1][b][c-1]=recur(a-1,b,c-1);
if(f[a-1][b-1][c] == 0)
f[a-1][b-1][c]=recur(a-1,b-1,c);
if(f[a-1][b-1][c-1]==0)
f[a-1][b-1][c-1]=recur(a-1,b-1,c-1);
f[a][b][c]=f[a-1][b][c]+f[a-1][b][c-1]+f[a-1][b-1][c]-f[a-1][b-1][c-1];
}
return f[a][b][c];
}
int main()
{
int a,b,c;
//cout<<"输入3个数: "<<endl;
while(cin>>a>>b>>c) {
//memset(f,0,sizeof(f));//写这个就超时。。。
if(a==-1 && b==-1 && c==-1) break;
if(a<=0 || b<=0 || c<=0)
cout<<"w("<<a<<", "<<b<<", "<<c<<") = "<<1<<endl;
else
cout<<"w("<<a<<", "<<b<<", "<<c<<") = "<<recur(a,b,c)<<endl;
}
return 0;
}