用最少的矩阵覆盖n*m的地图。注意矩阵不能互相覆盖。

这里显然是一个精确覆盖,但因为矩阵拼接过程中,有公共的边,这里须要的技巧就是把矩阵的左边和以下截去一个单位。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
using namespace std;
const int maxnode = 550005;
const int MaxM = 999;
const int MaxN = 555;
int K,n,m;
void print(int x)
{
printf("(%d %d)\n",x%(n+1)==0? n:(x%(n+1)-1),x%(n+1)==0? x/(n+1)-1:x/(n+1));
}
struct DLX
{
int n,m,size;
int U[maxnode],D[maxnode],R[maxnode],L[maxnode],Row[maxnode],Col[maxnode];
int H[MaxN],S[MaxM];
int ands,ans[MaxN],ANS;
void init(int _n,int _m)
{
ANS=0x3f3f3f3f;
n = _n;
m = _m;
for(int i = 0; i <= m; i++)
{
S[i] = 0;
U[i] = D[i] = i;
L[i] = i-1;
R[i] = i+1;
}
R[m] = 0;
L[0] = m;
size = m;
for(int i = 1; i <= n; i++)
H[i] = -1;
}
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] < 0)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)
{
int i,j;
L[R[c]]=L[c];
R[L[c]]=R[c];
for(i=D[c]; i!=c; i=D[i])
{
for(j=R[i]; j!=i; j=R[j])
{
U[D[j]]=U[j],D[U[j]]=D[j];
S[Col[j]]--;
}
}
}
void resume(int c)
{
int i,j;
for(i=U[c]; i!=c; i=U[i])
{
for(j=L[i]; j!=i; j=L[j])
{
U[D[j]]=j;
D[U[j]]=j;
S[Col[j]]++;
}
}
L[R[c]]=c;
R[L[c]]=c;
}
bool v[maxnode];
int f()
{
int ret = 0;
for(int c = R[0]; c != 0; c = R[c])v[c] = true;
for(int c = R[0]; c != 0; 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;
}
void Dance(int d)
{
if(d+f()>=ANS) return;
if(R[0] == 0)
{
ANS=min(ANS,d);
return;
}
int c = R[0];
for(int i = R[0]; i != 0; i = R[i])
if(S[i] < S[c])
c = i;
remove(c);
for(int i = D[c]; i != c; i = D[i])
{
for(int j = R[i]; j != i; j = R[j])remove(Col[j]);
Dance(d+1);
for(int j = L[i]; j != i; j = L[j])resume(Col[j]);
}
resume(c);
}
} g;
int main()
{
int cas,x1,y1,x2,y2,k;
scanf("%d",&cas);
while(cas--)
{
scanf("%d%d%d",&n,&m,&k);
g.init(k,n*m);
for(int q=1; q<=k; q++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
for(int i = x1+1; i <= x2; i++)
for(int j = y1+1; j <= y2; j++)
g.Link(q,j+(i-1)*m);
}
g.Dance(0);
if(g.ANS==0x3f3f3f3f) g.ANS=-1;
printf("%d\n",g.ANS);
}
return 0;
}

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 (Dancing Links)

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

  3. ZOJ 3209 Treasure Map (Dancing Links)

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

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

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

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

    直接精确覆盖 开始逐行添加超时了,换成了单点添加 #include <iostream> #include <cstring> #include <cstdio> ...

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

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

  7. ZOJ 3209 Treasure Map 精确覆盖

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

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

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

  9. ZOJ3209 Treasure Map —— Danc Links 精确覆盖

    题目链接:https://vjudge.net/problem/ZOJ-3209 Treasure Map Time Limit: 2 Seconds      Memory Limit: 32768 ...

随机推荐

  1. OC文件操作、获取文件属性

    #import <Foundation/Foundation.h> //获取文件的属性 int main(int argc, const char * argv[]) { @autorel ...

  2. Codeforces Beta Round #16 E. Fish (状压dp)(概率dp)

    Codeforces Beta Round #16 (Div. 2 Only) E. Fish 题目链接:## 点击打开链接 题意: 有 \(n\) 条鱼,每两条鱼相遇都会有其中一只吃掉对方,现在给你 ...

  3. Android EditText回车不换行

    有时候我们需要在EditText组件输入完直接点回车进入 回车点击事件中处理相关业务,总是换行很伤脑筋,索性网上搜了一个办法,很实用,在这里记下来. 首先获取组件id: edt_city= (Edit ...

  4. LM4990音频功放芯片

    我们选用的一种封装:我们用的是DGK封装. 典型电路图: 下面是示意图:四中封装的示意图是不一样的: 下面是真正的原理图: 高放大倍数的原理图: 查分式的: 单个输入的原理图: 下面是有关电源的选择:

  5. ejs模板引擎的使用

    引入ejs.min.js 创建模板,以<%=jsCode%>包裹起来其余的html和html结构一样 focusTemplateData是模板使用的数据,使用$.each()方法遍历绑定数 ...

  6. FZU Problem 2156 Climb Stairs DP

    http://acm.fzu.edu.cn/problem.php?pid=2156 题目大意: 爬楼梯,要爬到n这个位置,每次可以走x也可以走y,然后一定要经过A和B两点,求最终到达n的方案数. 思 ...

  7. jQuery知识点汇总

    $(this) 当前 HTML 元素 $("p") 所有 <p> 元素 $("p.intro") 所有 class="intro" ...

  8. macos中xcode使用opencv,错误总结

    使用opencv时,进行编译时,会出现如上图所示错误,按图示更改就行了(我编译出来的有2处)主要参考 https://blog.csdn.net/fk1174/article/details/7011 ...

  9. 【hdu 1403】Longest Common Substring

    [链接]h在这里写链接 [题意] 求两个串的最长公共子串. [题解] Sa[i]表示的是字典序为i的后缀的起始位置. 可以把两个字符串合在一起(中间用一个比'z'大的字符分割); 则如果Sa[i-1] ...

  10. Android 最火的高速开发框架AndroidAnnotations使用具体解释

    Android 最火的高速开发框架androidannotations配置具体解释文章中有eclipse配置步骤.Android 最火高速开发框架AndroidAnnotations简介文章中的简介. ...