首页 > 代码库 > FZU 1686 神龙的难题(DLX可重复覆盖)

FZU 1686 神龙的难题(DLX可重复覆盖)

题意:中文。点击打开链接
DLX做法:将怪物数量p作为M,由于艾米莉攻击范围n1*m1咋n*m的大范围内就会有n*m

个n1*m1,故将n*m作为N。那么bool值又是如何确定的了。n1*m1块的区域内的怪物对

的第几块小区域和怪物编号就是1.接下来就是DLX做法求最小的值。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int maxn=15*15+10;
const int maxnnode=maxn*maxn;
const int mod = 1000000007;
struct DLX{
    int n,m,size;
    int U[maxnnode],D[maxnnode],L[maxnnode],R[maxnnode],Row[maxnnode],Col[maxnnode];
    int H[maxn],S[maxn];//H[i]位置,S[i]个数
    int ansd;
    void init(int a,int b)
    {
        n=a;  m=b;
        REPF(i,0,m)
        {
            S[i]=0;
            U[i]=D[i]=i;
            L[i]=i-1;
            R[i]=i+1;
        }
        R[m]=0; L[0]=m;
        size=m;
        REPF(i,1,n)
           H[i]=-1;
    }
    void link(int r,int c)
    {
        ++S[Col[++size]=c];
        Row[size]=r;
        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;
        }
    }
    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;
    }
    bool v[maxn];
    int f()
    {
        int ret = 0;
        for(int c = R[0];c != 0;c = R[c])v[c] = true;
        for(int c = R[0];c != 0;c = R[c])
            if(v[c])
            {
                ret++;
                v[c] = false;
                for(int i = D[c];i != c;i = D[i])
                    for(int j = R[i];j != i;j = R[j])
                        v[Col[j]] = false;
            }
        return ret;

    }
    void Dance(int d)
    {
        if(d + f() >=ansd) return ;
        if(R[0] == 0)
        {
            if(d<ansd)  ansd=d;
            return ;
        }
        int c = R[0];
        for(int i = R[0];i != 0;i = R[i])
            if(S[i] < S[c])
                c = i;
        for(int i = D[c];i != c;i = D[i])
        {
            remove(i);
            for(int j = R[i];j != i;j = R[j])remove(j);
            Dance(d+1);
            for(int j = L[i];j != i;j = L[j])resume(j);
            resume(i);
        }
    }
};
DLX L;
int n,m;
int id[25][25];
int main()
{
    int x,n1,m1;
    while(~scanf("%d%d",&n,&m))
    {
        int num=0;
        CLEAR(id,0);
        REP(i,n)
        {
            REP(j,m)
            {
                scanf("%d",&id[i][j]);
                if(id[i][j]) id[i][j]=(++num);
            }
        }
        L.init(n*m,num);
        num=1;
        scanf("%d%d",&n1,&m1);
        REP(i,n)
        {
            REP(j,m)
            {
                for(int xx=0;xx<n1&&i+xx<n;xx++)
                {
                    for(int yy=0;yy<m1&&j+yy<=m;yy++)
                        if(id[i+xx][j+yy])
                          L.link(num,id[i+xx][j+yy]);
                }
                num++;
            }
        }
//        cout<<"fuck  "<<endl;
        L.ansd=0x3f3f3f3f;
        L.Dance(0);
        printf("%d\n",L.ansd);
    }
    return 0;
}



FZU 1686 神龙的难题(DLX可重复覆盖)