题目:
题意:
有个 n ∗ m n*m n∗m的棋盘,我们需要在没有颜色的方格内选择一些颜色填入,使得从左上角只能向下或向右走直到右下角不会经过相同颜色的方案数
分析:
直接愉快搜索
当剩余可选颜色数小于步数,无解
而每个格子从没有选过的颜色中任选一个,本质是相同的,所以搜索一遍,统计贡献的时候就算下
颜色种类阔以用状压保存
代码:
#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;
}