C语言描述:
#include<stdio.h> int place[8]={0};//皇后位置 bool flag[8]={1,1,1,1,1,1,1,1};//定义列 bool d1[15]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; /*定义上对角线(共有15个对角线,因此定义一个长度为15的bool型数组,初值为1代表该对角线没有被皇后占领,若被皇后占领则赋值为0*/ bool d2[15]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} ;//定义下对角线 int number=0;//记录输出次数 void print()//定义输出函数 { int col,i,j; number++;//每调用一次输出函数number自加一次,记录摆放方法个数 printf("No.%2d\n",number); int table[8][8]={0};//设置一个8*8的棋盘 for (i=0;i<8;i++) { table[i][place[i]]=1;//将每一行皇后所在位置赋值为1 } for (i=0;i<8;i++) { for (j=0;j<8;j++) { printf("%d|",table[i][j]); }printf("\n"); } } int queen(int n )//定义递归回溯函数 { int col; for (col=0;col<8;col++) { if (flag[col]&&d1[n-col+7]&&d2[n+col])//判断皇后是否冲突 { place[n]=col;//放置皇后 flag[col]=false; d1[n-col+7]=false; d2[n+col]=false;//将该皇后所在的行、列、对角线设置为被占领 if(n<7) {queen(n+1);}//当行数小于7时;递归调用下一行 else{print();}//调用输出函数 flag[col]=true;//回溯 d1[n-col+7]=true; d2[n+col]=true; } } return number; } int main() { number=queen(0);//从第0行开始执行 printf("共有%d种摆放
方法",number);//输出方法的个数
return 0;}
汇编语言:
INCLUDE Irvine32.inc .data Queens dd 8 dup(0) Count dd 0 msg db 'The number of the way setting is:',0 sep db '===============',0 eight dd 8 seven dd 7 .code main PROC mov eax,0 push eax call eight_queen lea edx,msg call writestring mov eax,Count call writedec call crlf ; call print exit main ENDP eight_queen PROC push ebp mov ebp,esp pushad mov esi,[ebp+8] ;esi:line mov edi,0 ;edi:list ; mov eax,esi ; call writedec ; call crlf for_0: cmp edi,eight jge final push edi push esi call Check cmp eax,1 jne for_0_end mov edx,offset Queens ;edx:Queens mov [edx+esi*4],edi cmp esi,seven ; mov eax,edi ; call writedec ; call crlf jne next inc Count call print mov eax,0 mov edx,offset Queens mov [edx+esi*4],eax jmp final next: mov eax,esi add eax,1 push eax call eight_queen mov edx,offset Queens mov eax,0 mov [edx+esi*4],eax for_0_end: inc edi jmp for_0 final: popad pop ebp ret 4 eight_queen ENDP Check PROC push ebp mov ebp,esp sub esp,4 pushad mov ecx,[ebp+8] ;ecx:line mov edx,[ebp+12] ;edx:list mov esi,0 ;esi:index for_0: cmp esi,ecx jge ret1 mov ebx,offset Queens ;ebx:Queens mov edi,[ebx+esi*4] ;edi:data cmp edx,edi je ret0 ; mov ebx,offset Queens mov edi,esi add edi,[ebx+esi*4] sub edi,ecx sub edi,edx cmp edi,0 je ret0 ; mov ebx,offset Queens mov edi,esi sub edi,[ebx+esi*4] sub edi,ecx add edi,edx cmp edi,0 je ret0 for_0_end: inc esi jmp for_0 ret1: mov eax,1 mov [ebp-4],eax jmp final ret0: mov eax,0 mov [ebp-4],eax jmp final final: popad mov eax,[ebp-4] add esp,4 pop ebp ret 8 Check ENDP print PROC push ebp mov ebp,esp pushad mov esi,0 ;esi:line for_0: cmp esi,eight jge final mov edi,0 ;edi:list for_1: mov edx,offset Queens ;edx:Queens cmp edi,[edx+esi*4] jge next mov eax,0 call writedec for_1_end: inc edi jmp for_1 next: mov al,'#' call writechar mov edx,offset Queens ;edx:Queens mov edi,[edx+esi*4] inc edi for_2: cmp edi,eight jge for_0_end mov eax,0 call writedec for_2_end: inc edi jmp for_2 for_0_end: call crlf inc esi jmp for_0 final: lea edx,sep call writestring call crlf popad pop ebp ret print ENDP END main