题目链接:abc_196d
题目大意
一个房间大小是h*w,有两种板砖,a块1*1的,b块2*1的。用完所有板砖可以铺满整个房间,问你有几种铺的方案。h*w<=16
思路
看到16,就可以二进制记录状态了嘛,然后二维转一维进行搜索,5(101)表示1,3位置已经砖覆盖,具体看代码。
ac代码
#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(time(0));
#define io cin.tie(0);ios::sync_with_stdio(false);
#define ok(x, y) x >= 1 && x <= n && y >= 1 && y <= m
#define debug(x) cout<<#x<<"="<<x<<endl
#define lowbit(x) x&(-x)
#define pii pair<int,int>
#define mk make_pair
#define ll long long
#define ull unsigned long long
#define rs p<<1|1
#define ls p<<1
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 1e18;
inline ll read(){
ll p=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){p=(p<<1)+(p<<3)+(c^48),c=getchar();}
return f*p;
}
void print(__int128 x){
if(x<0) {putchar('-'); x=-x;}
if (x>9) print(x/10);
putchar('0'+x%10);
}
int vis[105];
int h, w, ans;
void dfs(int x, int now, int a, int b){
if(x == h * w){ //因为从0开始的,到h*w说明已经铺完了,方式数加加!
ans ++;
return;
}
if((now >> x) & 1) { //已经覆盖过了,继续搜下一个
dfs(x + 1, now, a, b);
}else{ //当前没有被覆盖
if(a){ //2*1砖的还有,只需要考虑向右或者向下
if(x % w != (w - 1)){ //没有到边界,可以向右
int bit = now; //bit是状态,二进制1的位置表示已选,向右就要加上当前的和右边的
bit |= (1 << x);
bit |= (1 << (x + 1));
dfs(x + 1, bit, a - 1, b);
}
if(x + w < w * h){ //没有出边界,可以向下
int bit = now; //bit是状态,二进制1的位置表示已选,向下就要加上当前的和下面的
bit |= (1 << x);
bit |= (1 << (x + w));
dfs(x + 1, bit, a - 1, b);
}
}
if(b){ //1*1砖还有,只需要添加当前状态就好了
int bit = now;
bit |= (1 << x);
dfs(x, bit, a, b - 1);
}
}
}
void solve(){
int a, b;
cin >> h >> w >> a >> b;
dfs(0, 0, a, b);
cout << ans << endl;
}
int main(){
// freopen("1.in", "r", stdin);
// freopen("std.out", "w", stdout);
// cout << fixed << setprecision(6)
io;
solve();
return 0;
}