Algorlthm gosslp:八皇后问题浅谈
2012-07-25 20:09
225 查看
八皇后
8*8的棋盘上放八个皇后棋子,要求在纵行,竖行和斜行的八个方向上都不能有两个以上的皇后。1970和1971年,E.W.Dijkstra与N.Wirth曾用这个问题讲解程序设计技巧,淡然,像我这码农级程序员很难理解怎样应用到程序设计技巧中,现阶段只管理解问题的解决方法。
八皇后问题主要使用递归求解,然而如何减少递归次数呢?
在每次递归时,不必要所有的都检查过,例如若某列检查过,该列的其他格子就不在检查了,这个方法称为分支修剪。
程序中风别在横竖八个地方和斜方向15个地方做了标记,若有皇后则递归返回。本人觉得斜方向的标记控制和递归的调用时问题的精华。
C语言实现:
八皇后问题是大数学家高斯于1850年提出来的。该问题是在8×8的国际象棋棋盘上放置8个皇后,使得没有一个皇后能“吃掉”任何其他一个皇后,即没有任何两个皇后被放置在棋盘的同一行、同一列或同一斜线上。
编一个程序求出该问题的所有解。
回溯法
使用回溯算法求解的问题特征,求解问题要分为若干步,且每一步都有几种可能的选择,而且往往在某个选择不成功时需要回头再试另外一种选择,如果到达求解目标则每一步的选择构成了问题的解,如果回头到第一步且没有新的选择则问题求解失败。
该问题也可扩展到N后问题求解,只需修改程序main函数中的n值即可。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/************************************************************************
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* n后问题求解
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
************************************************************************/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <stdio.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <stdlib.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <math.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <conio.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define MAXNUMBER 20
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//判断当前得到的解向量是否满足问题的解
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
bool place_queen(int x[],int k)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i=1;i<k;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if((x[i]==x[k]) || (abs(x[i]-x[k])==abs(i-k)))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//将结果简单信息打印到屏幕
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void output_queens(int x[],int n)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=1;i<=n;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("%3d",x[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//将结果详细信息写入文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void output_queens(FILE *fp,int number,int x[],int n)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fprintf(fp,"solution %d:",number);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=1;i<=n;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int j=1;j<=n;j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(j==x[i])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fprintf(fp,"1 ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fprintf(fp,"0 ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fprintf(fp,"");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fprintf(fp,"");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/************************************************************************
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* n后问题求解
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* input : n, the number of queens
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* output : the vector of solution, X
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
************************************************************************/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int n_queens(FILE *fp,int n,int x[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int nCount=0; //解个数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int k=1; //先处理第1个皇后
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
x[1]=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(k>0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
x[k]=x[k]+1;//在当前列加1的位置开始搜索
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(x[k]<=n && !place_queen(x,k)) //当前列位置是否满足条件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
x[k]=x[k]+1; //不满足,继续搜索下一列位置
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(x[k]<=n) //若存在满足条件的列
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(k==n)//是最后一个皇后,则得到一个最终解
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//break; //此处若break,则只能得到一个解
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
nCount++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
output_queens(x,n); //输出
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
output_queens(fp,nCount,x,n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else //否则,处理下一个皇后,即第 k+1 个皇后
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
k++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
x[k]=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else //若不存在满足条件的列,则回溯
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
x[k]=0; //第k个皇后复位为0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
k--; //回溯到前一个皇后
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return nCount;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
int n=8,x[MAXNUMBER]={0};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FILE *fp=fopen("8皇后问题的解.txt","w");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(fp==NULL)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("can not wirte file!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
exit(0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("the queens are placed on the coloums :");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//求解并写入文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int nCount=n_queens(fp,n,x);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("there are %d solutions!",nCount);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fclose(fp);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
getch();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
与其他方法对比:
#include <stdio.h>
#include <stdlib.h>
#define max 8
int queen[max], sum=0; /* max为棋盘最大坐标 */
void show() /* 输出所有皇后的坐标 */
{
int i;
printf("(");
for(i = 0; i < max; i++)
{
printf(" %d", queen[i]);
}
printf(")\n");
sum++;
}
int PLACE(int n) /* 检查当前列能否放置皇后 */
{
int i;
for(i = 0; i < n; i++) /* 检查横排和对角线上是否可以放置皇后 */
{
if(queen[i] == queen
|| abs(queen[i] - queen
) == (n - i))
{
return 1;
}
}
return 0;
}
void NQUEENS(int n) /* 回溯尝试皇后位置,n为横坐标 */
{
int i;
for(i = 0; i < max; i++)
{
queen
= i; /* 将皇后摆到当前循环到的位置 */
if(!PLACE(n))
{
if(n == max - 1)
{
show(); /* 如果全部摆好,则输出所有皇后的坐标 */
}
else
{
NQUEENS(n + 1); /* 否则继续摆放下一个皇后 */
}
}
}
}
int main()
{
NQUEENS(0); /* 从横坐标为0开始依次尝试 */
printf("%d", sum);
system("pause");
return 0;
}
8*8的棋盘上放八个皇后棋子,要求在纵行,竖行和斜行的八个方向上都不能有两个以上的皇后。1970和1971年,E.W.Dijkstra与N.Wirth曾用这个问题讲解程序设计技巧,淡然,像我这码农级程序员很难理解怎样应用到程序设计技巧中,现阶段只管理解问题的解决方法。
八皇后问题主要使用递归求解,然而如何减少递归次数呢?
在每次递归时,不必要所有的都检查过,例如若某列检查过,该列的其他格子就不在检查了,这个方法称为分支修剪。
程序中风别在横竖八个地方和斜方向15个地方做了标记,若有皇后则递归返回。本人觉得斜方向的标记控制和递归的调用时问题的精华。
C语言实现:
#include<stdio.h> #include<stdlib.h> #define N 8 int column[N+1]; int rup[2*N+1]; int lup[2*N+1]; int queen[N+1]={0}; int num; void backtrack(int); int main(void){ int i; num = 0;
for(i=1;i<=N;i++)
column[i] = 1; for(i=1;i<=2*N;i++) rup[i] = lup[i] =1; backtrack(1); return 0; } void showAnswer(){ int x,y; printf("\n解答%d\n",++num); for(y=1;y<=N;y++){ for(x=1;x<=N;x++){ if(queen[y]==x){ printf(" Q"); }else{ printf(" ."); } } printf("\n"); } } void backtrack(int i){ int j; if(i>N){ showAnswer(); }else{ for(j= 1;j<=N;j++){ if(column[j] == 1&&lup[i-j+N] == 1){ queen[i] = j; column[j] = rup[i+j] = lup[i-j+N] = 0; backtrack(i+1); column[j] = rup[i+j] = lup[i-j+N] = 1; } } } }
问题描述:
八皇后问题是大数学家高斯于1850年提出来的。该问题是在8×8的国际象棋棋盘上放置8个皇后,使得没有一个皇后能“吃掉”任何其他一个皇后,即没有任何两个皇后被放置在棋盘的同一行、同一列或同一斜线上。
要求:
编一个程序求出该问题的所有解。
算法思想:
回溯法
使用回溯算法求解的问题特征,求解问题要分为若干步,且每一步都有几种可能的选择,而且往往在某个选择不成功时需要回头再试另外一种选择,如果到达求解目标则每一步的选择构成了问题的解,如果回头到第一步且没有新的选择则问题求解失败。
该问题也可扩展到N后问题求解,只需修改程序main函数中的n值即可。
代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/************************************************************************
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* n后问题求解
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
************************************************************************/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <stdio.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <stdlib.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <math.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <conio.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define MAXNUMBER 20
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//判断当前得到的解向量是否满足问题的解
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
bool place_queen(int x[],int k)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i=1;i<k;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if((x[i]==x[k]) || (abs(x[i]-x[k])==abs(i-k)))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//将结果简单信息打印到屏幕
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void output_queens(int x[],int n)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=1;i<=n;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("%3d",x[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//将结果详细信息写入文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void output_queens(FILE *fp,int number,int x[],int n)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fprintf(fp,"solution %d:",number);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=1;i<=n;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int j=1;j<=n;j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(j==x[i])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fprintf(fp,"1 ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fprintf(fp,"0 ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fprintf(fp,"");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fprintf(fp,"");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/************************************************************************
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* n后问题求解
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* input : n, the number of queens
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* output : the vector of solution, X
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
************************************************************************/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int n_queens(FILE *fp,int n,int x[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int nCount=0; //解个数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int k=1; //先处理第1个皇后
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
x[1]=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(k>0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
x[k]=x[k]+1;//在当前列加1的位置开始搜索
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(x[k]<=n && !place_queen(x,k)) //当前列位置是否满足条件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
x[k]=x[k]+1; //不满足,继续搜索下一列位置
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(x[k]<=n) //若存在满足条件的列
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(k==n)//是最后一个皇后,则得到一个最终解
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//break; //此处若break,则只能得到一个解
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
nCount++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
output_queens(x,n); //输出
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
output_queens(fp,nCount,x,n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else //否则,处理下一个皇后,即第 k+1 个皇后
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
k++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
x[k]=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else //若不存在满足条件的列,则回溯
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
x[k]=0; //第k个皇后复位为0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
k--; //回溯到前一个皇后
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return nCount;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
int n=8,x[MAXNUMBER]={0};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FILE *fp=fopen("8皇后问题的解.txt","w");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(fp==NULL)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("can not wirte file!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
exit(0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("the queens are placed on the coloums :");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//求解并写入文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int nCount=n_queens(fp,n,x);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("there are %d solutions!",nCount);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fclose(fp);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
getch();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
与其他方法对比:
#include <stdio.h>
#include <stdlib.h>
#define max 8
int queen[max], sum=0; /* max为棋盘最大坐标 */
void show() /* 输出所有皇后的坐标 */
{
int i;
printf("(");
for(i = 0; i < max; i++)
{
printf(" %d", queen[i]);
}
printf(")\n");
sum++;
}
int PLACE(int n) /* 检查当前列能否放置皇后 */
{
int i;
for(i = 0; i < n; i++) /* 检查横排和对角线上是否可以放置皇后 */
{
if(queen[i] == queen
|| abs(queen[i] - queen
) == (n - i))
{
return 1;
}
}
return 0;
}
void NQUEENS(int n) /* 回溯尝试皇后位置,n为横坐标 */
{
int i;
for(i = 0; i < max; i++)
{
queen
= i; /* 将皇后摆到当前循环到的位置 */
if(!PLACE(n))
{
if(n == max - 1)
{
show(); /* 如果全部摆好,则输出所有皇后的坐标 */
}
else
{
NQUEENS(n + 1); /* 否则继续摆放下一个皇后 */
}
}
}
}
int main()
{
NQUEENS(0); /* 从横坐标为0开始依次尝试 */
printf("%d", sum);
system("pause");
return 0;
}
相关文章推荐
- 浅谈八皇后问题
- 浅谈八皇后问题
- 浅谈移动端的自适应问题——响应式、rem/em、利用Js动态实现移动端自适应
- 八皇后问题
- 浅谈约瑟夫问题,续
- 浅谈C/C++中内存分配、函数调用和返回值问题
- 八皇后问题
- 八皇后问题
- 多维算法思考(二):关于八皇后问题解法的探讨
- 数据结构---八皇后问题
- 浅谈凸优化问题中的Bregman迭代算法
- 浅谈MySQL备份字符集的问题
- python解决八皇后问题
- 八皇后问题:递归和非递归解法
- 《一个 Go 程序系统线程暴涨的问题》结论
- .NET.GC 浅谈.net托管程序中的资源释放问题
- 【面试准备】八皇后问题
- 二维数组实现八皇后问题
- 从八皇后问题到回溯法
- java float double精度为什么会丢失?浅谈java的浮点数精度问题