题目描述
比如 w(30,-1,0)w(30,−1,0)既满足条件1又满足条件2
这种时候我们就按最上面的条件来算
所以答案为1
输入
测试样例由多组测试数据组成。每组测试数据第一行输入三个整数 a , b ,c ( -20 <= a,b,c <= 20 )
如果a,b,c均为-1则退出程序
输出
输出递归后的结果
样例输入 Copy
1 1 1
2 2 2
-1 -1 -1
样例输出 Copy
w(1, 1, 1) = 2
w(2, 2, 2) = 4
代码 (记忆化搜索)
#include<bits/stdc++.h>
using namespace std;
long long dp[50][50][50];
long long dfs(long long n,long long m,long long h){
if(n>0&&m>0&&h>0&&dp[n][m][h]!=0){
return dp[n][m][h];
}
if(n<=0||m<=0||h<=0){
return 1;
}
if(n>20||m>20||h>20) {
dp[n][m][h]=dfs(20,20,20);
return dp[n][m][h];
}
if(n<m&&m<h){
dp[n][m][h]=dfs(n,m,h-1)+dfs(n,m-1,h-1)-dfs(n,m-1,h);
return dp[n][m][h];
}
dp[n][m][h]=dfs(n-1,m,h)+dfs(n-1,m-1,h)+dfs(n-1,m,h-1)-dfs(n-1,m-1,h-1);
return dp[n][m][h];
}
int main(){
long long a,b,c;
while(cin>>a>>b>>c){
if(a==-1&&b==-1&&c==-1) break;
memset(dp,0,sizeof(dp));
cout<<"w("<<a<<", "<<b<<", "<<c<<") = "<<dfs(a,b,c)<<endl;
}
return 0;
}