没有合适的资源?快使用搜索试试~ 我知道了~
用栈求解n皇后问题.docx
需积分: 0 0 下载量 96 浏览量
2023-03-16
12:52:45
上传
评论
收藏 17KB DOCX 举报
温馨提示
试读
2页
用栈求解n皇后问题
资源推荐
资源详情
资源评论
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 100
typedef struct
{ int col[MaxSize];
int top;
} StackType;
void dispasolution(StackType St)
{ static int count=0;
printf(" 第%d 个解:",++count);
for (int i=1;i<=St.top;i++)
printf("(%d,%d) ",i,St.col[i]);
printf("");
}
bool place(StackType St,int k,int j)
{ int i=1;
if (k==1) return true;
while (i<=k-1)
{ if ((St.col[i]==j) || (abs(j-St.col[i])==abs(i-k)))
return false;
i++;
}
return true;
}
void queen(int n)
{
int k;
bool find;
StackType St;
St.top=0;
St.top++; St.col[St.top]=0;
while (St.top!=0)
{ k=St.top;
find=false;
for (int j=St.col[k]+1;j<=n;j++)
if (place(St,k,j))
{ St.col[St.top]=j;
find=true;
break;
}
if (find)
{ if (k==n)
dispasolution(St);
else
资源评论
electrical1024
- 粉丝: 1903
- 资源: 2086
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功