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 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
随机推荐
- tow sum
今天面试好打脸!!! 解决方案方法一:暴力法暴力法很简单.遍历每个元素 xx,并查找是否存在一个值与 target−x 相等的目标元素. public int[] twoSum(int[] nums, ...
- Python开发【Tornado】:简介与使用
Tornado框架 简介: Tornado是使用Python编写的一个强大的.可扩展的Web服务器.它在处理严峻的网络流量时表现得足够强健,但却在创建和编写时有着足够的轻量级,并能够被用在大量的应用和 ...
- mysql 数据操作 单表查询 where约束 练习
create table employee( id int not null unique auto_increment, name ) not null, sex enum('male','fema ...
- loadNibNamed:(NSString *)name owner:(nullable id)owner options:(nullable NSDictionary *)options用法
1.name xib的名字 owner当前类对象 options初始参数 实际应用: NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@&quo ...
- PAT 1114 Family Property[并查集][难]
1114 Family Property(25 分) This time, you are supposed to help us collect the data for family-owned ...
- HTML5游戏开发系列教程10(译)
原文地址:http://www.script-tutorials.com/html5-game-development-lesson-10/ 最后我们将继续使用canvas来进行HTML5游戏开发系列 ...
- 5.8 Components — Composing Components(组合组件)
一.概述 当你通过和另外一个组件组合的时候,组件就会真正发挥它们的所有潜能.比如<ul>元素,只有<li>元素是适合作为它的子元素的.如果我们希望同样类型的行为,那么我们就必须 ...
- 2018-2019 ICPC, NEERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams Preferred) Solution
A. Alice the Fan Solved. 题意: 两个人打网球,要求teamA 的得分与其他队伍拉开尽量大 输出合法的方案 思路: $dp[i][j][k][l] 表示 A 赢i局,其他队伍赢 ...
- Java实现使用位图生成真值组合
摘要: 使用位图生成真值组合. 难度: 初级. /** * 问题描述: 给定 n 个布尔变量,打印所有真值组合. * 例如, n = 2 时 , 所有真值组合为 (true, false),(tr ...
- mysql 触发器 trigger用法 one (简单的)
实例~~ example1: 创建表tab1 1 2 3 4 DROP TABLE IF EXISTS tab1; CREATE TABLE tab1( tab1_id varchar(11) ...