Mondriaan's Dream 題解(棋盤狀壓問題)
阿新 • • 發佈:2021-08-23
題目連結
題目大意
現在有一個 n×m 的方格棋盤,和無限的 1×2 的骨牌。
問有多少種方法可以用骨牌鋪滿棋盤。1 ≤ n,m ≤ 11
題目思路
這種算是狀壓dp的模板題目
主要是思考上一行和這一行的轉移即可
需要兩個連續的空位,並且上一行的這兩個位置也得已經被覆蓋。
如果豎著:
(a) 上一行對應的位置是空的,我們把那個空填上。
(b) 上一行對應的位置是被覆蓋的,那麼我們把這一行的位置設為空,表示下一行的對應位置必須豎放,填上這塊空白。
執行dfs預處理所有狀態即可
程式碼
卷也卷不過,躺又躺不平#include<set> #include<map> #include<queue> #include<stack> #include<cmath> #include<cstdio> #include<vector> #include<string> #include<cstring> #include<iostream> #include<algorithm> #define se second #define debug cout<<"I AM HERE"<<endl; using namespace std; typedef long long ll; const int maxn=1e5+5,inf=0x3f3f3f3f,mod=1e9+7; const double eps=1e-6; int n,m; int tot=0; int from[maxn],to[maxn]; ll dp[15][1<<12]; void dfs(int d,int pre,int now){ if(d>m) return ; if(d==m){ ++tot; from[tot]=pre; to[tot]=now; } dfs(d+2,pre<<2|3,now<<2|3); dfs(d+1,pre<<1,now<<1|1); dfs(d+1,pre<<1|1,now<<1); } signed main(){ while(scanf("%d%d",&n,&m)!=-1&&(n+m)){ memset(dp,0,sizeof(dp)); tot=0; dfs(0,0,0); dp[0][(1<<m)-1]=1; for(int i=1;i<=n;i++){ for(int j=1;j<=tot;j++){ dp[i][to[j]]+=dp[i-1][from[j]]; } } printf("%lld\n",dp[n][(1<<m)-1]); } return 0; }