Problem Description:
Put eight queens on the 8x8 chess, so that they can not attack each other, that is, any two queens can not be in the same row, the same column or the same slash, ask how many kinds of pendulum.
Programming:
1, one-dimensional array a[17], the array is divided into three paragraphs, the first paragraph a[0] used to mark the completion of the eight Queen placement; the second paragraph a[1,8] is used to mark the column position with or without children, convenient to judge the column conflict; The third paragraph a[9,16] is used to mark the storage location.
2, the key algorithm recursive judgment position, Eightqueens.
3, diagonal position mutually exclusive judgment, isdiagonal.
4, Output function, Printresult.
Algorithm Description:
1, first passed into the array A and the starting column variable I=1, to determine whether the marked A[0] is 1, 1 into 2, otherwise enter 3;
2, print array a[9,16];
3, test whether J is less than 8, under the end of recursion, or the detection of J-column is legal, legally enter 4, otherwise j + +, re-walk 3;
4, column labeled A[j] 1, storage location tag A[8+i] I,J combination of Location Code (I*10+J);
5. Judge if all queens have been placed, then mark as a[0] 1.
6, column number plus 1, recursion again.
7, clear the value of this recursive setting, in order to facilitate backtracking, re-look for other combinations, return 3.
1 /*2 function: Eight Queens3 time: 2014-08-31 14:34:564 */5#include <stdio.h>6 7 intc =1;//Simple Paging8 9 voidEightqueens (int*a,inti);//Recursive SolutionTen intIsdiagonal (int*a,intn);//Judging whether the same diagonal One voidPrintresult (int*A);//Output Results A - intMainvoid) - { the inta[ -]; - inti; - - //Initialize the board + for(i=0; i< -; i++) - { +A[i] =0; A } at - //Solution Method -Eightqueens (A,1); - - return 0; - } in - voidEightqueens (int*a,inti) to { + intJ; - the if(a[0]==1) * { $ //Output ResultsPanax Notoginseng Printresult (a); - } the Else + { A for(j=1; j<9; J + +) the { + if(A[j]! =1&& isdiagonal (A, i*Ten+J))//non-peers, non-identical columns, no longer diagonal lines. - { $A[J] =1; $a[8+i] = i*Ten+J; - if(i = =8) a[0] =1; - //Recursive the if(i<=8) - {WuyiEightqueens (A, i+1); the } -A[J] =0; Wua[8+i] =0; -a[0] =0; About } $ } - } - - } A + //Judging whether the same diagonal the intIsdiagonal (int*a,intN) - { $ intADCD, abit;//tagged row and column coordinates the intNDCD, NBit;//the coordinates of the rows to be judged the intTDCD, Tbit;//marked and pending row difference values the intK; the -NDCD = n/Ten; inNBit = n%Ten; the the for(k=9; k< -; k++) About { the if(A[k]! =0) the { the //separating rows and columns of tagged coordinates +ADCD = a[k]/Ten; -Abit = a[k]%Ten; the BayiTDCD = NDCD-ADCD;//Calculate the row difference value theTbit = Nbit-abit;//Calculate column Difference values the - if((tdcd-tbit) = =0|| (TDCD + tbit) = =0)//The absolute values of the row and column differences are equal in the same diagonal - { the return 0; the } the } the Else - { the return 1; the } the }94 return 1; the } the the //Output Results98 voidPrintresult (int*a) About { - intm;101 102printf"Scheme%d: \ n", c);103 for(m=9; m< -; m++)104 { theprintf"a[%d]\t", A[m]);106 }107printf"\ n");108 109C++; the 111 if(c%Ten==0){ the GetChar ();113 } the}
View Code
C language Learning to start, many are not too familiar with, have not seen the algorithm, just by their own want to write, relatively rough, as for the optimization of such as stay with the late bar.
C-language Learning-eight queens question