zoj 3209

来源:互联网 发布:warshall算法详解 编辑:程序博客网 时间:2024/05/09 22:47
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 <= n, m <= 30, 1 <= p <= 500), the width and the height of the map, and the number of pieces. Thenp 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.


把n*m的矩阵看成n*m个单位元,作为n*m列;每一个矩形一行。

问题即转化为从这些行中选择最少的一部分使每一列被覆盖且仅覆盖一次

通过这题,知道啦自己的模板居然有问题,以前也注意过,觉得没啥影响,只是遍历顺序不一样而已,可是这题,用我的模板就是超时,换种遍历方法,就过啦。

甚是不解,先放着,以后想明白啦再说。

ac代码:

#include<stdio.h>#define inf 0x3fffffff#define N 500000int ans,s[N],u[N],d[N],l[N],r[N],col[N],row[N],h[N],size;void init(int n,int m){  int i;  for(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(i=1;i<=n;i++)      h[i]=-1;}void link(int x,int y){   ++s[col[++size]=y];   row[size]=x;//从下往上开始插入   d[size]=d[y];   u[d[y]]=size;   u[size]=y;   d[y]=size;   if(h[x]<0)    h[x]=l[size]=r[size]=size;   else   {      r[size]=r[h[x]];      l[r[h[x]]]=size;      l[size]=h[x];      r[h[x]]=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;   l[r[c]]=r[l[c]]=c;   for(i=u[c];i!=c;i=u[i])//从下到上     for(j=l[i];j!=i;j=l[j])//从右到左     {      d[u[j]]=j;      u[d[j]]=j;      ++s[col[j]];      }}void dance(int x){  int i,j;  if(r[0]==0)  {    if(x<ans)      ans=x;    return ;  }  int c=r[0];  for(i=r[0];i!=0;i=r[i])     if(s[i]<s[c])       c=i;  remove(c);  for(i=d[c];i!=c;i=d[i])  {   for(j=r[i];j!=i;j=r[j])      remove(col[j]);    dance(x+1);    for(j=l[i];j!=i;j=l[j])//从左开始遍历       resume(col[j]);  }  resume(c);}int main(){  int t,n,m,p,k,x1,y1,x2,y2,i,j;  //freopen("a.txt","r",stdin);  scanf("%d",&t);  while(t--)  {     scanf("%d%d%d",&n,&m,&p);     init(p,n*m);     for(k=1;k<=p;k++)     {       scanf("%d%d%d%d",&x1,&y1,&x2,&y2);       for(i=x1;i<x2;i++)        for(j=y1;j<y2;j++)           link(k,i*m+j+1);     }     ans=inf;     dance(0);     if(ans==inf)         printf("-1\n");     else         printf("%d\n",ans);  }  return 0;}
超时代码:
#include<stdio.h>#define inf 0x3fffffff#define N 500000int ans,s[N],u[N],d[N],l[N],r[N],col[N],row[N],h[N],size;void init(int n,int m){  int i;  for(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(i=1;i<=n;i++)      h[i]=-1;}void link(int x,int y){   ++s[col[++size]=y];   row[size]=x;   d[u[y]]=size;   u[size]=u[y];   d[size]=y;   u[y]=size;   if(h[x]<0)    h[x]=l[size]=r[size]=size;   else   {      r[size]=r[h[x]];      l[r[h[x]]]=size;      l[size]=h[x];      r[h[x]]=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;   l[r[c]]=r[l[c]]=c;   for(i=d[c];i!=c;i=d[i])     for(j=r[i];j!=i;j=r[j])     {      d[u[j]]=j;      u[d[j]]=j;      ++s[col[j]];      }}void dance(int x){  int i,j;  if(r[0]==0)  {    if(x<ans)      ans=x;    return ;  }  int c=r[0];  for(i=r[0];i!=0;i=r[i])     if(s[i]<s[c])       c=i;  remove(c);  for(i=d[c];i!=c;i=d[i])  {   for(j=r[i];j!=i;j=r[j])      remove(col[j]);    dance(x+1);    for(j=r[i];j!=i;j=r[j])       resume(col[j]);  }  resume(c);}int main(){  int t,n,m,p,k,x1,y1,x2,y2,i,j;  //freopen("a.txt","r",stdin);  scanf("%d",&t);  while(t--)  {     scanf("%d%d%d",&n,&m,&p);     init(p,n*m);     for(k=1;k<=p;k++)     {       scanf("%d%d%d%d",&x1,&y1,&x2,&y2);       for(i=x1;i<x2;i++)        for(j=y1;j<y2;j++)           link(k,i*m+j+1);     }     ans=inf;     dance(0);     if(ans==inf)         printf("-1\n");     else         printf("%d\n",ans);  }  return 0;}



0 0
原创粉丝点击