SwethessPotion @ 2024-02-10 12:07:24
题目
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <cstdlib>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#define INT_MAX 2147483647
#define INT_MIN -2147483648
#define LLONG_MAX 9223372036854775807
#define LLONG_MIN -9223372036854775808
#define ll long long
using namespace std;
ll funcw[21][21][21];
ll w(ll a, ll b, ll c)
{
if (a <= 0 || b <= 0 || c <= 0)
{
return 1;
}
if (a > 20 || b > 20 || c > 20)
{
a = 20, b = 20, c = 20;
}
if (funcw[a][b][c] != 0) return funcw[a][b][c];
if (a < b && b < c)
{
return w(a, b, c - 1) + w(a - 1, b - 1, c) + w(a - 1, b, c - 1) - w(a - 1, b - 1, c - 1);
}
return 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()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
ll a, b, c;
cin >> a >> b >> c;
while (a != -1 || b != -1 || c != -1)
{
cout << "w(" << a << ", " << b << ", " << c << ") = " << w(a, b, c) << endl;
cin >> a >> b >> c;
}
return 0;
}
by him0715 @ 2024-02-11 11:36:49
//首先,你引入了一堆没有必要的头文件(看上去很麻烦
//然后,你的记忆化搜索只在第37行出现过一次,应该在四个if+最后的return里都使用,所以你这funcw[21][21][21]就等于没用一样
//改动后的函数部分:
ll w(ll a, ll b, ll c){
if (a <= 0 || b <= 0 || c <= 0)
return 1;
if (a > 20 || b > 20 || c > 20)
return w(20, 20, 20);
if (funcw[a][b][c] != 0)
return funcw[a][b][c];
if (a < b && b < c)
return funcw[a][b][c] = w(a, b, c - 1) + w(a, b - 1, c - 1) - w(a, b - 1, c);
return funcw[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);
}
by him0715 @ 2024-02-11 11:37:11
这样就可以AC了
by qusia_MC @ 2024-02-12 19:20:18
这么多头文件(好像一个iostream就够了)我怀疑你在炫技但我没有证据
说正事,额你这个只开了个数组啊,相当于你用记忆化搜索,但是啥也没记,这不就相当于啥也没有?
应该把赋值的也整上,这样就相当于记住了,才能不重复计算:
改动:
这个部分:return w(a, b, c - 1) + w(a - 1, b - 1, c) + w(a - 1, b, c - 1) - w(a - 1, b - 1, c - 1);改为:return funcw[a][b][c]=w(a, b, c - 1) + w(a - 1, b - 1, c) + w(a - 1, b, c - 1) - w(a - 1, b - 1, c - 1);
还有,下面的return,也同上,在那一串东西的前面加个funcw[a][b][c]=
by qusia_MC @ 2024-02-12 19:20:37
@ SwethessPotion
by qusia_MC @ 2024-02-12 19:20:52
@SwethessPotion
by qusia_MC @ 2024-02-12 19:27:04
最后附上我的AC代码(你那个里头不必要的东西太多)
#include<bits/stdc++.h>
using namespace std;
#define int long long
int k,w[21][21][21];
int psort(int a,int b,int c)
{
if(a<=0||b<=0||c<=0)return 1;
//情况1
if(a>20||b>20||c>20)
{
return psort(20,20,20);
}//情况2
if(w[a][b][c]!=0)return w[a][b][c];
//脑子里记住了,下次不必要算一遍,拿记忆来就可以
if(a<b&&b<c)return w[a][b][c]=psort(a,b,c-1)+psort(a,b-1,c-1)-psort(a,b-1,c);
//情况3
else return w[a][b][c]=psort(a-1,b,c)+psort(a-1,b-1,c)+psort(a-1,b,c-1)-psort(a-1,b-1,c-1);
//最后一种
}
signed main()
{
int a,b,c;
psort(20,20,20);
while(1)
{
cin>>a>>b>>c;
if(a==-1&&b==-1&&c==-1)return 0;
//结束标志
printf("w(%lld, %lld, %lld) = %d\n",a,b,c,psort(a,b,c));
//printf很方便
}
return 0;
}
总结:记忆化搜索不仅要调用记忆,还要往脑子里记东西
by SwethessPotion @ 2024-02-13 09:17:53
谢谢 @