| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦   
共有 403 人关注过本帖
标题:皇后问题,栈实现,大家帮忙找找错啊
收藏  订阅  推荐  打印

皇后问题,栈实现,大家帮忙找找错啊

#include<stdio.h>
#include<stdlib.h>
#define max 20
struct{
    int line;//行
    int flag;//标志位置安全
}st[max];//栈

void queue(int n)//n皇后问题
{
    int i,j,s;int top=1;
st[top].line=1//首行赋值为1;
st[top].flag=1;//第一行可放

while(top<=n)
{
    if(st[top].flag==1)
{top++;st[top].line=1;st[top].flag=0;}//此位置安全,去下一列找,把安全位置入栈
else
{
    while(st[top].line<=n)
{
    for(i=1;i<top;i++)//找安全行
if(st[i].line==st[top].line||(abs(top-i)==abs(st[top].line-st[i].line)))//行和对角线
{st[top].flag=0;break;}
else
st[top].flag=1;
if(st[top].flag==1)break;
else (st[top].line)++;//找下一行
    }

if (st[top].flag==0||st[top].line>n)//不安全,退栈
{top--;st[top].flag=0;}
}
}

for(j=1;j<=n;j++)

printf("\t(%d,%d)",st[j].line,j);
}

int main()
{
    int m;
    scanf("%d",&m);
    printf("%d皇后问题",m);
        queue(m);
        return 0;
}

TOP

共有 402 人关注过本帖
发新话题
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.080152 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved