dancing links

题目链接:跳舞链

精准覆盖:

struct node
{
    int x,y;
    int l,r,u,d;
    node(int x=0,int y=0,int l=0,
         int r=0,int u=0,int d=0):
        x(x),y(y),l(l),r(r),u(u),d(d) {}
} dl[10105];

int a[105][105],id[105][105];
int idx,one[105];

void build(int n,int m)
{
    dl[0]=node(0),idx=m;
    for(int j=1; j<=m; j++)
    {
        dl[j]=node(0,j,j-1,dl[j-1].r,j,j);
        dl[dl[j-1].r].l=j,dl[j-1].r=j;
        int pre=j;
        for(int i=1; i<=n; i++)
            if(a[i][j]==1)
            {
                int x=(id[i][j]=++idx);
                dl[idx]=node(i,j,idx,idx);
                dl[x].d=dl[pre].d,dl[x].u=pre;
                dl[dl[pre].d].u=x,dl[pre].d=x;
                pre=x,one[j]++;
            }
    }
    for(int i=1; i<=n; i++)
    {
        int pre=0;
        for(int j=1; j<=m; j++)
            if(a[i][j]==1)
                if(!pre)pre=id[i][j];
                else
                {
                    int x=id[i][j];
                    dl[x].r=dl[pre].r,dl[x].l=pre;
                    dl[dl[pre].r].l=x,dl[pre].r=x;
                    pre=x;
                }
    }
}

void change(int x,bool op)
{
    dl[dl[x].r].l=op?x:dl[x].l;
    dl[dl[x].l].r=op?x:dl[x].r;
    for(int i=dl[x].d;i!=x;i=dl[i].d)
        for(int j=dl[i].r;j!=i;j=dl[j].r)
        {
            dl[dl[j].d].u=op?j:dl[j].u;
            dl[dl[j].u].d=op?j:dl[j].d;
            if(op)one[dl[j].y]++;
            else one[dl[j].y]--;
        }
}

int ans[105];

bool dancing(int depth)
{
    int x=dl[0].r;
    if(x==0)return true;
    if(dl[x].d==x)return false;
    for(int i=x;i;i=dl[i].r)
        if(one[i]

题目链接:数独

dancing links:

struct node
{
    int x,y;
    int l,r,u,d;
    node(int x=0,int y=0,int l=0,
         int r=0,int u=0,int d=0):
        x(x),y(y),l(l),r(r),u(u),d(d) {}
} dl[242550];

int a[735][330],id[735][330];
int idx,one[330],ans[85];

void build(int n,int m)
{
    dl[0]=node(0),idx=m;
    for(int j=1; j<=m; j++)
    {
        dl[j]=node(0,j,j-1,dl[j-1].r,j,j);
        dl[dl[j-1].r].l=j,dl[j-1].r=j;
        int pre=j;
        for(int i=1; i<=n; i++)
            if(a[i][j]==1)
            {
                int x=(id[i][j]=++idx);
                dl[idx]=node(i,j,idx,idx);
                dl[x].d=dl[pre].d,dl[x].u=pre;
                dl[dl[pre].d].u=x,dl[pre].d=x;
                pre=x,one[j]++;
            }
    }
    for(int i=1; i<=n; i++)
    {
        int pre=0;
        for(int j=1; j<=m; j++)
            if(a[i][j]==1)
                if(!pre)pre=id[i][j];
                else
                {
                    int x=id[i][j];
                    dl[x].r=dl[pre].r,dl[x].l=pre;
                    dl[dl[pre].r].l=x,dl[pre].r=x;
                    pre=x;
                }
    }
}

inline void change(int x,bool op)
{
    dl[dl[x].r].l=op?x:dl[x].l;
    dl[dl[x].l].r=op?x:dl[x].r;
    for(int i=dl[x].d; i!=x; i=dl[i].d)
        for(int j=dl[i].r; j!=i; j=dl[j].r)
        {
            dl[dl[j].d].u=op?j:dl[j].u;
            dl[dl[j].u].d=op?j:dl[j].d;
            if(op)one[dl[j].y]++;
            else one[dl[j].y]--;
        }
}

bool dancing(int depth)
{
    int x=dl[0].r;
    if(x==0)return true;
    if(dl[x].d==x)return false;
    for(int i=x; i; i=dl[i].r)
        if(one[i]

你可能感兴趣的:(dancing links)