Spoj 1771(Dancing Links 精确覆盖变形)

SPOJ Problem Set (classical)

1771. Yet Another N-Queen Problem

Problem code: NQUEEN


After solving Solution to the Queens Puzzle by constructing, LoadingTime wants to solve a harder version of the N-Queen Problem. Some queens have been set on particular locations on the board in this problem. Can you help him??

Input

The input contains multiple test cases. Every line begins with an integer N (N<=50), then N integers followed, representing the column number of the queen in each rows. If the number is 0, it means no queen has been set on this row. You can assume there is at least one solution.

Output

For each test case, print a line consists of N numbers separated by spaces, representing the column number of the queen in each row. If there are more than one answer, print any one of them.

Example

Input:
4 0 0 0 0
8 2 0 0 0 4 0 0 0

Output:
2 4 1 3
2 6 1 7 4 8 3 5

Added by:Bin Jin
Date:2007-09-06
Time limit:5s
Source limit:10000B
Languages:All except: C++ 4.0.0-8
Resource:own problem





题目:http://www.spoj.pl/problems/NQUEEN/

分析:这题也可以转化为DLX精确覆盖问题,将棋盘的 行 列  左斜线 右斜线 作为覆盖点,将每个格子作为操作,但是不同的是只需选折覆盖行,或者覆盖列,之前没有判断,直接扫描全部覆盖,一直无解,找了一个晚上加一个早上阿。。。速度还行,跑了0.5s

代码:

#include<cstdio>
#define mm 777777
#define mn 3333
int U[mm],D[mm],L[mm],R[mm],C[mm],id[mm];
int H[mn],S[mn],Q[mn],P[mn],X[mn],Y[mn];
bool p[mn];
int n,size;
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[size=c]=0;
    while(r)H[r--]=-1;
}
void remove(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],--S[C[j]];
}
void resume(int c)
{
    L[R[c]]=R[L[c]]=c;
    for(int i=U[c];i!=c;i=U[i])
        for(int j=L[i];j!=i;j=L[j])
            ++S[C[U[D[j]]=D[U[j]]=j]];
}
bool Dance(int k)
{
    if(k>=n)
    {
        for(int i=0;i<k;++i)P[X[id[Q[i]]]]=Y[id[Q[i]]];
        for(int i=1;i<n;++i)printf("%d ",P[i]);
        printf("%d\n",P[n]);
        return 1;
    }
    if(!R[0])return 0;
    int i,j,c,tmp=mm;
    for(i=R[0];i;i=R[i])
        if(i<=n)
        {
            if(S[i]<tmp)tmp=S[c=i];
        }else break;
    remove(c);
    for(i=D[c];i!=c;i=D[i])
    {
        Q[k]=i;
        for(j=R[i];j!=i;j=R[j])remove(C[j]);
        if(Dance(k+1))return 1;
        for(j=L[i];j!=i;j=L[j])resume(C[j]);
    }
    resume(c);
    return 0;
}
void Link(int r,int c)
{
    ++S[C[++size]=c];
    id[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 place(int &p1,int &p2,int &p3,int &p4,int i,int j)
{
    p1=j,p2=n+i,p3=n+n+i-j+n,p4=n+n+n+n-1+i+j-1;
}
int main()
{
    int i,j,k,p1,p2,p3,p4;
    while(scanf("%d",&n)!=-1)
    {
        for(i=1;i<=n*6-2;++i)p[i]=0;
        prepare(n*n,n*6-2);
        for(k=i=1;i<=n;++i)
        {
            scanf("%d",&j);
            if(j)
            {
                place(p1,p2,p3,p4,i,j);
                p[p1]=p[p2]=p[p3]=p[p4]=1;
                Link(k,p1),Link(k,p2),Link(k,p3),Link(k,p4);
                X[k]=i,Y[k]=j;
                ++k;
            }
        }
        for(i=1;i<=n;++i)
            for(j=1;j<=n;++j)
            {
                place(p1,p2,p3,p4,i,j);
                if(p[p1]||p[p2]||p[p3]||p[p4])continue;
                Link(k,p1),Link(k,p2),Link(k,p3),Link(k,p4);
                X[k]=i,Y[k]=j;
                ++k;
            }
        Dance(0);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
洛谷的SPOJ需要注册一个SPOJ账号并进行绑定才能进行交题。您可以按照以下步骤进行注册: 1. 打开洛谷网站(https://www.luogu.com.cn/)并登录您的洛谷账号。 2. 在网站顶部导航栏中找到“题库”选项,将鼠标悬停在上面,然后选择“SPOJ”。 3. 在SPOJ页面上,您会看到一个提示,要求您注册SPOJ账号并进行绑定。点击提示中的链接,将会跳转到SPOJ注册页面。 4. 在SPOJ注册页面上,按照要求填写您的用户名、密码和邮箱等信息,并完成注册。 5. 注册完成后,返回洛谷网站,再次进入SPOJ页面。您会看到一个输入框,要求您输入刚刚注册的SPOJ用户名。输入用户名后,点击“绑定”按钮即可完成绑定。 现在您已经成功注册并绑定了SPOJ账号,可以开始在洛谷的SPOJ题库上刷题了。祝您顺利完成编程练习!\[1\]\[2\] #### 引用[.reference_title] - *1* *3* [(洛谷入门系列,适合洛谷新用户)洛谷功能全解](https://blog.csdn.net/rrc12345/article/details/122500057)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [luogu p7492 序列](https://blog.csdn.net/zhu_yin233/article/details/122051384)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值