ACM: 搜索题 poj 1691 一觉醒来居…
2016-05-19 23:20
323 查看
Painting A Board
Description
The CE digital
company has built an Automatic Painting Machine (APM) to paint a
flat board fully covered by adjacent non-overlapping rectangles of
different sizes each with a predefined color.
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
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
The first line of
the input file contains an integer M which is the number of test
cases to solve (1 <= M <= 10). For
each test case, the first line contains an integer N, the number of
rectangles, followed by N lines describing the rectangles. Each
rectangle R is specified by 5 integers in one line: the y and x
coordinates of the upper left corner of R, the y and x coordinates
of the lower right corner of R, followed by the color-code of
R.
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
One line for each
test case showing the minimum number of brush pick-ups.
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. 染色问题. 先将图建立起来. 将当前小矩阵编号为i, 与其相邻的或则在上面的矩阵链接起来.(做标记)
2. 深搜解决. dfs(int reLen,int sum,int color)
reLen: 小矩阵已经染色的数目, sum: 当前使用画刷的次数. color: 当前画刷的颜色.
代码:
#include
<cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 20
const int INF = (1<<30);
struct node
{
int x1,
y1;
int x2,
y2;
int
color;
}rec[MAX];
int n;
bool vis[MAX], map[MAX][MAX];
int deg[MAX];
int result;
void read_graph()
{
for(int i =
0; i < n; ++i)
{
for(int j =
0; j < n; ++j)
{
if(rec[i].y2
== rec[j].y1 && (rec[j].x1
<= rec[i].x2 &&
rec[i].x1 <= rec[j].x2))
{
deg[j]++;
//第j个小矩阵有多少个前躯.
map[i][j] =
true;
}
}
}
}
void dfs(int reLen,int sum,int color)
{
if(sum
> result) return ;
if(reLen ==
n)
{
result =
sum;
return
;
}
for(int i =
0; i < n; ++i)
{
if(!vis[i]
&& deg[i] == 0)
//每次要从当前没有前躯的小矩阵开始染色.
{
vis[i] =
true;
for(int j =
0; j < n; ++j)
{
if(map[i][j]) //把小矩阵i的后继全部删除.
deg[j]--;
}
if(rec[i].color == color)
dfs(reLen+1,sum,color);
else
dfs(reLen+1,sum+1,rec[i].color);
相关文章推荐
- ACM: 动态规划 黑书上的题目
- ACM: 动态规划 华东师范OJ 1244 …
- ACM: 动态规划 poj 2192
- ACM: 变形的最大公共自序列 动态规…
- ACM: 暴力题 poj 1338
- ACM: 动态规划 poj1695
- ACM: poj 1050
- ACM: 动态规划 poj 1887
- ACM: 动态规划 poj 1579
- ACM: 动态规划 poj 1088
- ACM: 动态规划 poj 1390
- ACM: 动态规划题 poj 1191 Nothing…
- 动态规划初步进阶(一)
- 解决Warning:Ignoring InnerClasses attribute for an anonymous inner class associated EnclosingMethod at
- ACM: 动态规划 poj 1160 终于有点…
- java-ThreadLocal是解决线程安全问题
- 给未来的你 — 李开复在2011…
- ACM: 动态规划 poj 1692
- ACM: 动态规划 poj 1190
- 虚拟机下安装ubuntu后root密码设置