这题细节好多,我是用记忆化搜索做的,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;
}