首页 > 代码库 > sgu 131 状压DP
sgu 131 状压DP
棋盘覆盖(二)
时间限制:1000 ms | 内存限制:65535 KB- 描述
- The banquet hall of Computer Scientists‘ Palace has a rectangular form of the size M x N (1<=M<=9, 1<=N<=9). It is necessary to lay hardwood floors in the hall. There are wood pieces of two forms:
1) rectangles (2x1)
2) corners (squares 2x2 without one 1x1 square)
You have to determine X - the number of ways to cover the banquet hall.
Remarks. The number of pieces is large enough. It is not allowed to leave empty places, or to cover any part of a surface twice, or to saw pieces.
- 输入
- The first line contains natural number M. The second line contains a natural number N.
- 输出
- First line should contain the number X, or 0 if there are no solutions.
- 样例输入
2 3
- 样例输出
5
题意:
有1*2的砖块和2*2但缺一个角的砖块,用他们恰好铺满m*n的矩形有几种方案。
代码://思路就是:状压,从上到下铺dfs,sta表示本行的状态,next表示下一行的状态,每次要把sta铺满//sta,next随之变化。最后要m+1行的状态为0才可以。#include<iostream>#include<cstdio>#include<cstring>using namespace std;typedef long long ll;ll dp[11][1<<10];int n,m;bool chak(int sta,int x){ int tmp=(sta&(1<<x)); return !tmp;}void dfs(int sta,int next,int col,int row,ll w){ if(col==n){ dp[row+1][next]+=w; return; } if(sta&(1<<col)) dfs(sta,next,col+1,row,w); //不放 else{ int st=(sta|(1<<col)); if(chak(next,col)){ dfs(sta|(1<<col),next|(1<<col),col+1,row,w);//竖着的1*2 if(col+1<n&&chak(sta,col+1)) dfs(st|(1<<(col+1)),next|(1<<col),col+1,row,w);//缺右上角的2*2 st=(next|(1<<col)); if(col+1<n&&chak(next,col+1)) dfs(sta|(1<<col),st|(1<<(col+1)),col+1,row,w);//缺右下角的2*2 if(col-1>=0&&chak(next,col-1)) dfs(sta|(1<<col),st|(1<<(col-1)),col+1,row,w);//缺左下角的2*2 } st=(sta|(1<<col)); if(col+1<n&&chak(sta,col+1)){ dfs(st|(1<<(col+1)),next,col+1,row,w);//横着的1*2 if(chak(next,col+1)) dfs(st|(1<<(col+1)),next|(1<<(col+1)),col+1,row,w);//缺左上角的2*2 } }}int main(){ while(scanf("%d%d",&m,&n)==2){ memset(dp,0,sizeof(dp)); dp[1][0]=1; int N=(1<<n); for(int i=1;i<=m;i++){ for(int j=0;j<N;j++){ if(dp[i][j]) dfs(j,0,0,i,dp[i][j]); } } printf("%lld\n",dp[m+1][0]); } return 0;}
sgu 131 状压DP
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。