蓝桥杯备考:记忆化搜索之function
这道题是有重复的问题的,所以我们可以选择记忆化搜索
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 25;
LL ret[N][N][N];
LL dfs(LL a,LL b, LL c)
{
if(a<=0 || b<=0 || c<=0) return 1;
if(a>20 || b>20 || c>20) return dfs(20,20,20);
if(ret[a][b][c]) return ret[a][b][c];
if(a<b&&b<c) return ret[a][b][c] = dfs(a,b,c-1)+dfs(a,b-1,c-1)-dfs(a,b-1,c);
else
return ret[a][b][c] = dfs(a-1,b,c)+dfs(a-1,b-1,c)+dfs(a-1,b,c-1)-dfs(a-1,b-1,c-1);
}
LL a,b,c;
int main()
{
ios::sync_with_stdio(false);cin.tie(0);
while(cin >> a >> b >> c)
{
if(a==-1 && b==-1 && c==-1) break;
printf("w(%lld, %lld, %lld) = %lld\n",a,b,c,dfs(a,b,c));
}
return 0;
}