今天了解了一下记忆化搜索,作为一种dp,我难以理解
#include<iostream>
#include<cstdio>
using namespace std;
long long dp[22][22][22];
long long w(long long a,long long b,long long c){
if(a<=0||b<=0||c<=0){
return 1;
}
if(a>20||b>20||c>20){
return w(20,20,20);
}
if(a<b&&b<c){
if(dp[a][b][c-1]==0){
dp[a][b][c-1]=w(a,b,c-1);
}
if(dp[a][b-1][c-1]==0){
dp[a][b-1][c-1]=w(a,b-1,c-1);
}
if(dp[a][b-1][c]==0){
dp[a][b-1][c]=w(a,b-1,c);
}
dp[a][b][c]=dp[a][b][c-1]+dp[a][b-1][c-1]-dp[a][b-1][c];
}
else {
if(dp[a-1][b][c]==0){
dp[a-1][b][c]=w(a-1,b,c);
}
if(dp[a-1][b-1][c]==0){
dp[a-1][b-1][c]=w(a-1,b-1,c);
}
if(dp[a-1][b][c-1]==0){
dp[a-1][b][c-1]=w(a-1,b,c-1);
}
if(dp[a-1][b-1][c-1]==0){
dp[a-1][b-1][c-1]=w(a-1,b-1,c-1);
}
dp[a][b][c]=dp[a-1][b][c]+dp[a-1][b-1][c]+dp[a-1][b][c-1]-dp[a-1][b-1][c-1];
}
return dp[a][b][c];
}
long long a,b,c;
int main(){
while(cin>>a>>b>>c){
if(a==-1&&b==-1&&c==-1){
return 0;
}
printf("w(%lld, %lld, %lld) = ",a,b,c);
printf("%lld\n",w(a,b,c));
}
return 0;
}
出自洛谷P1464 Function
输入:
1 1 1
2 2 2
-1 -1 -1
输出:
w(1, 1, 1) = 2
w(2, 2, 2) = 4
标签:return,22,long,printf,20,日记,lld
From: https://www.cnblogs.com/2401377187PandZ/p/18178624