【记忆化搜索】Function

网址:https://www.luogu.com.cn/problem/P1464
在这里插入图片描述

u1s1是水题,但是呃先从水题开始吧。
题解

#include<iostream>
using namespace std;
typedef long long ll;
#define wd(x,y,z) (ac[x][y][z] ? ac[x][y][z] : ac[x][y][z]=w(x,y,z))
ll a,b,c;
int ac[25][25][25];
int w(int a,int b,int c){
	if(a<=0||b<=0||c<=0) return 1;
	if(a>20||b>20||c>20) return wd(20,20,20);
	if(a<b&&b<c) return wd(a,b,c-1)+wd(a,b-1,c-1)-wd(a,b-1,c);
	return wd(a-1,b,c)+wd(a-1,b-1,c)+wd(a-1,b,c-1)-wd(a-1,b-1,c-1);
}
int main(){
//	ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
	while(~scanf("%lld %lld %lld",&a,&b,&c)){
		if(a==-1&&b==-1&&c==-1) break;
		printf("w(%lld, %lld, %lld) = ",a,b,c); 
		printf("%d\n",w(a,b,c));
	}
} 

宏定义有时候还是非常的重要的
可以减少很多代码量 但是要注意别写bug

每次都判断一下数组里有没有被记录过,
如果没有就记录,然后直接用记录过的数组。

我刚开始是用map的 套了两层pair sb了,
其实直接就三维数组就好了。

发布了62 篇原创文章 · 获赞 0 · 访问量 656

猜你喜欢

转载自blog.csdn.net/weixin_44745441/article/details/104977545