A_pier @ 2021-10-30 12:50:46
#include<iostream>
#include<cstring>
using namespace std;
long long f[21][21][21] = { 0 };
long long dfs(long long a, long long b, long long c)
{
if (a <= 0 || b <= 0 || c <= 0)
return 1;
if (f[a][b][c])
return f[a][b][c];
if (a > 20 || b > 20 || c > 20)
return f[20][20][20];
if (a < b&&b < c)
return f[a][b][c] = dfs(a, b, c - 1) + dfs(a, b - 1, c - 1) - dfs(a, b - 1, c);
return f[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);
}
int main()
{
long long x, y, z;
while (cin>>x>>y>>z) {
if (x ==-1&& y ==-1&& z == -1)
break;
memset(f, 0, sizeof(f));
dfs(x, y, z);
cout << "w(" << x << ", " << y << ", " << z << ") = " << f[x][y][z] << endl;
}
return 0;
}
by Yanzj @ 2021-10-30 13:46:26
把四个if改成else if?
by Yanzj @ 2021-10-30 13:49:17
而且,输出的时候直接cout<<……<<dfs(xyz)否则return 是没用的
by Yanzj @ 2021-10-30 13:52:40
第三,当a或b或c>20,你要return dfs(20,20,20)不是f(20,20,20)即0
by Yanzj @ 2021-10-30 14:07:02
re的话可能输入有问题 在本地试一下 随便输入一堆然后以-1结尾
by A_pier @ 2021-10-30 15:27:04
@Yanzj 好的谢谢,我去改下
by Yanzj @ 2021-10-31 11:44:42
#include<bits/stdc++.h>
using namespace std;
int f[21][21][21];
bool flag=true;
int w(long long a,long long b,long long c){
if(a<=0||b<=0||c<=0) return 1;
//else if((a==b)||(a==c)) return f[a][b][c]=pow(2,a);
//else if(b==c) return f[a][b][c]=pow(2,b);
if(f[a][b][c]!=-1) return f[a][b][c];//这个是记忆化搜索的核心;每次输入完千万不要把f memset成0,跑的很快
//if(a>20||b>20||c>20) return f[20][20][20]=w(20,20,20);
if(a<b&&b<c) return f[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
else return f[a][b][c]=w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1);
}
int main(){
long long a,b,c;
memset(f,-1,sizeof f);//只初始化一次,建议-1,因为答案一定非负,方便记忆化
while(flag){
cin>>a>>b>>c;
if(a==-1&&b==-1&&c==-1) break;
cout<<"w("<<a<<", "<<b<<", "<<c<<") = ";
if(a<=0||b<=0||c<=0) cout<<1<<endl;
else if(a>20||b>20||c>20) {
cout<<w(20,20,20)<<endl;}
else{
cout<<w(a,b,c)<<endl;}
}
return 0;
}
我自己写的代码
by A_pier @ 2021-11-01 15:49:17
@Yanzj 很漂亮的代码,谢谢大佬之前的帮忙,我之后也成功ac了,谢谢!