hdu 4363 Draw and paint

本文详细介绍了如何运用记忆化搜索技术解决具有多种细节条件的问题。通过代码实例,展示了如何利用DP(动态规划)算法,结合记忆化搜索优化过程,有效解决复杂问题。文章深入探讨了状态转移的具体实现,并提供了完整的代码实现,旨在帮助读者理解和应用记忆化搜索在实际问题求解中的高效策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这题细节好多,我是用记忆化搜索做的,dp[x][y][a][b][c][d][w]前两维记录大小,然后是四个方向相邻的颜色,最后一维表示要横着切还是竖着切,具体的转移直接看代码吧

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;
#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif
/************ for topcoder by zz1215 *******************/
#define FOR(i,a,b)      for( int i = (a) ; i <= (b) ; i ++)
#define FFF(i,a)        for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a,b)      for( int i = (a) ; i >= (b) ; i --)
#define S64(a)          scanf(in64,&a)
#define SS(a)           scanf("%d",&a)
#define LL(a)           ((a)<<1)
#define RR(a)           (((a)<<1)+1)
#define pb              push_back
#define CL(Q)           while(!Q.empty())Q.pop()
#define MM(name,what)   memset(name,what,sizeof(name))
#define MAX(a,b)        ((a)>(b)?(a):(b))
#define MIN(a,b)        ((a)<(b)?(a):(b))
#define read            freopen("in.txt","r",stdin)
#define write           freopen("out.txt","w",stdout)

const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-9;
const double pi = acos(-1.0);
const int maxn = 41;
const int maxc = 5;
const int mod=1000000007;

i64 dp[maxn][maxn][maxc][maxc][maxc][maxc][2];
bool vis[maxn][maxn][maxc][maxc][maxc][maxc][2];
int n,m;

i64 find(int x,int y,int a,int b,int c,int d,int w)
{	
	if(vis[x][y][a][b][c][d][w]) return dp[x][y][a][b][c][d][w];
	if(x==0 || y==0)
	{	
		return 1;
	}
	else if(w==0)
	{
		i64 temp=0;
		for(int i=1;i<=x-1;i++)
		{
			for(int j=1;j<=4;j++)
			{
				if(j==a || j==c || j==d) continue;
				temp+=find(x-i,y,j,b,c,d,1);
				temp%=mod;
			}	
			for(int j=1;j<=4;j++)
			{
				if(j==b || j==c || j==d) continue;		
				temp+=find(i,y,a,j,c,d,1);
				temp%=mod;
			}
		}
		temp+=mod;
		int t2=0;
		for(int i=1;i<=4;i++)
		{
			if(i==a || i==c || i==d) continue;
			for(int j=1;j<=4;j++)
			{
				if(j==c || j==d || j==b || j==i) continue; 				
				t2++;
			}
		}
		temp-=t2*(x-1);
		for(int i=1;i<=4;i++)
		{
			if(i!=a && i!=b && i!=c && i!=d)
			{
				temp++;
			}
		}	
		temp%=mod;
		vis[x][y][a][b][c][d][w]=true;
		dp[x][y][a][b][c][d][w]=temp;	
		return temp;
	}
	else if(w==1)
	{
		i64 temp=0;
		for(int i=1;i<=y-1;i++)	
		{
			for(int j=1;j<=4;j++)
			{
				if(j==c || j==a || j==b) continue;
				temp+=find(x,y-i,a,b,j,d,0);
				temp%=mod;
			}
			for(int j=1;j<=4;j++)
			{
				if(j==a || j==b || j==d) continue;
				temp+=find(x,i,a,b,c,j,0);
				temp%=mod;
			}
		}	
		temp+=mod;
		int t2=0;
		for(int i=1;i<=4;i++)
		{
			if(i==a || i==b || i==c) continue;
			for(int j=1;j<=4;j++)
			{	
				if(j==i || j==a || j==b || j==d) continue;
				t2++;
			}
		}
		temp-=t2*(y-1);
		for(int i=1;i<=4;i++)
		{
			if(i!=a && i!=b && i!=c && i!=d)
			{
				temp++;
			}
		}
		temp%=mod;
		vis[x][y][a][b][c][d][w]=true;
		dp[x][y][a][b][c][d][w]=temp;
		return temp;
	}
}

int main()
{
	int T;
	cin>>T;	
	MM(vis,false);
	while(T--)
	{	
		cin>>n>>m;
		cout<<find(n,m,0,0,0,0,0)<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值