Spoj1771-Yet Another N-Queen Problem(精确覆盖)

本文介绍了一种解决N皇后问题的高效算法——精确覆盖DLX算法。通过构造棋盘模型,利用行、列和斜对角线的关系,解决了部分皇后已预设位置的情况,并给出了完整的C++代码实现。

Description

After solving Solution to the n 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

题意:N皇后问题,但是棋盘已经有一些皇后,然后问如何选择皇后的位置使得他们互不攻击,N<=50

 

解析:N这么大,显然不可能爆搜,这里用到的是精确覆盖DLX算法,行表示每个点,DLX的列对应棋盘的行,列和两个方向的斜对角。

搜到N步就可以了,考虑行就好了,不用考虑斜对角什么的。

 

代码:

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int INF=1e9+7;
const int ms=51*51;
const int maxn=ms*5;
int N,ans[55],res[55];//ans存储第几个选择的编号,res保存第几行答案是第几列
struct DLX
{
    int n,id;
    int L[maxn],R[maxn],U[maxn],D[maxn];
    int C[maxn],S[maxn],loc[maxn][2];
    int H[ms];
    void init(int nn=0) //传列长
    {
        n=nn;
        for(int i=0;i<=n;i++) U[i]=D[i]=i,L[i]=i-1,R[i]=i+1;
        L[0]=n; R[n]=0;
        id=n;
        memset(S,0,sizeof(S));
        memset(H,-1,sizeof(H));
    }
    void Link(int x,int y)
    {
        ++id;
        D[id]=y; U[id]=U[y];
        D[U[y]]=id; U[y]=id;
        loc[id][0]=x,loc[id][1]=y;
        C[id]=y; S[y]++;
        if(H[x]==-1) H[x]=L[id]=R[id]=id;
        else
        {
            int a=H[x];
            int b=R[a];
            L[id]=a; R[a]=id;
            R[id]=b; L[b]=id;
            H[x]=id;
        }
    }
    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)
    {
        for(int i=U[c];i!=c;i=U[i])
            for(int j=R[i];j!=i;j=R[j])
        {
            S[C[j]]++;
            U[D[j]]=j;
            D[U[j]]=j;
        }
        L[R[c]]=c;
        R[L[c]]=c;
    }
    bool dfs(int step)
    {
        if(step>=N) return true;
        if(R[0]==0) return false;
        int Min=INF,c=-1;
        for(int i=R[0];i;i=R[i])
        {
            if(i>N) break;
            if(Min>S[i]){ Min=S[i]; c=i; }
        }
        if(c==-1) return false;
        Remove(c);
        for(int i=D[c];i!=c;i=D[i])
        {
            ans[step]=loc[i][0];
            for(int j=R[i];j!=i;j=R[j]) Remove(C[j]);
            if(dfs(step+1)) return true;
            for(int j=L[i];j!=i;j=L[j]) Resume(C[j]);
        }
        Resume(c);
        return false;
    }
}dlx;
bool vis[55*6];
int main()
{
    while(scanf("%d",&N)!=EOF)
    {
        dlx.init(N*6-2);
        memset(vis,false,sizeof(vis));
        int y;
        for(int x=1;x<=N;x++)
        {
            scanf("%d",&y);
            if(y==0) continue;
            int a=x,b=N+y,c=2*N+N+x-y,d=4*N+x+y-2; //对应的行列斜对角编号
            vis[a]=vis[b]=vis[c]=vis[d]=true; //标记
            int t=(x-1)*N+y-1;
            dlx.Link(t,a); //连接
            dlx.Link(t,b);
            dlx.Link(t,c);
            dlx.Link(t,d);
        }
        for(int x=1;x<=N;x++)
            for(int y=1;y<=N;y++)
        {
            int a=x,b=N+y,c=2*N+N+x-y,d=4*N+x+y-2;
            if(vis[a]||vis[b]||vis[c]||vis[d]) continue; //有被占据不考虑
            int t=(x-1)*N+y-1;
            dlx.Link(t,a);
            dlx.Link(t,b);
            dlx.Link(t,c);
            dlx.Link(t,d);
        }
        if(!dlx.dfs(0)) printf("No answer find\n");
        else
        {
            for(int i=0;i<N;i++) res[ans[i]/N]=ans[i]%N;
            for(int i=0;i<N;i++) printf("%d%c",res[i]+1,i==N-1?'\n':' ');
        }
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/wust-ouyangli/p/5749698.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值