欢迎来到天天文库
浏览记录
ID:57642752
大小:30.50 KB
页数:11页
时间:2020-08-29
《八皇后问题及解答.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、八皇后问题问题描述: 在一个8×8的棋盘里放置8个皇后,要求每个皇后两两之间不相冲突(在每一横列,竖列,斜列只有一个皇后)。求解:标 题: 八皇后问题的解(回溯法程序代码)发信站: 网易虚拟社区 (Fri Jul 14 10:06:52 2000), 站内信件以前上学的时候,写8皇后程序的时候偷懒用最笨的算法,在8086上计算十皇后的时候,我放了张纸条,说明计算机正在运行,然后去吃饭,吃完以后,才看到结果。前几天,刚好有空,所以重写了一次,以补当年的遗憾。#include "stdio.h"int attacked(i
2、nt *array,int position){ int flag=-1; float step; if(position==1) return flag; for(step=1.00;step3、4、step==position)return1;if(((*(array+(int)step)-*(array+position))/(step-position))==15、6、((*(array+(7、int)step)-*(array+position))/(step-position))==-1){flag=1;break;}}returnflag;}voidmain(void){intcountSum,queenSum,printCount,*queenArray,queenPosition=0;inttempArray[20]={6666,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};countSum=1;queenArray=tempArray;printf("inputyou8、queenSumhere:");scanf("%d",&queenSum);fflush(stdin);if(queenSum<4){printf("the%dqueen'ssumis0",queenSum);return;}for(;;){if(countSum=q9、ueenSum){ if(*(queenArray+countSum-1)10、enArray+countSum--)=0;++*(queenArray+countSum);}}}else++*(queenArray+countSum);}else++*(queenArray+(++countSum));}else{if(attacked(queenArray,countSum)==1){if(*(queenArray+countSum)>=queenSum){ if(*(queenArray+countSum-1)11、*(queenArray+countSum+1)=0;continue;}else{*(queenArray+countSum--)=0;if(*(queenArray+countSum)12、tCount=1;printCount<=queenSum;printCount++)printf("%3d%",*(queenArray+printCount));if(printCount>=queenSum) printf(""); if(*(queenArray+countSum)>=queenSum
3、
4、step==position)return1;if(((*(array+(int)step)-*(array+position))/(step-position))==1
5、
6、((*(array+(
7、int)step)-*(array+position))/(step-position))==-1){flag=1;break;}}returnflag;}voidmain(void){intcountSum,queenSum,printCount,*queenArray,queenPosition=0;inttempArray[20]={6666,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};countSum=1;queenArray=tempArray;printf("inputyou
8、queenSumhere:");scanf("%d",&queenSum);fflush(stdin);if(queenSum<4){printf("the%dqueen'ssumis0",queenSum);return;}for(;;){if(countSum=q
9、ueenSum){ if(*(queenArray+countSum-1)10、enArray+countSum--)=0;++*(queenArray+countSum);}}}else++*(queenArray+countSum);}else++*(queenArray+(++countSum));}else{if(attacked(queenArray,countSum)==1){if(*(queenArray+countSum)>=queenSum){ if(*(queenArray+countSum-1)11、*(queenArray+countSum+1)=0;continue;}else{*(queenArray+countSum--)=0;if(*(queenArray+countSum)12、tCount=1;printCount<=queenSum;printCount++)printf("%3d%",*(queenArray+printCount));if(printCount>=queenSum) printf(""); if(*(queenArray+countSum)>=queenSum
10、enArray+countSum--)=0;++*(queenArray+countSum);}}}else++*(queenArray+countSum);}else++*(queenArray+(++countSum));}else{if(attacked(queenArray,countSum)==1){if(*(queenArray+countSum)>=queenSum){ if(*(queenArray+countSum-1)11、*(queenArray+countSum+1)=0;continue;}else{*(queenArray+countSum--)=0;if(*(queenArray+countSum)12、tCount=1;printCount<=queenSum;printCount++)printf("%3d%",*(queenArray+printCount));if(printCount>=queenSum) printf(""); if(*(queenArray+countSum)>=queenSum
11、*(queenArray+countSum+1)=0;continue;}else{*(queenArray+countSum--)=0;if(*(queenArray+countSum)12、tCount=1;printCount<=queenSum;printCount++)printf("%3d%",*(queenArray+printCount));if(printCount>=queenSum) printf(""); if(*(queenArray+countSum)>=queenSum
12、tCount=1;printCount<=queenSum;printCount++)printf("%3d%",*(queenArray+printCount));if(printCount>=queenSum) printf(""); if(*(queenArray+countSum)>=queenSum
此文档下载收益归作者所有