八皇后问题
来源:互联网 发布:金石计价软件破解 编辑:程序博客网 时间:2024/06/10 07:02
#if 0#include <stdio.h>#include <stdlib.h>// Eight Queens Problem :int count;int queen [10], column[20],left[20],right[20];/* 函数功能:打印出八皇后的结果*/void prt1(){ int j; printf("No.%d ",++count); for (j=1;j<=8;j++) {
printf("%3d",queen[j]);
} printf("\n");}/* 函数功能:筛选出符合条件的八皇后的位置 函数入口:第几行*/void try(int i){ int j; for (j=1;j<=8;j++) { if (column[j] && left[i-j+8] && right[i+j]) { queen[i]=j; column[j]=0; left[i-j+8]=0; right[i+j]=0; if (i<8)
{
try(i+1); } else {
prt1(); } column[j]=left[i-j+8]=right[i+j]=1; } }}int main(){ int i; for (i=1;i<=16;i++) { column[i]=left[i]=right[i]=1; } try(1); system("PAUSE"); return 0;}#endif