给出一个左下角为\((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. 【转】Odoo开发之:工作流 workflow

    在OpenERP中,工作流是管理一组“所做的事情”(与一些数据模型的记录关联)的人为现象.工作流提供了高级别的方式来组织记录要上做的事情. 具体地说,工作流是一个定向的路径,这里节点称为活动并且弧线称 ...

  2. php输出带尖括号的内容

    有这样的数组 $arr = array( 'facebook' => 'facebook', '<facebook>' => '<facebook>', ); 输出 ...

  3. LWM2M简介-学习记录

    1. Lightweight M2M 基础,谁搞出来的 OMA是一家国际组织,因为物联网的兴起, OMA在传统的OMA-DM协议基础之上,提出了LWM2M协议.这个协议基于COAP协议,COAP协议基 ...

  4. 解决replace格式替换后光标定位问题

    场景:格式化银行卡444格式 手机号344格式 身份证号684格式 校验数据格式,replace后光标定位错乱 或光标一直定位在最后 解决,只针对input,代码用的vue: 获取光标位置: getC ...

  5. 关于css,js显示不出来

    有时候取消了对js,css文件的拦截,但是在网页上还是显示不了,可以试一下下面的方法: 在jsp页面添加下面内容 css的调用路径用path来代替

  6. 三分钟小课堂-----------------docker(三)增删改查命令

    主要为docker容器的增删改查命令 1  创建容器: docker run   -it   --name 别名  image_name   /bin/bash --name 别名 -d 后台 -t ...

  7. 代码对齐 (Alignment of Code,ACM/ICPC NEERC 2010,UVa1593)

    题目描述: 解题思路: 输入时提出单个字符串,并用一个数组记录每列最长长度,格式化输出 #include <iostream> #include <algorithm> #in ...

  8. 查看python中包的文档

    核心命令:python -m pydoc 查询某包:python -m pydoc 包名 示例: C:\Users\xxx>python -m pydoc pydoc - the Python ...

  9. 由一个hash字符串生成多个子hash字符串

    通过存储一个head hash,然后把子hash放到网络中 当然,也可以像默克尔树那样的,生成多级的子hash ,可以通过规则配置不同的hash 生成方式.倒置的默克尔树 我有一个文件,然后我把她分隔 ...

  10. 3.hadoop完全分布式搭建

    3.Hadoop完全分布式搭建 1.完全分布式搭建 配置 #cd /soft/hadoop/etc/ #mv hadoop local #cp -r local full #ln -s full ha ...