Treasure Map

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Appoint description: 
System Crawler  (2015-04-09)

Description

Your boss once had got many copies of a treasure map. Unfortunately, all the copies are now broken to many rectangular pieces, and what make it worse, he has lost some of the pieces. Luckily, it is possible to figure out the position of each piece in the original map. Now the boss asks you, the talent programmer, to make a complete treasure map with these pieces. You need to make only one complete map and it is not necessary to use all the pieces. But remember, pieces are not allowed to overlap with each other (See sample 2).

Input

The first line of the input contains an integer T (T <= 500), indicating the number of cases.

For each case, the first line contains three integers nmp (1 <= nm <= 30, 1 <= p <= 500), the width and the height of the map, and the number of pieces. Then p lines follow, each consists of four integers x1y1x2y2 (0 <= x1 < x2 <= n, 0 <= y1 < y2 <= m), where (x1, y1) is the coordinate of the lower-left corner of the rectangular piece, and (x2, y2) is the coordinate of the upper-right corner in the original map.

Cases are separated by one blank line.

Output

If you can make a complete map with these pieces, output the least number of pieces you need to achieve this. If it is impossible to make one complete map, just output -1.

Sample Input

3
5 5 1
0 0 5 5 5 5 2
0 0 3 5
2 0 5 5 30 30 5
0 0 30 10
0 10 30 20
0 20 30 30
0 0 15 30
15 0 30 30

Sample Output

1
-1
2

Hint

For sample 1, the only piece is a complete map.

For sample 2, the two pieces may overlap with each other, so you can not make a complete treasure map.

For sample 3, you can make a map by either use the first 3 pieces or the last 2 pieces, and the latter approach one needs less pieces.

草泥马啊!!!!N和M弄反了啊!!!!!T了一下午啊!!!!!!!!找BUG都快找哭了啊!!!!!!!!!!

丧心病狂弄了个M行N列啊!!!!!!!!!!这尼玛反人类啊!!!!!!活生生由300ms优化到了80ms啊!!!!!!!!!

 #include <iostream>
#include <cstdio>
using namespace std; const int SIZE = * ;
const int HEAD = ;
int U[SIZE],D[SIZE],L[SIZE],R[SIZE],C[SIZE],S[SIZE];
int N,M;
int ANS = 0x7fffffff; void ini(void);
void dancing(int ans);
void remove(int);
void resume(int);
int main(void)
{
int t,p,count;
int x_1,y_1,x_2,y_2;
int col; scanf("%d",&t);
while(t --)
{
ANS = 0x7fffffff;
scanf("%d%d%d",&N,&M,&p);
ini();
count = N * M + ;
while(p --)
{
scanf("%d%d%d%d",&x_1,&y_1,&x_2,&y_2); int first = count;
for(int i = x_1;i < x_2;i ++)
for(int j = y_1;j < y_2;j ++)
{
col = i * M + j + ;
U[count] = U[col];
D[count] = col;
L[count] = count - ;
R[count] = count + ; D[U[col]] = count;
U[col] = count; C[count] = col;
++ S[col];
++ count;
}
R[count - ] = first;
L[first] = count - ;
}
dancing();
if(ANS == 0x7fffffff)
puts("-1");
else
printf("%d\n",ANS);
} return ;
} void ini(void)
{
L[HEAD] = N * M;
R[HEAD] = ;
U[HEAD] = D[HEAD] = S[HEAD] = C[HEAD] = HEAD; for(int i = ;i <= N * M;i ++)
{
U[i] = D[i] = i;
L[i] = i - ;
R[i] = i + ; C[i] = i;
S[i] = ;
}
R[N * M] = ;
} void dancing(int ans)
{
if(ans >= ANS)
return ; if(R[HEAD] == HEAD)
{
ANS = ans < ANS ? ans : ANS;
return ;
} int min_loc = R[HEAD];
for(int i = R[HEAD];i;i = R[i])
if(S[i] < S[min_loc])
min_loc = i; remove(min_loc);
for(int i = D[min_loc];i != min_loc;i = D[i])
{
for(int j = R[i];j != i;j = R[j])
remove(C[j]);
dancing(ans + );
for(int j = L[i];j != i;j = L[j])
resume(C[j]);
}
resume(min_loc); } void remove(int c)
{
R[L[c]] = R[c];
L[R[c]] = L[c]; for(int i = D[c];i != c;i = D[i])
for(int j = R[i];j != i;j = R[j])
{
D[U[j]] = D[j];
U[D[j]] = U[j];
-- S[C[j]];
}
} void resume(int c)
{
R[L[c]] = c;
L[R[c]] = c; for(int i = U[c];i != c;i = U[i])
for(int j = R[i];j != i;j = R[j])
{
D[U[j]] = j;
U[D[j]] = j;
++ S[C[j]];
}
}

ZOJ 3209 Treasure Map (Dancing Links)的更多相关文章

  1. ZOJ 3209 Treasure Map (Dancing Links)

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

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

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

  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+精确覆盖。

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

  5. ZOJ 3209 Treasure Map DLX

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

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

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

  7. ZOJ 3209 Treasure Map 精确覆盖

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

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

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

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

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

随机推荐

  1. Hibernate关联关系之双向1—n

    •双向 1-n 与双向 n-1 是完全相同的两种情形 •双向 1-n 需要在1的一端可以访问n的一端,反之依然. 测试实例代码: 实体类: package com.elgin.hibernate.nt ...

  2. ActiveX控件是什么?

    一.ActiveX的由来 ActiveX最初只不过是一个商标名称而已,它所涵盖的技术并不是各自孤立的,其中多数都与Internet和Web有一定的关联.更重要的是,ActiveX的整体技术是由Micr ...

  3. OLEVARIANT的替代——FIREDAC的TFDJSONDataSets和TFDJSONDeltas

    OLEVARIANT——这个COM的序列格式,也是DATASNAP已使用了20年的序列格式,在20年以后的今天,终于有了它的替代者:FIREDAC的TFDJSONDataSets和TFDJSONDel ...

  4. [iOS基础控件 - 6.9] 聊天界面Demo

    A.需求 做出一个类似于QQ.微信的聊天界面 1.每个cell包含发送时间.发送人(头像).发送信息 2.使用对方头像放在左边,我方头像在右边 3.对方信息使用白色背景对话框,我方信息使用蓝色背景对话 ...

  5. Educational Codeforces Round 10 D. Nested Segments (树状数组)

    题目链接:http://codeforces.com/problemset/problem/652/D 给你n个不同的区间,L或者R不会出现相同的数字,问你每一个区间包含多少个区间. 我是先把每个区间 ...

  6. Contest 7.23(不知道算什么)

    Problem A   URAL 1181 Cutting a Painted Polygon 题目大意就是说有一个N边形,让你做N-3条边,让他们的每个三角形的三个顶点颜色都不相同. 这里有一个引理 ...

  7. 单个SWF文件loading加载详解(转)

    通过带宽查看器,可以看到SWF中每帧所占带宽状况.另外,我们还可以在Flash发布设置中,选择生成体积报告. 勾选这一项之后,发布flash时,会自动在fla目录中生成一个名为”文件名 Report. ...

  8. java笔记-关于一些常用 且实用的开源包

    作为一只从.net转java的程序猿..表示有些jdk原生的类很不习惯.. 1.时间处理 代替原生Calendar   joda-time 框架.地址https://github.com/JodaOr ...

  9. 【M22】考虑以操作符复合形式(op=)取代其独身形式(op)

    1.对于内置类型,x = x+y 与x+=y的结果相同. 2. x=x+y 与 x+=y的结果相同,但二者做的事情差别很大. a.x=x+y做的事情:方法内有个局部对象,值为x+y,返回局部对象,返回 ...

  10. OSG中的示例程序简介(转)

    OSG中的示例程序简介 1.example_osganimate一)演示了路径动画的使用 (AnimationPath.AnimationPathCallback),路径动画回调可以作用在Camera ...