发信人: mathsprince@GZ() 
整理人: yangcs(2002-01-14 18:45:40), 站内信件
 | 
 
 
 标  题: Re: 八皇后问题的解(回溯法程序代码) 发信站: 网易虚拟社区 (Fri Jul 14 11:28:01 2000), 站内信件
  【 在 zhzhzzx (学海无涯) 的大作中提到: 】
 : 【 在 tabalulu (taba&lulu) 的大作中提到: 】
 : : 以前上学的时候,写8皇后程序的时候偷懒用最笨的算法,在8086上计算十皇后
 : : 的时候,我放了张纸条,说明计算机正在运行,然后去吃饭,吃完以后,才看到
 : : 
 :    .......
 我这也有一个程序,从网上当的,忘记作者是谁了。
 #include <graphics.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <dos.h>
 char n[3]={'0','0'};/*record the answer*/
 int a[8],b[15],c[24],i;
 int h[8]={127,177,227,277,327,377,427,477};/*every queens' row coordin 
 ate*/
 int l[8]={252,217,182,147,112,77,42,7};/*every queens' col coordinate  
 */
 void *arrow;
 void try(int i)
 {
 	int j;
 	for (j=1;j<=8;j++)
 	if (a[j-1]+b[i+j-2]+c[i-j+7]==3) /* if the rol[i] and cow[i] is null  
 */
 	{
 		a[j-1]=0;b[i+j-2]=0;c[i-j+7]=0;/*engross the row[i] */
 		putimage(h[i-1],l[j-1],arrow,COPY_PUT);/*display the queen icon */
 		delay(500);/* delay */
 		if(i<8)
 			try(i+1);
 		else /*output an answer */
 		{
 			n[1]++;
 			if (n[1]>'9')
 			{
 				n[0]++;
 				n[1]='0';
 			}
 			bar(160,300,490,340);/*diplay the no[n] answer */
 			outtextxy(275,300,n);
 			delay(3000);
 		}
 		a[j-1]=1;
 		b[i+j-2]=1;
 		c[i-j+7]=1;
 		putimage(h[i-1],l[j-1],arrow,XOR_PUT);/*expunction the queen,find th 
 e next answer continue */
 		delay(500);
 	}
 }
 int main(void)
 {
 	int gdrive=DETECT,gmode,errorcode;
 	unsigned int size;
 	initgraph(&gdrive,&gmode,"");
 	errorcode=graphresult();
 	if (errorcode!=grOk)
 	{
 		printf("Graphics error\n");exit(1);}
 		rectangle(50,5,100,40);
 		rectangle(60,25,90,33);
 		/*draw the crown */
 		line(60,28,90,28);line(60,25,55,15);
 		line(55,15,68,25);line(68,25,68,10);
 		line(68,10,75,25);line(75,25,82,10);
 		line(82,10,82,25);line(82,25,95,15);
 		line(95,15,90,25);
 		size=imagesize(52,7,98,38); arrow=malloc(size);
 		getimage(52,7,98,38,arrow);/*put the crown into buffer */
 		clearviewport();
 		settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4);
 		setusercharsize(3, 1, 1, 1);
 		setfillstyle(1,4);
 		for (i=0;i<=7;i++)
 			a[i]=1;
 		for (i=0;i<=14;i++)
 			b[i]=1;
 		for (i=0;i<=23;i++)
 			c[i]=1;
 		for (i=0;i<=8;i++)
 			line(125,i*35+5,525,i*35+5);/*draw the chessboard */
 		for (i=0;i<=8;i++)
 			line(125+i*50,5,125+i*50,285);
 		try(1);/*call the recursion function */
 		delay(3000);
 		closegraph();
 		free(arrow);
 }
 
 
 
 -- ※ 修改:.mathsprince 于 Jul 14 11:30:28 修改本文.[FROM: 210.75.57.219] ※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 210.75.57.219]
  | 
 
 
 |