给出一个左下角为\((0,0)\),右上角为\((n,m)\)的矩形,再给出\(k\)个在大矩形内的小矩形(可以重合),问是否能用这些小矩形完全覆盖这个大矩形,并且没有重合,如果可以至少需要多少个。

分析

看到覆盖没有重合,就可以知道是精确覆盖问题。精确覆盖问题的经典数据结构Dancing Links用来优化X算法可以很高效地解决。

精确覆盖问题的重点在于转化成DLX能够处理的01矩阵精确覆盖。这个转化的一个有效的方法就是,找到什么东西不能重合,什么东西只能覆盖一次。

例如这个题中,说的是小矩形不能重合,事实上可以转化成每个格子只能被一个矩形覆盖,并且一定要覆盖。这样问题就简单了。我们建一个01矩阵,\(k\)行\(n\times m\)列,表示一个矩形能够覆盖哪些点。这样就可以直接用Dancing Links X解01精确覆盖的方法来解决了。

代码

这道题虽然简单,但又写了差不多一天。在一些acm网站上做题总是这样,都不知道哪里错了,不小心有AC了,又不知道怎么回事,好像之前Topcoder的愚人节比赛一样。不过还是找出原因了。

一个是在给点编号的时候,写的应该是\((x-1)*m+y\),因为\(x\)是跟\(n\)有关的。还有一个就是,X算法本质上就是深搜的循环双向十字链表优化,所以该剪枝还是要剪枝,比如这里的最优性剪枝。

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
int read() {
int x=0,f=1;
char c=getchar();
for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;
for (;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int maxn=5e2+100;
const int maxm=1e3+100;
const int maxp=maxm*maxn;
const int inf=1e9+7;
bool a[maxn][maxm];
int n,m,ans;
int id(int x,int y) {
return (x-1)*m+y;
}
struct node {
int l,r,u,d,row,col;
};
struct DLX {
node p[maxp];
int last[maxm],tot,size[maxm];
void clear(int cs) {
memset(last,0,sizeof last);
memset(size,0,sizeof size);
memset(p,0,sizeof p);
for (int i=1;i<=cs;++i) last[i]=i;
tot=cs;
for (int i=1;i<=cs;++i) {
p[i]=(node){i-1,p[i-1].r,i,i,0,i};
p[p[i].l].r=p[p[i].r].l=i;
}
}
void build(int row,int c[],int len) {
p[++tot]=(node){tot,tot,last[c[1]],p[last[c[1]]].d,row,c[1]};
p[p[tot].u].d=p[p[tot].d].u=last[c[1]]=tot;
++size[c[1]];
for (int i=2;i<=len;++i) {
int x=c[i];
p[++tot]=(node){tot-1,p[tot-1].r,last[x],p[last[x]].d,row,x};
p[p[tot].l].r=p[p[tot].r].l=p[p[tot].u].d=p[p[tot].d].u=last[x]=tot;
++size[x];
}
}
void del(int c) {
p[p[c].l].r=p[c].r,p[p[c].r].l=p[c].l;
for (int i=p[c].d;i!=c;i=p[i].d) for (int j=p[i].r;j!=i;j=p[j].r) p[p[j].d].u=p[j].u,p[p[j].u].d=p[j].d,--size[p[j].col];
}
void back(int c) {
p[p[c].l].r=p[p[c].r].l=c;
for (int i=p[c].u;i!=c;i=p[i].u) for (int j=p[i].l;j!=i;j=p[j].l) p[p[j].d].u=p[p[j].u].d=j,++size[p[j].col];
}
void dance(int k) {
if (!p[0].r) {
ans=min(ans,k);
return;
}
int first=p[0].r;
for (int i=p[0].r;i;i=p[i].r) if (size[i]<size[first]) first=i;
if (p[first].d==first) return;
del(first);
for (int i=p[first].d;i!=first;i=p[i].d) {
for (int j=p[i].r;j!=i;j=p[j].r) del(p[j].col);
dance(k+1);
for (int j=p[i].l;j!=i;j=p[j].l) back(p[j].col);
}
back(first);
}
} dlx;
int main() {
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
freopen("my.out","w",stdout);
#endif
int T=read();
while (T--) {
n=read(),m=read(),ans=inf;
memset(a,0,sizeof a);
int K=read();
dlx.clear(n*m);
for (int k=1;k<=K;++k) {
int x1=read(),y1=read(),x2=read(),y2=read();
for (int i=x1+1;i<=x2;++i) for (int j=y1+1;j<=y2;++j) a[k][id(i,j)]=true;
}
for (int i=1;i<=K;++i) {
static int c[maxm];
int tot=0;
for (int j=1;j<=n*m;++j) if (a[i][j]) c[++tot]=j;
dlx.build(i,c,tot);
}
dlx.dance(0);
printf("%d\n",ans==inf?-1:ans);
}
return 0;
}

zoj3209-Treasure Map的更多相关文章

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

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

  2. ZOJ 3209 Treasure Map (Dancing Links)

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

  3. ZOJ 3209 Treasure Map (Dancing Links)

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

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

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

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

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

  6. 2017年上海金马五校程序设计竞赛:Problem K : Treasure Map (蛇形填数)

    Description There is a robot, its task is to bury treasures in order on a N × M grids map, and each ...

  7. ZOJ 3209 Treasure Map DLX

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

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

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

  9. ZOJ 3209 Treasure Map 精确覆盖

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

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

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

随机推荐

  1. mac, start sublime from terminal

    1.where is sublime CLI /Applications/Sublime Text.app/Contents/SharedSupport/bin/subl 2. run sublime ...

  2. HTML基础part2

    表单标签form 功能:表单用于向服务器传输数据,从而实现用户与Web服务器的交互 表单能够包含input系列标签,比如文本字段.复选框.单选框.提交按钮等等. 表单还可以包含textarea.sel ...

  3. Java设计模式(19)——行为模式之责任链模式(chain of responsibilitiy)

    一.概述 概念 UML简图 角色 抽象处理器:定义处理请求的接口 具体处理器:接收到请求后可以选择处理,也可以选择发给下家处理(持有下家的引用) 当然这里必须指出,实际中纯的责任链模式很难寻找,一般是 ...

  4. Java设计模式(7)——结构型模式之适配器模式(Adapter)

    一.概述 概念 其实,举个生活中的例子的话,适配器模式可以类比转接头,比如typeC和USB的转接头,把原本只能接typeC的接口,拓展为可以接普通USB:这里的转接头一方面需要查在typeC上,一方 ...

  5. 使用vs code写php及调试

    原文来自:http://www.cnblogs.com/CLR010/p/5276077.html 首页先改下php.ini 一般是在最底部,有就修改没有就加上去下面的配置: xdebug.remot ...

  6. Oracle DELETE和TRUNCATE 的区别

    语法delete from aa truncate table aa 区别 1.delete from后面可以写条件,truncate不可以. 2.delete from记录是一条条删的,所删除的每行 ...

  7. centos7下安装mysql8.0.12及设置权限

    一.mysql版本介绍 mysql的官网为:https://www.mysql.com/ 在官网上可以看到多个版本,主要版本如下, 1.MySQL Community Server 社区版本,开源免费 ...

  8. 第二篇 Flask基础篇之(闪现,蓝图,请求扩展,中间件)

    本篇主要内容: 闪现 请求扩展 中间件 蓝图 写装饰器,常用 functools模块,帮助设置函数的元信息 import functools def wrapper(func): @functools ...

  9. leetcode-第k个排列(Java和c++版)

    第k个排列 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" " ...

  10. CDH/Hadoop 5.15 installation steps

    I will talk the main steps to install CDH 5.15 on Linux(CENT OS 6.10).  The installation method is M ...