ZOJ 3209 Treasure Map (Dancing Links 精确覆盖 )
题意 : 给你一个大小为 n * m 的矩形 , 坐标是( 0 , 0 ) ~ ( n , m ) 。然后给你 p 个小矩形 。 坐标是( x1 , y1 ) ~ ( x2 , y2 ) , 你选择最小的几个矩形 , 使得这些矩形能够覆盖整个矩形 。 而且互相不会重叠 。( n , m <= 30 )
思路 : Dancing Links 的精确覆盖问题 。
我们将 n * m 的矩形分成 n * m 个小正方形 ,那么我们仅仅要保证每一个小正方形被覆盖且仅仅被覆盖一次就可以 。
那么列表示每一个小正方形 。 行表示你选择的矩形 。 假设选择这个矩形能够覆盖某个小正方形 。 则相应的格子是1 , 否则相应的格子是 0
最多有 30 * 30 = 900 列 ,最多有 500 行
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std; const int maxn = 900 + 10 ;
const int maxr = 500 + 10 ;
const int maxnode = 500 * 900 + maxr + 10 ; #define FOR( i , A , s ) for( int i = A[s] ; i != s ; i = A[i] ) struct DLX{
// maxn 列数 , maxnode 总节点数 , maxr 行数
int n , sz ;
int S[maxn] ; int row[maxnode] , col[maxnode] ;
int L[maxnode] , R[maxnode] , U[maxnode] , D[maxnode] ;
int H[maxr] ; int ansd , ans[maxr] ; void init( int N ) {
n = N ;
// 第一行的虚拟结点
for( int i = 0 ; i <= n ; i ++ ) {
U[i] = D[i] = i ;
L[i] = i - 1 ;
R[i] = i + 1 ;
}
R[n] = 0 ; L[0] = n ;
sz = n + 1 ;
// 每一列的个数
memset( S , 0 , sizeof(S) ) ;
// H[i] = -1 表示这一行还没有 1
// 否则表示第一个 1 的 sz 是多少
memset( H , -1 , sizeof(H)) ;
} // 在第r行第c列加入一个1
void Link( int r , int c ) {
row[sz] = r ;
col[sz] = c ;
S[c] ++ ; D[sz] = c ; U[sz] = U[c] ;
D[U[c]] = sz ; U[c] = sz ; if( H[r] < 0 ) { H[r] = L[sz] = R[sz] = sz ; }
else{
R[sz] = H[r] ;
L[sz] = L[H[r]] ;
R[L[sz]] = sz ;
L[R[sz]] = sz ;
} sz ++ ; } // 删除 第 c 列
void remove ( int c ) {
// 删除虚拟结点中的 c 列
L[R[c]] = L[c] ;
R[L[c]] = R[c] ;
// 从 c 列向下遍历
FOR( i , D , c ) {
// 删除遍历到的行
FOR( j , R , i ) {
D[U[j]] = D[j] ;
U[D[j]] = U[j] ;
-- S[col[j]] ;
}
}
} // 恢复第 c 列
void restore( int c ) {
FOR( i , U , c ) {
FOR( j , L , i ) {
++S[col[j]] ;
U[D[j]] = D[U[j]] = j ;
}
}
L[R[c]] = R[L[c]] = c ;
} void dfs( int d ) { // 剪枝
if( d >= best ) {
return ;
} // R[0] = 0 表示找到一个可行解
if( R[0] == 0 ) {
best = d ;
return ;
} // 找到 s 最小的列 , 加快搜索的速度
int c = R[0] ;
FOR( i , R , 0 )
if( S[i] < S[c] ) c = i ; // 删除第 c 列
remove( c ) ; // 遍历选中列中有1的行
FOR( i , D , c ) {
ans[d] = row[i] ;
// 删除选中行中有1的列
FOR( j , R , i ) {
remove( col[j] ) ;
}
dfs( d + 1 ) ;
// 回复删除掉的列
FOR( j , L , i ) {
restore( col[j] ) ;
}
}
restore( c ) ;
} int solve() {
best = INF ;
dfs( 0 ) ;
if( best == INF )
return -1 ;
else
return best ;
}
int best ;
const static int INF = 0x3f3f3f3f ;
} dlx ; int main(){
int cas ;
scanf( "%d" , &cas ) ;
while( cas -- ) {
int n , m , p ;
scanf( "%d%d%d" , &n , &m , &p ) ;
dlx.init( n * m ) ;
for( int i = 1 ; i <= p ; i ++ ) {
int x1 , y1 , x2 , y2 ;
scanf( "%d%d%d%d" , &x1 , &y1 , &x2 , &y2 ) ;
for( int x = x1 ; x < x2 ; x ++ ) {
for( int y = y1 ; y < y2 ; y ++ ) {
dlx.Link( i , x * m + y + 1 ) ;
}
}
}
printf( "%d\n" , dlx.solve() ) ;
}
return 0 ;
}
ZOJ 3209 Treasure Map (Dancing Links 精确覆盖 )的更多相关文章
- ZOJ 3209 Treasure Map (Dancing Links)
Treasure Map Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit S ...
- zoj 3209.Treasure Map(DLX精确覆盖)
直接精确覆盖 开始逐行添加超时了,换成了单点添加 #include <iostream> #include <cstring> #include <cstdio> ...
- ZOJ3209 Treasure Map —— Danc Links 精确覆盖
题目链接:https://vjudge.net/problem/ZOJ-3209 Treasure Map Time Limit: 2 Seconds Memory Limit: 32768 ...
- ZOJ 3209 Treasure Map (Dancing Links)
Treasure Map Time Limit: 2 Seconds Memory Limit: 32768 KB Your boss once had got many copies of ...
- ZOJ 3209 Treasure Map(精确覆盖)
Treasure Map Time Limit: 2 Seconds Memory Limit: 32768 KB Your boss once had got many copies of ...
- (简单) ZOJ 3209 Treasure Map , DLX+精确覆盖。
Description Your boss once had got many copies of a treasure map. Unfortunately, all the copies are ...
- ZOJ 3209 Treasure Map 精确覆盖
题目链接 精确覆盖的模板题, 把每一个格子当成一列就可以. S忘记初始化TLE N次, 哭晕在厕所...... #include<bits/stdc++.h> using namespac ...
- zoj - 3209 - Treasure Map(精确覆盖DLX)
题意:一个 n x m 的矩形(1 <= n, m <= 30),现给出这个矩形中 p 个(1 <= p <= 500)子矩形的左下角与右下角坐标,问最少用多少个子矩形能够恰好 ...
- hihoCoder #1321 : 搜索五•数独 (Dancing Links ,精确覆盖)
hiho一下第102周的题目. 原题地址:http://hihocoder.com/problemset/problem/1321 题意:输入一个9*9数独矩阵,0表示没填的空位,输出这个数独的答案. ...
随机推荐
- 常用css属性总结
边框修饰:border------>top,bottom,left,right上下左右边框 分为:color,类型style{ groove,dashed,ridge,solid}一个值---- ...
- WinRT ListView间隔变色(一)
我们知道,在WPF里,MSDN提供了三种方法 1.使用转换器Converter 2.继承ListView类,自己处理 3.使用StyleSelctor 到了WinRT的世界了 1. Winrt中Set ...
- nginx--提供一键安装脚本
nginx特点 基于进程池实现的fastcgi 单一进程即可实现处理上千的连接 易于扩展的插件系统 安装篇 *下载源码 curl -O http://nginx.org/download/nginx- ...
- Protecting resources in iPhone and iPad apps
源码:https://github.com/lingzhao/EncryptedResourceDemo UPDATE: The example project has been updated to ...
- Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean
WebsocketSourceConfiguration { @Bean ServletWebServerFactory servletWebServerFactory(){ return new T ...
- 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay
[阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...
- Keil uVision “已停止工作”
之前一直受这个问题的困扰,但是因为只是下载程序,下载镜像文件完事就算了.随便keil挂掉. 今天要调试程序,发现开启调试keil就挂掉了,烦. 解决办法参见: http://218.244.144.1 ...
- BZOJ 3277 串 & BZOJ 3473 字符串 (广义后缀自动机、时间复杂度分析、启发式合并、线段树合并、主席树)
标签那么长是因为做法太多了... 题目链接: (bzoj 3277) https://www.lydsy.com/JudgeOnline/problem.php?id=3277 (bzoj 3473) ...
- 利用tempalte.js模版引擎渲染页面,作对应的数据处理
需要启个服务 需引入jquery.js和template.js <!DOCTYPE html> <html lang="en"> <head> ...
- 使用NamedParameterJdbcTemplate
[在JDBC模板中使用具名参数] 1.在经典的JDBC用法中,SQL参数使用占位符?表示,并且受到位置的限制.定为参数的问题在于,一旦参数的顺序发生变化,就必须改变参数绑定. 2.在Spring JD ...