校内测 10.27 T3 飞行棋【搜索】


题目:

传送门


题意:

有个 n ∗ m n*m nm的棋盘,我们需要在没有颜色的方格内选择一些颜色填入,使得从左上角只能向下或向右走直到右下角不会经过相同颜色的方案数


分析:

直接愉快搜索
当剩余可选颜色数小于步数,无解
而每个格子从没有选过的颜色中任选一个,本质是相同的,所以搜索一遍,统计贡献的时候就算下
颜色种类阔以用状压保存


代码:

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
#include<cmath>
#define LL long long 
#define mo 1000000007
using namespace std;
inline LL read() {
    LL d=0,f=1;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1; s=getchar();}
    while(s>='0'&&s<='9'){d=d*10+s-'0'; s=getchar();}
    return d*f;
}
int t[15][15],tf[15],f[15][15],lg[1030];
int n=read(),m=read(),k=read();
int dfs(int x,int y)
{
	if(y>m) x++,y=1;
	if(x>n) return 1;
	int s=f[x-1][y]|f[x][y-1],last=-1,ans=0;
	int st=~s&((1<<k)-1);
	if(n+m-x-y+1>lg[st]) return 0;
	for(int tt=0;tt<k;tt++)
	{
		if(!(st&(1<<tt))) continue;
		if(!t[x][y]||t[x][y]==tt+1)
		{
			tf[tt+1]++;
			f[x][y]=s|(1<<tt);
			if(tf[tt+1]==1)
			{
				if(last==-1) last=dfs(x,y+1);
				(ans+=last)%=mo;
			}
			else (ans+=dfs(x,y+1))%=mo;
			tf[tt+1]--;
		}
	}
	return ans;
}
int main()
{
	if(n+m-1>k) return !printf("0");
	for(int i=1;i<1024;i++) lg[i]=lg[i>>1]+(i&1);
	for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) tf[t[i][j]=read()]++;
	cout<<dfs(1,1);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值