首页 > 代码库 > zoj 3209 Treasure Map(精确覆盖)

zoj 3209 Treasure Map(精确覆盖)

Treasure Map

Time Limit: 2 Seconds      Memory Limit: 32768 KB

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 n m p (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 x1 y1 x2 y2 (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

35 5 10 0 5 55 5 20 0 3 52 0 5 530 30 50 0 30 100 10 30 200 20 30 300 0 15 3015 0 30 30

Sample Output

1-12

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.

#include <iostream>#include <string>#include <stdio.h>#include <cmath>#include <algorithm>#include <cstring>#define INF 1000000000#define MAXN 500005using namespace std;int n,m,p,tot,col[MAXN],u[MAXN],d[MAXN],l[MAXN],r[MAXN],sum[1005],ans;int add(int c){    ++tot;    u[tot]=d[tot]=l[tot]=r[tot]=tot;    col[tot]=c;    sum[c]++;        return tot;}void linklr(int head,int x){    l[x]=l[head];    r[l[head]]=x;    l[head]=x;    r[x]=head;}void linkud(int head,int x){    u[x]=u[head];    d[u[head]]=x;    u[head]=x;    d[x]=head;}void del(int c){    l[r[c]]=l[c];    r[l[c]]=r[c];    for (int i=d[c];i!=c;i=d[i])      for (int j=r[i];j!=i;j=r[j])     {         u[d[j]]=u[j];         d[u[j]]=d[j];         sum[col[j]]--;         }}void res(int c){    for (int i=u[c];i!=c;i=u[i])      for (int j=l[i];j!=i;j=l[j])     {         u[d[j]]=j;         d[u[j]]=j;         sum[col[j]]++;         }     l[r[c]]=c;     r[l[c]]=c;}void dfs(int num){    int c,i,j;        if (!r[0])//如果列为空的话    {         ans=min(ans,num);//有很多种解决方案 选中其中步数最小的         return ;    }    c=r[0];//得到第一列    for (i=r[0];i!=0;i=r[i]) //枚举还存在的每一列 选中该列相面相同数量最少的进行操作        if (sum[i]<sum[c]) c=i;    del(c);//先删除该列所影响的    for (i=d[c];i!=c;i=d[i])//枚举该列下面所有存在的行    {        for (j=r[i];j!=i;j=r[j])//枚举相同行 中的所有列 删除        {             del(col[j]);        }        dfs(num+1) ;//传递删除了几行 当前加1        for (j=l[i];j!=i;j=l[j]) res(col[j]);//然后恢复该行的    }    res(c);    //恢复该列}int main(){    int t0,x1,y1,x2,y2,cur,h,fst;        scanf("%d",&t0);    while (t0--)    {        scanf("%d%d%d",&n,&m,&p);//n*m大小的矩形  p个矩形块        ans=INF;        tot=-1;        memset(sum,0,sizeof(sum));        for (int t=0;t<n*m+1;t++)//枚举n*m个单位 主要是所有列名建立连接r,l,u,d(前n*m都是列)        {             add(t);             linklr(0,t);//r[0] 是所有列的头部指向        }                for (int t=0;t<p;t++)//为n个块做连接        {            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);//得到左上和右下角的点            fst=0;            for (int i=x1;i<x2;i++)//遍历这块面积中的每一个点 建立连接             for (int j=y1;j<y2;j++)             {                 cur=add(i*m+j+1); //在该列下面重复添加一次  即该数量sum记录了列下面相同的个数和自己                 if (fst++) linklr(h,cur); else h=cur;//如5→7→9                 linkud(i*m+j+1,cur);//列 作为头部连接该点             }        }        dfs(0);//删除了0行        if (ans!=INF) printf("%d\n",ans);else printf("-1\n");    }        return 0;}