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 and column 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的更多相关文章

  1. SGU 132 Another Chocolate Maniac 状态压缩DP

    感觉不是很好写的一道状态压缩. dp[i][j][k]表示第 i 行状态为k,第i - 1行状态为 j,具体细节见代码. 内存卡的很死,要用滚动数组. 还有一个比较坑爹的地方是它在输入蛋糕的时候中间可 ...

  2. SGU 223 Little Kings(状压DP)

    Description 用字符矩阵来表示一个8x8的棋盘,'.'表示是空格,'P'表示人质,'K'表示骑士.每一步,骑士可以移动到他周围的8个方格中的任意一格.如果你移动到的格子中有人质(即'P'), ...

  3. SGU 132.Another Chocolate Maniac

    时间限制:0.25s 空间限制:4M 题目: Bob非常喜欢巧克力,吃再多也觉得不够.当他的父母告诉他将要买很多矩形巧克力片为他庆祝生日时,他的喜悦是能被理解的.巧克力都是 2x1 或 1x2 的矩形 ...

  4. SGU 223 little kings BSOJ2772 状压DP

    1896 [SCOI2005]互不侵犯King [问题描述]在n*n(1<=n<=10)的棋盘上放k(0<=k<=n*n)个国王(可攻击相邻的8 个格子),求使它们无法互相攻击 ...

  5. 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 ...

  6. 【模拟8.11】星空(差分转化,状压DP,最短路)

    一道很好的题,综合很多知识点. 首先复习差分:      将原来的每个点a[i]转化为b[i]=a[i]^a[i+1],(如果是求和形式就是b[i]=a[i+1]-a[i]) 我们发现这样的方便在于我 ...

  7. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

  8. nefu1109 游戏争霸赛(状压dp)

    题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...

  9. poj3311 TSP经典状压dp(Traveling Saleman Problem)

    题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...

随机推荐

  1. js-template-art【一】简述

    一.官方地址 地址:https://github.com/aui/art-template 二.概述 官方对比 三.特性 拥有接近 JavaScript 渲染极限的的性能 调试友好:语法.运行时错误日 ...

  2. oracle连接数据库报错:ORA-01034: ORACLE not available(Oracle 不存在),ORA-27101: shared memory realm does not exist

    花一天半的时间解决客户端连接服务端的oracle数据库,无法连接问题.ORA-01034: ORACLE not available(Oracle 不存在),ORA-27101: shared mem ...

  3. 【转载】Java动态代理之JDK实现和CGlib实现(简单易懂)

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6542259.html 一:代理模式(静态代理) 代理模式是常用设计模式的一种,我们在软件设计时常用的代理一般是 ...

  4. Sublime Text 3图标更改

    Sublime Text 3图标更改 步骤: 1.下载ico图标 2.然后更改图标 注意:重点讲解下,如何将png文件转换为ico图标: 网络上单独找sublime text 3的ico图标比较不好找 ...

  5. Python3:pyecharts数据可视化插件

    Python3:pyecharts数据可视化插件 一.简介 pyecharts 是一个用于生成 Echarts 图表的类库. Echarts 是百度开源的一个数据可视化 JS 库.主要用于数据可视化. ...

  6. linux中readl()和writel()函数---用于读写寄存器

    writel() 往内存映射的 I/O 空间上写数据,wirtel() I/O 上写入 32 位数据 (4字节). 原型: #include <asm/io.h> void writel ...

  7. LM358电流检测电路

    ----------------------------------------------------------------------------------------- ---------- ...

  8. 如何解决Nginx php 50x 错误

    SEO反馈百度爬虫经常504,一般情况下是由nginx默认的fastcgi进程响应慢引起的,但也有其他情况,这里我总结了一些解决办法供大家参考.   方法/步骤 一般50x状态码问题分析: Nginx ...

  9. 枚举子集&高位前缀和

    最近做的题里面有这个东西,于是写一篇博客总结一下吧. 枚举子集 枚举子集就是状压的时候枚举其中的二进制位中的1的子集.直接暴力枚举二进制位时间复杂度是\(O(4^n)\),但是我们可以发现,对于每一位 ...

  10. C# typeof 与GetType()的区别

    C#中Type类的介绍:https://msdn.microsoft.com/zh-cn/library/system.type(VS.80).aspx C#中任何对象都具有GetType()方法,它 ...