|
tiantian0623
新手上路
帖子:13 |
1#
大中
小发表于 2008-5-1 20:33 只看该作者
皇后问题,栈实现,大家帮忙找找错啊#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 |
|
TOP |









