欢迎来到天天文库
浏览记录
ID:41033660
大小:38.50 KB
页数:3页
时间:2019-08-14
《八皇后的编程实现问题》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、#include#include#defineTRUE1#defineFALSE0intnqueens(int*a,intn){inti,j,top,conflict;if(n<=0)returnFALSE;top=-1;i=0;do{conflict=FALSE;for(j=0;j2、3、top+1-j==i-a[j]4、5、top+1-j==a[j]-i)conflict=TRUE;if(conflict==FALSE){a[++top]6、=i;if(top==n-1)returnTRUE;i=0;}else{while(i==n-1&&top>=0)i=a[top--];i++;}}while(i7、",i,a[i]);printf("Q8、");for(j=a[i]9、+1;j10、");printf("");}}voidmain(){int*a,n;printf("pleaseenteranintegerasthesizeoftheproblem:=");scanf("%d",&n);if(n>0)a=(int*)malloc(n*sizeof(int));else{printf("Error!invalidinput!11、n");return;}if(nqueens(a,n)==TRUE)printsolution(a,n);elseprin12、tf("sorry!thereisnosolutionfpr%dqueensproblem!",n);free(a);}
2、
3、top+1-j==i-a[j]
4、
5、top+1-j==a[j]-i)conflict=TRUE;if(conflict==FALSE){a[++top]
6、=i;if(top==n-1)returnTRUE;i=0;}else{while(i==n-1&&top>=0)i=a[top--];i++;}}while(i7、",i,a[i]);printf("Q8、");for(j=a[i]9、+1;j10、");printf("");}}voidmain(){int*a,n;printf("pleaseenteranintegerasthesizeoftheproblem:=");scanf("%d",&n);if(n>0)a=(int*)malloc(n*sizeof(int));else{printf("Error!invalidinput!11、n");return;}if(nqueens(a,n)==TRUE)printsolution(a,n);elseprin12、tf("sorry!thereisnosolutionfpr%dqueensproblem!",n);free(a);}
7、",i,a[i]);printf("Q
8、");for(j=a[i]
9、+1;j10、");printf("");}}voidmain(){int*a,n;printf("pleaseenteranintegerasthesizeoftheproblem:=");scanf("%d",&n);if(n>0)a=(int*)malloc(n*sizeof(int));else{printf("Error!invalidinput!11、n");return;}if(nqueens(a,n)==TRUE)printsolution(a,n);elseprin12、tf("sorry!thereisnosolutionfpr%dqueensproblem!",n);free(a);}
10、");printf("");}}voidmain(){int*a,n;printf("pleaseenteranintegerasthesizeoftheproblem:=");scanf("%d",&n);if(n>0)a=(int*)malloc(n*sizeof(int));else{printf("Error!invalidinput!
11、n");return;}if(nqueens(a,n)==TRUE)printsolution(a,n);elseprin
12、tf("sorry!thereisnosolutionfpr%dqueensproblem!",n);free(a);}
此文档下载收益归作者所有