SGU 132. Another Chocolate Maniac 状压dp 难度:1
132. Another Chocolate Maniac
time limit per test: 0.25 sec. 
memory limit per test: 4096 KB
Bob really LOVES chocolate. He thinks he never gets enough. Imagine his joy when his parents told him that they would buy him many rectangular chocolate pieces for his birthday. A piece of chocolate is a 2x1 or1x2 rectangle. Bob's parents also bought him a nice birthday cake, which can be imagined as a matrix having M rows and N columns. Some positions on the cake are occupied by candles, the others are empty. Bob's parents asked their son to place as many chocolate pieces as he can on the empty squares on the cake, in such a manner that no two chocolate pieces overlap. However, he would like to keep the chocolate pieces to himself. That's why, he wants to place only a minimal amount of them on the cake and keep the rest. In order not to make Mon and Dad suspicious, Bob wants to place the chocolate pieces in such a way, that no other piece may be placed on the cake (that is, there won't exist any two adjacent empty squares). Find the minimal number of pieces which need to be placed on the cake, so that they do not overlap and no extra piece may be added.
Input
The first line of the input contains 2 integers: M (1<=M<=70) and N (1<=N<=7). Next, M lines will follow, each of them containing N characters, describing the cake. The character on row i and column j of the cake may be either a '*' (ASCII code 42), representing a candle, or a '.' (ASCII code 46), representing an empty square.
Output
You should output one integer: the minimal amount of pieces of chocolate which need to be placed on the cake.
Sample Input
5 5
.*..*
*....
..**.
**.*.
.**..
Sample Output
4
简单的状态压缩,注意不填充这个格子的时候,可以填充左边的两个,下和右下,下和左下即可,然后就是如果恰有一对上下都没有填充,可以试试能否填充下和下下
#include <cstring>
#include <algorithm>
using namespace std;
#define bin(bit) (1<<(bit))
#define spb(x,bit) ((x)&(1<<(bit)))
const int inf=0x3f3f3f3f;
char maz[75][10];
int dp[2][1<<8][1<<8]; int init[75];
int n,m;
int row,nrow,len;
int inver(int ind){//初始状态转成数字
int ans=0;
int base=1;
for(int i=0;i<m;i++){
if(maz[ind][i]=='*'){
ans+=base;
}
base<<=1;
}
return ans;
}
bool contain(int nowline,int nxtline,int nnxtline){//如果恰好该状态有格子在某个格和下一个格都没填充,检查能否填充下下格,当然,下下格状态就是下下行对应的初始状态
int base=1;
row=nxtline;nrow =nnxtline;len=0;
for(int i=0;i<m;i++){
if((nowline&base)==0&&(nxtline&base)==0){
if((nnxtline&base)!=0)return false;
else {nrow|=base;row|=base;len++;}
}
base<<=1;
}
return true;
}
bool judge(int nowline,int nxtline){//判断这一行相对下一行是否都符合规范(没有格不合题意)
for(int i=0;i<m;i++){
if(spb(nowline,i)==0){
if(spb(nxtline,i)==0){return false;}
if(i<m-1&&spb(nowline,i+1)==0)return false;
}
}
return true;
}
bool judge(int nowline){//判断这一行是否符合规范
for(int i=0;i<m;i++){
if(spb(nowline,i)==0){
if(i<m-1&&spb(nowline,i+1)==0)return false;
}
}
return true;
}
void update(int cnt,int stj,int stk){//用这个状态更新同行的其它状态
for(int i=0;i<m;i++){
if(spb(stj,i)==0){
/*
TT
*/
if(i<m-1&&spb(stj,i+1)==0){
dp[cnt][stj|bin(i+1)|bin(i)][stk]=min( dp[cnt][stj|bin(i+1)|bin(i)][stk],dp[cnt][stj][stk]+1);
} /* ATT
*/
if(i<m-2&&spb(stj,i+2)==0&&spb(stj,i+1)==0){
dp[cnt][stj|bin(i+2)|bin(i+1)][stk]=min( dp[cnt][stj|bin(i+2)|bin(i+1)][stk],dp[cnt][stj][stk]+1);
}
/*
T
T
*/
if(spb(stk,i)==0){
dp[cnt][stj|bin(i)][stk|bin(i)]=min( dp[cnt][stj|bin(i)][stk|bin(i)],dp[cnt][stj][stk]+1);
}
/*
A
TT
*/
if(i<m-1&&spb(stk,i+1)==0&&spb(stk,i)==0){
dp[cnt][stj][stk|bin(i+1)|bin(i)]=min( dp[cnt][stj][stk|bin(i+1)|bin(i)],dp[cnt][stj][stk]+1);
}
/*
A
TT
*/
if(i&&spb(stk,i)==0&&spb(stk,i-1)==0){
dp[cnt][stj][stk|bin(i)|bin(i-1)]=min( dp[cnt][stj][stk|bin(i)|bin(i-1)],dp[cnt][stj][stk]+1);
}
}
}
}
int main(){
memset(dp[0],0x3f,sizeof(dp[0]));
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){//初始化
scanf("%s",maz[i]);
init[i]=inver(i);
}
dp[0][bin(m)-1][init[0]]=0;
init[n]=0;
init [n+1]=0;
int cnt=0; for(int i=0;i<=n;i++){
memset(dp[cnt^1],0x3f,sizeof(dp[0]));
for(int j=0;j<bin(m);j++){//行内dp
for(int k=0;k<bin(m);k++){
if(dp[cnt][j][k]!=inf){
update(cnt,j,k);
}
}
}
// printdp(i,true);
if(i==n)continue;//如果恰好到了最后一行了,就不需要行间转移
for(int j=0;j<bin(m);j++){//行间转移
if(!judge(j))continue;
for(int k=0;k<bin(m);k++){
if(dp[cnt][j][k]==inf)continue;
if(judge(j,k)){//如果已经满足题意
dp[cnt^1][k][init[i+1]]=min(dp[cnt^1][k][init[i+1]],dp[cnt][j][k]);
}
else if(contain(j,k,init[i+1])){//否则试试染下下
dp[cnt^1][row][nrow]=min(dp[cnt^1][row][nrow],dp[cnt][j][k]+len);
}
}
}
// printdp(i,false);
cnt^=1;
}
int ans=inf;
for(int j=0;j<bin(m);j++){
if(!judge(j))continue;
ans=min(ans,dp[cnt][j][0]);
}
printf("%d\n",ans);
return 0;
}
SGU 132. Another Chocolate Maniac 状压dp 难度:1的更多相关文章
- SGU 132 Another Chocolate Maniac 状态压缩DP
		
感觉不是很好写的一道状态压缩. dp[i][j][k]表示第 i 行状态为k,第i - 1行状态为 j,具体细节见代码. 内存卡的很死,要用滚动数组. 还有一个比较坑爹的地方是它在输入蛋糕的时候中间可 ...
 - SGU 223 Little Kings(状压DP)
		
Description 用字符矩阵来表示一个8x8的棋盘,'.'表示是空格,'P'表示人质,'K'表示骑士.每一步,骑士可以移动到他周围的8个方格中的任意一格.如果你移动到的格子中有人质(即'P'), ...
 - SGU 132.Another Chocolate Maniac
		
时间限制:0.25s 空间限制:4M 题目: Bob非常喜欢巧克力,吃再多也觉得不够.当他的父母告诉他将要买很多矩形巧克力片为他庆祝生日时,他的喜悦是能被理解的.巧克力都是 2x1 或 1x2 的矩形 ...
 - SGU 223 little kings BSOJ2772 状压DP
		
1896 [SCOI2005]互不侵犯King [问题描述]在n*n(1<=n<=10)的棋盘上放k(0<=k<=n*n)个国王(可攻击相邻的8 个格子),求使它们无法互相攻击 ...
 - SGU 131. Hardwood floor 状压dp 难度:2
		
131. Hardwood floor time limit per test: 0.25 sec. memory limit per test: 4096 KB The banquet hall o ...
 - 【模拟8.11】星空(差分转化,状压DP,最短路)
		
一道很好的题,综合很多知识点. 首先复习差分: 将原来的每个点a[i]转化为b[i]=a[i]^a[i+1],(如果是求和形式就是b[i]=a[i+1]-a[i]) 我们发现这样的方便在于我 ...
 - BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
		
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
 - nefu1109 游戏争霸赛(状压dp)
		
题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...
 - poj3311 TSP经典状压dp(Traveling Saleman Problem)
		
题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
 
随机推荐
- Python开发【模块】:matplotlib 绘制折线图
			
matplotlib 1.安装matplotlib ① linux系统安装 # 安装matplotlib模块 $ sudo apt-get install python3-matplotlib # 如 ...
 - Linux下的Daemon简介(转)
			
add by zhj: 接触这个概念也有段时间了,但一直没搞明白,今天总算明白了. 原文:http://wenku.baidu.com/link?url=FEJeQ0J45YaFXansxT2GVVB ...
 - SQL基础--查询之一--单表查询
			
SQL基础--查询之一--单表查询
 - http和socket之长连接和短连接区别
			
TCP/IP TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层. 在网络层有IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议. 在传输层中有TCP协议与UDP协议. 在应 ...
 - 在SQL Server里如何进行数据页级别的恢复
			
在SQL Server里如何进行页级别的恢复 关键词:数据页修复 在今天的文章里我想谈下每个DBA应该知道的一个重要话题:在SQL Server里如何进行页级别还原操作.假设在SQL Server里你 ...
 - C#基础笔记(第九天)
			
1.面向过程<-->面向对象面向过程:面向的是完成这件事儿的过程,强调的是完成这件事儿的动作. 面向对象:找个对象帮你做事儿.意在写出一个通用的代码,屏蔽差异. 每个人都有姓名,性别,身高 ...
 - enum 枚举类型默认值
			
enum value { one, two, three, four }; 默认值
 - RabittMQ实践(一): RabbitMQ的安装、启动
			
安装: 启动监控管理器:rabbitmq-plugins enable rabbitmq_management 关闭监控管理器:rabbitmq-plugins disable rabbitmq_ ...
 - jQuery源码分析--Event模块(1)
			
jQuery的Event模块提供了强大的功能:事件代理,自定义事件,自定义数据等.今天记录一下它实现的原理. 我们都知道,在js的原生事件中,有事件对象和回调函数这两样东西.但是事件对象是只读的,所以 ...
 - Python3.x:函数定义
			
Python3.x:函数定义 1,函数定义: def 函数名称([参数1,参数2,参数3......]): 执行语句 2,实例一(不带参数和没返回值): def helloWorld(): print ...