题目大意

要求你在N*M大小的主板上嵌入2*3大小的芯片,不能够在损坏的格子放置,问最多能够嵌入多少块芯片?

题解

妈蛋,这道题折腾了好久,黑书上的讲解看了好几遍才稍微有点眉目(智商捉急),接着看了网上大牛的解题报告和实现代码才弄明白怎么用三进制来进行状态压缩,关键就是理解能够横着放置和竖着放置的条件。由于竖着放置会受到前面两行的影响,这样我们就可以用三进制来表示前面两行的状态了,然后根据前面两行的状态我们也可以得到当前行与前一行的初始状态,之后再根据两个的状态进行放置砖块~~~~具体怎么样的看黑书吧

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 15
int dp[2][60000],pre[MAXN],now[MAXN];
int mp[MAXN*10+5][MAXN];
int s[MAXN],n,m;
int To_ten(int *a)
{
int ret=0;
for(int i=1; i<=m; i++)
ret+=a[i]*s[i];
return ret;
}
void To_three(int *a,int x)
{
for(int i=1; i<=m; i++)
{
a[i]=x%3;
x/=3;
}
}
void dfs(int sum,int x,int y,int status)
{
dp[x][status]=max(dp[x][status],sum);
if(y>=m) return;
if(!pre[y]&&!pre[y+1]&&!now[y]&&!now[y+1])
{
now[y]=2,now[y+1]=2;
int st=To_ten(now);
dfs(sum+1,x,y+2,st);
now[y]=0,now[y+1]=0;
}
if((y+2<=m)&&!now[y]&&!now[y+1]&&!now[y+2])
{
now[y]=2,now[y+1]=2,now[y+2]=2;
int st=To_ten(now);
dfs(sum+1,x,y+3,st);
now[y]=0,now[y+1]=0,now[y+2]=0;
}
dfs(sum,x,y+1,status);
}
int main()
{
int T;
s[0]=0,s[1]=1;
for(int i=2; i<=12; i++)
s[i]=s[i-1]*3;
scanf("%d",&T);
while(T--)
{
int k;
scanf("%d%d%d",&n,&m,&k);
memset(dp,-1,sizeof(dp));
memset(mp,0,sizeof(mp));
while(k--)
{
int x,y;
scanf("%d%d",&x,&y);
mp[x][y]=1;
}
for(int i=1; i<=m; i++)
pre[i]=mp[1][i]+1;
int temp=To_ten(pre);
dp[0][temp]=0;
for(int i=2; i<=n; i++)
{
memset(dp[(i+1)&1],-1,sizeof(dp[(i+1)&1]));
for(int j=0; j<s[m+1]; j++)
if(dp[i&1][j]!=-1)
{
To_three(pre,j);
for(int p=1; p<=m; p++)
if(mp[i][p])
now[p]=2;
else
now[p]=max(0,pre[p]-1);
temp=To_ten(now);
dfs(dp[i&1][j],(i+1)&1,1,temp);
}
}
int ans=0;
for(int i=0; i<s[m+1]; i++)
ans=max(ans,dp[(n+1)&1][i]);
printf("%d\n",ans);
}
return 0;
}

POJ1038 - Bugs Integrated, Inc.(状态压缩DP)的更多相关文章

  1. POJ 1038 Bug Integrated Inc(状态压缩DP)

    Description Bugs Integrated, Inc. is a major manufacturer of advanced memory chips. They are launchi ...

  2. POJ1038 Bugs Integrated, Inc 状压DP+优化

    (1) 最简单的4^10*N的枚举(理论上20%) (2) 优化优化200^3*N的枚举(理论上至少50%) (3) Dfs优化状压dp O(我不知道,反正过不了,需要再优化)(理论上80%) (4) ...

  3. poj1038 Bugs Integrated,Inc. (状压dp)

    题意:N*M的矩阵,矩阵中有一些坏格子,要在好格子里铺2*3或3*2的地砖,问最多能铺多少个. 我的方法好像和网上流传的方法不太一样...不管了.... 由数据范围很容易想到状压dp 我们设某个状态的 ...

  4. POJ1038 Bugs Integrated, Inc.

    题目来源:http://poj.org/problem?id=1038 题目大意: 有一家芯片公司要在一块N*M的板子上嵌入芯片,其中1<=N<=150, 1<=M<=10,但 ...

  5. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  6. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

  7. [知识点]状态压缩DP

    // 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...

  8. HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP

    题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...

  9. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

随机推荐

  1. Avro RPC 之 Protocol 定义和代码生成

    摘自http://avro.apache.org/docs/current/spec.html#Protocol+Declaration,1.7.6版 Protocol Declaration Avr ...

  2. UNITY3D使用NGUI制作自适应UI的总结

    原地址:http://www.cnitblog.com/updraft/archive/2013/11/12/88801.html 制作自适应的几个方法1. 使用 UIROOT 里设置自定义高度的方法 ...

  3. C++ DLL 模板 .

    C++ DLL 模板 1.使用VS2005创建Win32 DLL项目,选择空项目,然后加入CppDll.h和CppDll.cpp文件. 2.修改CppDll.h和CppDll.cpp文件使之成为需要的 ...

  4. [译]C++书籍终极推荐

    转载声明: 翻译仅以技术学习和交流为目的,如需转载请务必标明原帖链接. 来源:http://stackoverflow.com/questions/388242/the-definitive-c-bo ...

  5. [itint5]两数积全为1

    http://www.itint5.com/oj/#18 这一题,首先如果直接去算的话,很容易就超出int或者long的表示范围了.那么要利用%的性质,(num * 10 + 1) % a = 10 ...

  6. php Ajax 局部刷新

    php Ajax 局部刷新: HTML部分 </head> <body> <h1>Ajax动态显示时间</h1> <input type=&quo ...

  7. SRM 585 DIV1 L2

    记录dp(i, j)表示前i种卡片的排列,使得LISNumber为j的方法数. #include <iostream> #include <vector> #include & ...

  8. XST综合、实现过程包含哪些步骤

    2013-06-25 18:53:50 在ISE的主界面的处理子窗口的synthesis的工具可以完成下面的任务: 查看RTL原理图(View RTL schematic) 查看技术原理图(View ...

  9. Android 之 内存管理-查看内存泄露(三)

    概述 在android的开发中,要时刻主要内存的分配和垃圾回收,因为系统为每一个dalvik虚拟机分配的内存是有限的,在google的G1中,分配的最大堆大小只有16M,后来的机器一般都为24M,实在 ...

  10. string evaluated instead to freemarker.template.SimpleScalar

    [2015-09-06 09:07:32.879] ERROR [6B68DD09CE6FECFE20936CA3C6D560AD:http-bio-8087-exec-8] o.a.s.v.free ...