CSP201409T5拼图

题意:给出一个 n × m n×m n×m的方格图,现在要用如下L型的占3个的积木拼到这个图中,总共有多少种拼法使图满。

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
long long n,m,k=1,Now;
int Mod=1000000007;
struct Matrix
{
	long long a[129][129];
	Matrix(){for(long long i=0;i<k;i++)a[i][i]=1;}
	Matrix(int x){memset(a,0,sizeof(a));}
	Matrix operator*(Matrix& B)//矩阵乘法 
	{
		Matrix t(0);
		for(int i=0;i<k;i++)
		for(int j=0;j<k;j++)
		for(int l=0;l<k;l++)
		t.a[i][j]=(t.a[i][j]+a[i][l]*B.a[l][j])%Mod;
		return t;
	}
}; 	
Matrix quickmul(Matrix ans,long long p)//矩阵快速幂 
{
	Matrix t;	
	while(p)
	{
		if(p&1)t=t*ans;
		ans=ans*ans;
		p>>=1;
	}
	return t;
}
Matrix A(0);
void dfs(int now,int next)
{
	//cout<<now<<" "<<next<<endl;
	if(now+1==k)
	{
		A.a[Now][next]++;
		return ;
	}
	for(int l=0;l<m;l++)
	{
		if((now>>l)&1)continue;//当前列的位置上有拼图则跳过
		int temp=1<<l;
		int now1,next1;//递归,四种拼图 
		now1=temp|(temp<<1);// 1 1
		next1=temp;         // 0 1
		if((l+1<m)&&!(now&now1||next&next1))dfs(now1|now,next1|next);
		
		now1=temp|(temp<<1);// 1 1 
		next1=temp<<1;      // 1 0
		if((l+1<m)&&!(now&now1||next&next1))dfs(now1|now,next1|next);
		
		now1=temp;		    // 0 1 
		next1=temp|temp<<1; // 1 1
		if((l+1<m)&&!(now&now1||next&next1))dfs(now1|now,next1|next);
		
		now1=temp;	        // 1 0 
		next1=temp|temp>>1; // 1 1
		if((l)&&!(now&now1||next&next1))dfs(now1|now,next1|next);
		return ;
	}
}
int main()
{
	cin>>n>>m;
	for(long long i=0;i<m;i++)k<<=1;
	for(Now=0;Now<k;Now++)dfs(Now,0);
	Matrix ANS=quickmul(A,n);	
	/*for(int i=0;i<k;i++)
	{
		for(int j=0;j<k;j++)cout<<ANS.a[i][j]<<" ";
		cout<<endl;	
	}*/
	cout<<ANS.a[0][0]<<endl;
	return 0;
}
相关推荐
hi0_67 分钟前
03 数组 VS 链表
java·数据结构·c++·笔记·算法·链表
aPurpleBerry7 分钟前
hot100 hot75 栈、队列题目思路
javascript·算法
卷福同学2 小时前
【AI编程】AI+高德MCP不到10分钟搞定上海三日游
人工智能·算法·程序员
mit6.8242 小时前
[Leetcode] 预处理 | 多叉树bfs | 格雷编码 | static_cast | 矩阵对角线
算法
皮卡蛋炒饭.2 小时前
数据结构—排序
数据结构·算法·排序算法
??tobenewyorker3 小时前
力扣打卡第23天 二叉搜索树中的众数
数据结构·算法·leetcode
贝塔西塔3 小时前
一文读懂动态规划:多种经典问题和思路
算法·leetcode·动态规划
众链网络3 小时前
AI进化论08:机器学习的崛起——数据和算法的“二人转”,AI“闷声发大财”
人工智能·算法·机器学习
4 小时前
Unity开发中常用的洗牌算法
java·算法·unity·游戏引擎·游戏开发
飒飒真编程5 小时前
C++类模板继承部分知识及测试代码
开发语言·c++·算法