题意:
给出一个 n ⋅ m n \cdot m n⋅m的方格,由 0 0 0 和 # 组成。#可以被填入非负整数,但要满足以下要求:
1. 1. 1. 相邻单元格之间的数值之差不能超过 1 1 1
2. 2. 2. 假如某个单元格上的数字为正整数,那么必须至少存在一个相邻格子上的数值比其小。
求有多少种填数方案。
题解:
结论题。结论:#的数量为 c n t cnt cnt个,则方案数为 2 c n t 2^{cnt} 2cnt ,若初始状态全为#,则方案数要减一。
证明:
假设有 x x x 个#的值为0,那么此时0的数量已经固定了,又因为条件1,所有接下来只能填1,那么1填在哪呢?根据条件2可以知道,1只能填在0的上下左右,填完1后,所有的0就会被包围,再接着就是填2,同理,2也只能填在1的上下左右,以此类推,可以发现,其实就是从0的位置开始 b f s bfs bfs的过程。当0确定后,其他数都能确定。
所以枚举#中0的个数,方案数就是 C ( c n t , 0 ) + C ( c n t , 1 ) + C ( c n t , 2 ) + . . . . . . + C ( c n t , c n t ) = 2 c n t C(cnt,0)+C(cnt,1)+C(cnt,2)+......+C(cnt,cnt)=2^{cnt} C(cnt,0)+C(cnt,1)+C(cnt,2)+......+C(cnt,cnt)=2cnt
当初始状态全为#时, C ( c n t , 0 ) C(cnt,0) C(cnt,0) 不满足条件,因为没有0就无法往下填数,所有要减1。
代码:
#pragma GCC diagnostic error "-std=c++11"
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<ctime>
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> pii;
const int mod=1e9+7;
const int MAXN=2e5+5;
const int inf=0x3f3f3f3f;
ll qpow(ll a,ll b)
{
ll res=1;
while(b){
if(b&1) res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res;
}
int main()
{
int t;
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
int cnt=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
char s;
cin>>s;
if(s=='#') cnt++;
}
}
ll ans=qpow(2,cnt);
if(cnt==n*m) ans=(ans-1+mod)%mod;
printf("%lld\n",ans);
}
}