zoj 3209.Treasure Map(DLX精确覆盖)
直接精确覆盖
开始逐行添加超时了,换成了单点添加
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <vector>
using namespace std; #define FOR(i,A,s) for(int i = A[s]; i != s; i = A[i])
#define exp 1e-8 const int MAX = ;
int n, m, k, t, len; struct DLX {
int n, Size;//Size为尾指针,真正大小
int row[MAX], col[MAX];//记录每个点的行列
int U[MAX], D[MAX], R[MAX], L[MAX]; //4个链表
int S[MAX],H[MAX];//每列1的个数
int ncnt, ans[MAX];
void init (int n) {
this->n = n;
ncnt = MAX;
//增加n+1个辅助链表,从0到n
for (int i = ; i <= n; i++)
U[i] = D[i] = i, L[i] = i - , R[i] = i + ,S[i]=;
R[n] = , L[] = n; //头尾相接
Size = n + ;
memset (H, -, sizeof H);
}
//单点添加
void Link (int r, int c)
{
++S[col[++Size] = c];
row[Size] = r;
D[Size] = D[c];
U[D[c]] = Size;
U[Size] = c;
D[c] = Size;
if (H[r] < ) H[r] = L[Size] = R[Size] = Size;
else
{
R[Size] = R[H[r]];
L[R[H[r]]] = Size;
L[Size] = H[r];
R[H[r]] = Size;
}
}
void Remove (int c) {
//精确覆盖
L[R[c]] = L[c], R[L[c]] = R[c];
FOR (i, D, c)
FOR (j, R, i)
U[D[j]] = U[j], D[U[j]] = D[j], --S[col[j]];
// //重复覆盖
// for (int i = D[c]; i != c; i = D[i])
// L[R[i]] = L[i], R[L[i]] = R[i];
}
void Restore (int c) {
FOR (i, U, c)
FOR (j, L, i)
++S[col[j]], U[D[j]] = j, D[U[j]] = j;
L[R[c]] = c, R[L[c]] = c;
//重复覆盖
// for (int i = U[c]; i != c; i = U[i])
// L[R[i]] = R[L[i]] = i;
}
bool v[MAX];
int ff() {
int ret = ;
for (int c = R[]; c != ; c = R[c]) v[c] = true;
for (int c = R[]; c != ; c = R[c])
if (v[c])
{
ret++;
v[c] = false;
for (int i = D[c]; i != c; i = D[i])
for (int j = R[i]; j != i; j = R[j])
v[col[j]] = false;
}
return ret;
}
bool dfs (int d) {
if (d >= ncnt) return ;
//if (d + ff() > k) return 0;//重复覆盖
if (R[] == ) {
ncnt = min (ncnt, d);
return ;
}
int c = R[];
for (int i = R[]; i != ; i = R[i])
if (S[i] < S[c])
c = i;
Remove (c);//精确覆盖
FOR (i, D, c) {
//Remove (i);//重复覆盖
ans[d] = row[i];
FOR (j, R, i) Remove (col[j]);//精确覆盖
//FOR (j, R, i) Remove (j);//重复覆盖
//if (dfs (d + 1) ) return 1;
dfs (d + );
FOR (j, L, i) Restore (col[j]);//精确覆盖
//FOR (j, L, i) Restore (j);//重复覆盖
//Restore (i);//重复覆盖
}
Restore (c);//精确覆盖
return ;
}
bool solve (vector<int> &v) {
v.clear();
if (!dfs () ) return ;
for (int i = ; i < ncnt; i++) v.push_back (ans[i]);
return ;
}
} Dance;
int columns[][ * ];
int main() {
scanf ("%d", &t);
while (t--) {
memset (columns, , sizeof columns);
scanf ("%d %d %d", &n, &m, &k);
len = n * m;
Dance.init (len);
int x,y,xx,yy;
for (int p = ; p <= k; p++) {
scanf ("%d %d %d %d", &x, &y, &xx, &yy);
for (int i = x+; i <= xx; i++)
for (int j = y+; j <= yy; j++)
Dance.Link (p, j + (i-)*m);
}
Dance.dfs ();
printf ("%d\n", Dance.ncnt == MAX ? - : Dance.ncnt);
}
return ;
}
zoj 3209.Treasure Map(DLX精确覆盖)的更多相关文章
- (简单) 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(精确覆盖DLX)
题意:一个 n x m 的矩形(1 <= n, m <= 30),现给出这个矩形中 p 个(1 <= p <= 500)子矩形的左下角与右下角坐标,问最少用多少个子矩形能够恰好 ...
- 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
用最少的矩阵覆盖n*m的地图.注意矩阵不能互相覆盖. 这里显然是一个精确覆盖,但因为矩阵拼接过程中,有公共的边,这里须要的技巧就是把矩阵的左边和以下截去一个单位. #include <stdio ...
- ZOJ 3209 Treasure Map 精确覆盖
题目链接 精确覆盖的模板题, 把每一个格子当成一列就可以. S忘记初始化TLE N次, 哭晕在厕所...... #include<bits/stdc++.h> using namespac ...
- ZOJ 3209 Treasure Map (Dancing Links 精确覆盖 )
题意 : 给你一个大小为 n * m 的矩形 , 坐标是( 0 , 0 ) ~ ( n , m ) .然后给你 p 个小矩形 . 坐标是( x1 , y1 ) ~ ( x2 , y2 ) , 你选 ...
- 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 (Dancing Links)
Treasure Map Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit S ...
- DLX精确覆盖与重复覆盖模板题
hihoCoder #1317 : 搜索四·跳舞链 原题地址:http://hihocoder.com/problemset/problem/1317 时间限制:10000ms 单点时限:1000ms ...
随机推荐
- Delphi 调试WEBService程序(ISAPI或CGI) 把Web App Debugger executable转换成 ISAPI/NSAPI
1.新建一个web工程,请选中最下面一项:Web App Debugger executable,Coclass name我们设为demo1: 2.在弹出的WebModule2中右击,在弹出的Ac ...
- attitude
刚看到一段挺有趣的游戏,分享一下. 如果 令 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 分别等于 1 2 3 4 5 6 7 8 9 10 ...
- .net code injection
.NET Internals and Code Injection http://www.ntcore.com/files/netint_injection.htm Windows Hooks in ...
- SRM 440(1-250pt, 1-500pt)
DIV1 250pt 题意:小球从一段折线斜坡上滚下来,告诉所用时间,求重力加速度. 解法:二分答案模拟即可. tag:二分,simulation // BEGIN CUT HERE /* * Aut ...
- lightoj1057 - Collecting Gold (tsp问题)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1057 题目大意:在二维矩阵中,给你一个起点和至多15个的目标点.要你求出从起点出发经过 ...
- jquery 使用方式记录
1.监听键盘事件 $("#id").keyup(function(event){ if(event.keyCode == 13){ $("#btn_addgoods_su ...
- Struct.xml Action配置
<package name="default" namespace="/" extends="struts-default"> ...
- ADB server didn't ACK的问题
今天出现eclipse用手机调试时,一直起不来,出现ADB server didn't ACK,提示restart adb或者重启eclipse,按照原来的,查看了任务管理器中,没发现已经启动的adb ...
- c#委托和事件(下) 分类: C# 2015-03-09 08:42 211人阅读 评论(0) 收藏
C#中的委托和事件(下) 引言 如果你看过了 C#中的委托和事件 一文,我想你对委托和事件已经有了一个基本的认识.但那些远不是委托和事件的全部内容,还有很多的地方没有涉及.本文将讨论委托和事件一些更为 ...
- Unity3D 制作右上角小地图
一个简单的方法, 首先先在俯视图视角截取一张图片,用作小地图的背景图片.然后新建一个Plane,把截图附到Plane上,然后把Plane与刚才截图的场景的相应位置重合,要尽量重合,当做地图.(见 ...