直接精确覆盖

开始逐行添加超时了,换成了单点添加

#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精确覆盖)的更多相关文章

  1. (简单) ZOJ 3209 Treasure Map , DLX+精确覆盖。

    Description Your boss once had got many copies of a treasure map. Unfortunately, all the copies are ...

  2. zoj - 3209 - Treasure Map(精确覆盖DLX)

    题意:一个 n x m 的矩形(1 <= n, m <= 30),现给出这个矩形中 p 个(1 <= p <= 500)子矩形的左下角与右下角坐标,问最少用多少个子矩形能够恰好 ...

  3. ZOJ 3209 Treasure Map(精确覆盖)

    Treasure Map Time Limit: 2 Seconds      Memory Limit: 32768 KB Your boss once had got many copies of ...

  4. ZOJ 3209 Treasure Map DLX

    用最少的矩阵覆盖n*m的地图.注意矩阵不能互相覆盖. 这里显然是一个精确覆盖,但因为矩阵拼接过程中,有公共的边,这里须要的技巧就是把矩阵的左边和以下截去一个单位. #include <stdio ...

  5. ZOJ 3209 Treasure Map 精确覆盖

    题目链接 精确覆盖的模板题, 把每一个格子当成一列就可以. S忘记初始化TLE N次, 哭晕在厕所...... #include<bits/stdc++.h> using namespac ...

  6. ZOJ 3209 Treasure Map (Dancing Links 精确覆盖 )

    题意 :  给你一个大小为 n * m 的矩形 , 坐标是( 0 , 0 ) ~ ( n , m )  .然后给你 p 个小矩形 . 坐标是( x1 , y1 ) ~ ( x2 , y2 ) , 你选 ...

  7. ZOJ 3209 Treasure Map (Dancing Links)

    Treasure Map Time Limit: 2 Seconds      Memory Limit: 32768 KB Your boss once had got many copies of ...

  8. ZOJ 3209 Treasure Map (Dancing Links)

    Treasure Map Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit S ...

  9. DLX精确覆盖与重复覆盖模板题

    hihoCoder #1317 : 搜索四·跳舞链 原题地址:http://hihocoder.com/problemset/problem/1317 时间限制:10000ms 单点时限:1000ms ...

随机推荐

  1. 【转】Linux下svn的常用工作流程

    原文网址:http://www.cnblogs.com/cobbliu/archive/2011/07/08/2389011.html 上篇文章在ubuntu和redhat5.5上搭建好了svnser ...

  2. Delphi实现WebService带身份认证的数据传输

    WebService使得不同开发工具开发出来的程序可以在网络连通的环境下相互通信,它最大的特点就是标准化(基于XML的一系列标准)带来的跨平台.跨开发工具的通用性,基于HTTP带来的畅通无阻的能力(跨 ...

  3. C++下写的MD5类,简单易用

    //--------------------------------------------------------------------------- /////cpp文件 #pragma hdr ...

  4. 【转】265行JavaScript代码的第一人称3D H5游戏Demo

    译文:http://blog.jobbole.com/70956/ 原文:http://www.playfuljs.com/a-first-person-engine-in-265-lines/ 这是 ...

  5. XML文档部署到Tomcat服务器上总是加载出错

    config.xnl 起初文档路径是在src/Dao/config.xml 在Dao目录下BaseDao类中,解析config.xml文件路径 path="/Dao/config.xml&q ...

  6. Java GC 专家系列3:GC调优实践

    本篇是”GC专家系列“的第三篇.在第一篇理解Java垃圾回收中我们学习了几种不同的GC算法的处理过程,GC的工作方式,新生代与老年代的区别.所以,你应该已经了解了JDK 7中的5种GC类型,以及每种G ...

  7. Conversion between json and object using SBJson lib

    Define two methods in an object class as follows: @interface MyObject : NSObject @property (nonatomi ...

  8. Gwt 整合FusionCharts及封装搜狗地图时出现的问题

    smartGwt 整合FusionCharts 把需要的.swf文件和FusionCharts.js放在war下面(路径就自己定了) 可以工程的html文件中引FusionCharts.js文件 也可 ...

  9. spring mvc DispatcherServlet详解之interceptor和filter的区别

    首先我们看一下spring mvc Interceptor的功能及实现: http://wenku.baidu.com/link?url=Mw3GaUhCRMhUFjU8iIDhObQpDcbmmRy ...

  10. ios中的界面跳转方式

    ios中,两种界面跳转方式 1.NavgationController本身可以作为普通ViewController的容器,它有装Controller的栈,所以可以push和pop它们,实现你所说的跳转 ...