zoj 1031& poj 1084 (Dancing Links 重复覆盖)

Square Destroyer

Time Limit: 10 Seconds       Memory Limit: 32768 KB

The left figure below shows a complete 3*3 grid made with 2*(3*4) (=24) matchsticks. The lengths of all matchsticks are one. You can find many squares of different sizes in the grid. The size of a square is the length of its side. In the grid shown in the left figure, there are 9 squares of size one, 4 squares of size two, and 1 square of size three. 

Each matchstick of the complete grid is identified with a unique number which is assigned from left to right and from top to bottom as shown in the left figure. If you take some matchsticks out from the complete grid, then some squares in the grid will be destroyed, which results in an incomplete 3*3 grid. The right figure illustrates an incomplete 3*3 grid after removing three matchsticks numbered with 12, 17 and 23. This removal destroys 5 squares of size one, 3 squares of size two, and 1 square of size three. Consequently, the incomplete grid does not have squares of size three, but still has 4 squares of size one and 1 square of size two. 

As input, you are given a (complete or incomplete) n*n grid made with no more than 2n(n+1) matchsticks for a natural number n<=5 . Your task is to compute the minimum number of matchsticks taken out to destroy all the squares existing in the input n*n grid.

Input 

The input consists of T test cases. The number of test cases (T) is given in the first line of the input. Each test case consists of two lines: The first line contains a natural number n , not greater than 5, which implies you are given a (complete or incomplete) n*n grid as input, and the second line begins with a nonnegative integer k , the number of matchsticks that are missing from the complete n*n grid, followed by k numbers specifying the matchsticks. Note that if k is equal to zero, then the input grid is a complete n*n grid; otherwise, the input grid is an incomplete n*n grid such that the specified k matchsticks are missing from the complete n*n grid. 


Output 

Print exactly one line for each test case. The line should contain the minimum number of matchsticks that have to be taken out to destroy all the squares in the input grid. 



Sample Input 





3 12 17 23


Output for the Sample Input




Source: Asia 2001, Taejon (South Korea)

题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=31

poj一模一样的题:http://poj.org/problem?id=1084

分析:这题只要把存在的正方形作为列,把火柴棒作为行就行,关键是建图,挺纠结的~~~

代码:

#include<cstdio>
#include<cstring>
#define mm 111111
#define mn 1111
int U[mm],D[mm],L[mm],R[mm],C[mm];
int H[mn],S[mn],g[mn][mn],q[mn];
bool v[mn],x[mn],e[mn][mn];
int n,m,size,ans,r;
void prepare(int r,int c)
{
    for(int i=0;i<=c;++i)
    {
        S[i]=0;
        U[i]=D[i]=i;
        L[i+1]=i;
        R[i]=i+1;
    }
    R[c]=0;
    while(r)H[r--]=-1;
}
void remove(int c)
{
    for(int i=D[c];i!=c;i=D[i])
        L[R[i]]=L[i],R[L[i]]=R[i];
}
void resume(int c)
{
    for(int i=U[c];i!=c;i=U[i])
        L[R[i]]=R[L[i]]=i;
}
int f()
{
    int i,j,c,ret=0;
    for(c=R[0];c;c=R[c])v[c]=1;
    for(c=R[0];c;c=R[c])
        if(v[c])for(v[c]=0,++ret,i=D[c];i!=c;i=D[i])
            for(j=R[i];j!=i;j=R[j])v[C[j]]=0;
    return ret;
}
void Dance(int k)
{
    if(k+f()>=ans)return;
    if(!R[0])
    {
        ans=k;
        return;
    }
    int i,j,c,tmp=mm;
    for(i=R[0];i;i=R[i])
        if(S[i]<tmp)tmp=S[c=i];
    for(i=D[c];i!=c;i=D[i])
    {
        remove(i);
        for(j=R[i];j!=i;j=R[j])remove(j);
        Dance(k+1);
        for(j=L[i];j!=i;j=L[j])resume(j);
        resume(i);
    }
}
void Link(int r,int c)
{
    ++S[C[++size]=c];
    D[size]=D[c];
    U[D[c]]=size;
    U[size]=c;
    D[c]=size;
    if(H[r]<0)H[r]=L[size]=R[size]=size;
    else
    {
        R[size]=R[H[r]];
        L[R[H[r]]]=size;
        L[size]=H[r];
        R[H[r]]=size;
    }
}
bool ok(int x1,int y1,int x2,int y2)
{
    int i,j,id;
    r=0;
    for(i=x1;i<=x2;++i)
    {
        if(x[id=g[i<<1][(y1<<1)-1]])q[r++]=id;
        else return 0;
        if(x[id=g[i<<1][(y2<<1)+1]])q[r++]=id;
        else return 0;
    }
    for(j=y1;j<=y2;++j)
    {
        if(x[id=g[(x1<<1)-1][j<<1]])q[r++]=id;
        else return 0;
        if(x[id=g[(x2<<1)+1][j<<1]])q[r++]=id;
        else return 0;
    }
    return 1;
}
int main()
{
    int i,j,k,T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        m=n*(n+1)*2;
        for(i=0;i<=m;++i)x[i]=1;
        for(k=0,i=1;i<=n+1;++i)
        {
            for(j=1;j<=n;++j)g[i*2-1][j*2]=++k;
            for(j=1;j<=n+1;++j)g[i*2][j*2-1]=++k;
        }
        scanf("%d",&k);
        while(k--)scanf("%d",&i),x[i]=0;
        memset(e,0,sizeof(e));
        for(size=k=0;k<n;++k)
            for(i=1;i+k<=n;++i)
                for(j=1;j+k<=n;++j)
                    if(ok(i,j,i+k,j+k))
                    {
                        ++size;
                        while(r--)e[q[r]][size]=1;
                    }
        prepare(m,size);
        for(i=1;i<=m;++i)
            for(j=1;j<=size;++j)
                if(e[i][j])Link(i,j);
        ans=m;
        Dance(0);
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值