POJ 1691 Painting a Board(状态压缩DP)
Description

To color the board, the APM has access to a set of brushes. Each brush has a distinct color C. The APM picks one brush with color C and paints all possible rectangles having predefined color C with the following restrictions:
To avoid leaking the paints and mixing colors, a rectangle can only be painted if all rectangles immediately above it have already been painted. For example rectangle labeled F in Figure 1 is painted only after rectangles C and D are painted. Note that each rectangle must be painted at once, i.e. partial painting of one rectangle is not allowed.
You are to write a program for APM to paint a given board so that the number of brush pick-ups is minimum. Notice that if one brush is picked up more than once, all pick-ups are counted.
Input
Note that:
- Color-code is an integer in the range of 1 .. 20.
- Upper left corner of the board coordinates is always (0,0).
- Coordinates are in the range of 0 .. 99.
- N is in the range of 1..15.
Output
Sample Input
1
7
0 0 2 2 1
0 2 1 6 2
2 0 4 2 1
1 2 4 4 2
1 4 3 6 1
4 0 6 4 1
3 4 6 6 2
Sample Output
3
思路:
1. 拓扑排序加深搜
2. 拓扑排序加广搜
3. 状态压缩DP. 设 dp[s][i] 表示当前状态为 s 时, 刚画完第 i 个矩形所用的最少画笔数. s = [1, 1<<15), s 的二进制表示中, 第 i 位 为1 表示第 i 个矩形已经被涂完色.
dp[news][i] = min(dp[news][i], dp[olds][j]+1) if color[i] != color[j]
= min(dp[olds][j]) if color[i] == color[j]
其中, news = (olds | 1<<i)
上述状态转移方程的意思是, 要计算 dp[s][i] 的值, 那么考虑当前所有 dp[olds][j], 其中 s = (olds|1<<i), 假如 j 的颜色和 i 的颜色相同, 这不需要另拿画笔, 否则, 画笔数加 1
当然, 对 i 进行涂色需要满足 i 的直接前驱都已被涂完
总结:
1. 这道题近似于暴力破解, 枚举所有状态集合的所有状态, 在某个状态 s 下, 以 s 中以涂色的某个矩形为支点来更新一个还未被涂色的点
2. 第 48 行代码错过一次, 把 i 写成了 j
3. 第 45 行很精髓, 我本打算用一个 for 循环进行判断的
4. 第 44, 48 行, 体现了 (1) 的思想, 即以 k 为支点来更新 i
代码:
#include <iostream>
using namespace std; class tangle {
public:
int x1, y1, x2, y2;
int color;
tangle(int _x1, int _y1, int _x2, int _y2):x1(_x1), y1(_y1), x2(_x2), y2(_y2) {}
tangle() {
tangle(0, 0, 0, 0);
}
};
const int INF = 0X3F3F3F3F;
int M, N;
tangle tangles[20];
int dp[1<<15][20];
int up[20]; bool isUpper(int i, int j) {
if(tangles[i].x2 != tangles[j].x1) return false;
if(tangles[i].y1 >= tangles[j].y2) return false;
if(tangles[i].y2 <= tangles[j].y1) return false;
return true;
}
void pre_process() {
memset(up, 0, sizeof(up));
for(int i = 1; i <= N; i ++) {
for(int j = 1; j <= N; j ++) {
if(isUpper(i, j))
up[j] = (up[j]|(1<<(i-1)));
}
} memset(dp, 0x3f, sizeof(dp));
for(int i = 1; i <= N; i ++)
if(up[i] == 0)
dp[1<<(i-1)][i] = 1; }
int mainFunc() {
int END = (1<<N) -1;
for(int s = 1; s <= END; s ++) { // 从状态 s 导出 dp[s][i], 当前 s 第 i 个矩形不能被涂色
for(int i = 1; i <= N; i ++) { // 将要给第 i 个矩形涂色
if(s&(1<<(i-1)) ) continue; // 状态 s 中, 对应第 i 个矩形已经被涂完了
if((s&up[i]) != up[i]) continue; // 确保 i 的直接前驱都已涂完颜色
for(int k = 1; k <= N; k ++) {
if(!(s&(1<<(k-1)))) continue;
int news = (s|1<<(i-1)); // update 新的 dp[][]
if(tangles[i].color != tangles[k].color)
dp[news][i] = min(dp[news][i], dp[s][k]+1);
else
dp[news][i] = min(dp[news][i], dp[s][k]);
}
}
}
int ans = INF;
for(int i = 1; i <= N; i ++) {
ans = min(ans, dp[END][i]);
}
return ans;
}
int main() {
freopen("E:\\Copy\\ACM\\poj\\1691\\in.txt", "r", stdin);
cin >> M;
while( M -- >= 1) {
cin >> N;
for(int i = 1; i <= N; i ++) {
scanf("%d%d%d%d%d", &tangles[i].x1, &tangles[i].y1, &tangles[i].x2, &tangles[i].y2, &tangles[i].color);
}
pre_process();
cout << mainFunc() << endl;
}
return 0;
}
POJ 1691 Painting a Board(状态压缩DP)的更多相关文章
- poj 3311 floyd+dfs或状态压缩dp 两种方法
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6436 Accepted: 3470 ...
- POJ 2686_Traveling by Stagecoach【状态压缩DP】
题意: 一共有m个城市,城市之间有双向路连接,一个人有n张马车票,一张马车票只能走一条路,走一条路的时间为这条路的长度除以使用的马车票上规定的马车数,问这个人从a出发到b最少使用时间. 分析: 状态压 ...
- poj 2411 Mondriaan's Dream_状态压缩dp
题意:给我们1*2的骨牌,问我们一个n*m的棋盘有多少种放满的方案. 思路: 状态压缩不懂看,http://blog.csdn.net/neng18/article/details/18425765 ...
- poj 1185 炮兵阵地 [经典状态压缩DP]
题意:略. 思路:由于每个大炮射程为2,所以如果对每一行状态压缩的话,能对它造成影响的就是上面的两行. 这里用dp[row][state1][state2]表示第row行状态为state2,第row- ...
- POJ 1038 Bug Integrated Inc(状态压缩DP)
Description Bugs Integrated, Inc. is a major manufacturer of advanced memory chips. They are launchi ...
- poj 2411 Mondriaan's Dream(状态压缩dP)
题目:http://poj.org/problem?id=2411 Input The input contains several test cases. Each test case is mad ...
- poj 2686 Traveling by Stagecoach ---状态压缩DP
题意:给出一个简单带权无向图和起止点,以及若干张马车车票,每张车票可以雇到相应数量的马. 点 u, v 间有边时,从 u 到 v 或从 v 到 u 必须用且仅用一张车票,花费的时间为 w(u, v) ...
- POJ 1185 炮兵阵地(状态压缩DP)
题解:nState为状态数,state数组为可能的状态 代码: #include <map> #include <set> #include <list> #inc ...
- POJ 3254 Corn Fields(状态压缩DP)
题目大意:给出一个M*N的矩阵,元素为0表示这个地方不能种玉米,为1表示这个地方能种玉米,现在规定所种的玉米不能相邻,即每行或者没列不能有相邻的玉米,问一共有多少种种植方法. 举个例子: 2 3 1 ...
随机推荐
- [转]手工实现RTTI
运行时的动态类型检查(RTTI,Run Time Type Indentifiation)是c++中提供的一项语言机制,它主要用于判断多态对象的具体类型. 为什么不使用c++提供的RTTI功能 但c ...
- vue2.0的常用功能简介
路由跳转 当我们想要实现点击链接跳转时,可以使用$router来进行跳转 语法如下: '}}) 这里path是要跳转的路径,query里面是路径跳转时要携带的参数,以对象的形式存在 2 获取路由参数 ...
- Ribbon对于SocketTimeOutException重试的坑以及重试代码解析
背景 本文基于Spring-Cloud, Daltson SR4 微服务一般多实例部署,在发布的时候,我们要做到无感知发布:微服务调用总会通过Ribbon,同时里面会实现一些重试的机制,相关配置是: ...
- 在VC中向数据库提交SLQ语句
1.通过链接对象执行SQL语句 mfc没有提供对ado的操作类,它对ado的操作是通过智能指针来完成,要创建ado连接对象,需要像下面代码一样先定义一个连接对象的智能指针: _ConnectionPt ...
- 略过ftp利用samba共享
开发阶段每次通过ftp上传代码到服务器是件麻烦事,而且有时候一些小错误直接通过ftp在服务器上修改后,容易忘记在本地保存. 于是想到了网络共享,windows上直接挂载网络硬盘,这样就可以略过ftp了 ...
- ecshop和ucenter的整合
按照网上的教材,一直提示数据库.密码错误,开始怀疑代码错了,毕竟都是两个老古董. 于是开始调试,居然调试也不能很好的支持,点击下一步后就卡死了,好吧,只好用log大法了, error_log(prin ...
- 【WPF】用代码给集合(Collection)容器动态添加子元素(Item)
需求:如何向 TabControl 中添加选项卡项. 问题:做的TabControl分页栏想要通过代码来控制添加的子元素.同理可以将解决思路拓展到用于其他的集合控件添加子元素的问题. 在布局文件She ...
- size_t ssize_t socklen_t
size_t 解释一:为了增强程序的可移植性,便有了size_t,它是为了方便系统之间的移植而定义的,不同的系统上,定义size_t可能不一样. 在32位系统上 定义为 unsigned int 也就 ...
- 【高可用HA】Apache (3) —— Mac下配置Apache Httpd负载均衡(Load Balancer)之mod_proxy
Mac下配置Apache Httpd负载均衡(Load Balancer)之mod_proxy httpd版本: httpd-2.4.17 参考来源: Apache (1) -- Mac下安装Apac ...
- spring配置事务 元素 "tx:annotation-driven" 的前缀 "tx" 未绑定
在进行spring与mybatis整合时,启动项目报错,控制台提示“元素 "tx:annotation-driven" 的前缀 "tx" 未绑定”. 经过查找, ...