直接精确覆盖

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

#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. touchend事件的preventDefault阻止掉了click事件

    <div id="box">box</div> <script> var isTouchDevice = function() { return ...

  2. 【转】VC的MFC中重绘函数的使用总结(整理)

    原文网址:http://www.cnblogs.com/x8023z/archive/2008/12/09/mfc33.html 在刷新窗口时经常要调用重绘函数MFC提供了三个函数用于窗口重绘Inva ...

  3. 数据结构与算法分析——C语言描述

    P1.1 选择问题,选择出第K大的数,并画出N为不同值的运行时间,K=N/2 毕业两年半,重写排序,感觉良好.代码使用冒泡排序,库函数clock计算大致运行时间. // P1_1.cpp : Defi ...

  4. [转]让程序在崩溃时体面的退出之SEH+Dump文件

    原文地址:http://blog.csdn.net/starlee/article/details/6649605 在我上篇文章<让程序在崩溃时体面的退出之SEH>中讲解了SEH中try/ ...

  5. Asterisk 安装与配置

    如果用来管理 1.4 版本的 Asterisk ,可能会存在未知的问题.通过集成 CentOS . Asterisk 和 FreePBX , Fonality 公司提供了一个完全傻瓜式的 Asteri ...

  6. flexpaper 与js 交互

    flash 代码//写到要响应的方法体中import flash.external.ExternalInterface;ExternalInterface.call("alert" ...

  7. Struct2 向Action中传递参数(中文乱码问题)

    就是把视图上的值传递到Action定义的方法中 也就是把数据从前台传递到后台 三种方式: 1.  使用action属性接收参数 比如jsp页面: <body> 使用action属性接收参数 ...

  8. Leo 搭积木

    Leo 搭积木[问题描述]Leo是一个快乐的火星人,总是能和地球上的 OIers玩得很 high.2012 到了, Leo 又被召回火星了,在火星上没人陪他玩了,但是他有好多好多积木,于是他开始搭积木 ...

  9. winfrom 截屏、抓屏 分类: WinForm 2014-08-01 13:02 198人阅读 评论(0) 收藏

    截取全屏代码: try { this.Hide(); Rectangle bounds = Screen.GetBounds(Screen.GetBounds(Point.Empty)); Bitma ...

  10. jQuery Animation实现CSS3动画

    jQuery Animation的工作原理是通过将元素的CSS样式从一个状态改变为另一个状态.CSS属性值是逐渐改变的,这样就可以创建动画效果.只有数字值可创建动画(比如 "margin:3 ...