| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 992 人关注过本帖
标题:皇后问题,栈实现,大家帮忙找找错啊
只看楼主 加入收藏
tiantian0623
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2008-4-23
收藏
 问题点数:0 回复次数:0 
皇后问题,栈实现,大家帮忙找找错啊
#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;
}
搜索更多相关主题的帖子: 皇后问题 int flag line top 
2008-05-01 20:33
快速回复:皇后问题,栈实现,大家帮忙找找错啊
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017437 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved