/** * */ Packageunit1;/** * @author * @versioncreation Time: 2015-10-30 02:55:24 class Description*/ Public classeightqueensnotrecursive {Private Static Final BooleanAVAILABLE =true; Private intSquares = +, Norm = squares-1; Private intPositioninrow[] =New int[squares]; Private intp =-1; Private Boolean[] rows =New Boolean[squares]; Private Boolean[] column =New Boolean[squares]; Private Boolean[] leftdiagonal =New Boolean[2 * squares-1]; Private Boolean[] rightdiagonal =New Boolean[2 * squares-1]; Private Static intHowmany = 0; Publiceightqueensnotrecursive () {//The initialization work for the//column,leftdiagonal,rigthdiagonal. for(inti = 0; i < squares; i++) {Rows[i]=AVAILABLE; Column[i]=AVAILABLE; Positioninrow[i]=-1; } for(inti = 0; I < 2 * SQUARES-1; i++) {Leftdiagonal[i]=AVAILABLE; Rightdiagonal[i]=AVAILABLE; } } Public voidPrintresults (int[] columns) { intRow, col; System.out.println ("The Eight Queens question" + Howmany + "Method of Solution"); System.out.print ("The result of the eight Queens question is:"); for(inte:columns) {System.out.print (e); } System.out.println ("\ n the specific diagram as shown:"); for(row = 0; row < squares; row++) { for(col = 0; col < squares; col++) { if(col = =Positioninrow[row]) {System.out.print ("@"); } Else{System.out.print ("*"); }} System.out.println (); } System.out.println (); } Public voidPutqueen () {intRow = 0, col; while(true) { for(col = p + 1; col < squares; col++) { if(Rows[row] = = AVAILABLE && Column[col] = =AVAILABLE&& Leftdiagonal[row + col] = =AVAILABLE&& rightdiagonal[row-col + norm] = =AVAILABLE) { Break; } } //Found the place where the queen can be placed in the current line . if(Col <squares) {Rows[row]= !AVAILABLE; Column[col]= !AVAILABLE; Leftdiagonal[row+ Col] =!AVAILABLE; Rightdiagonal[row-Col + norm] =!AVAILABLE; Positioninrow[row]=Col; P=Col; } Else//if the current line doesn't have a queen, then backtrack { if(Row > 0)//to the previous line{row--; P=Positioninrow[row]; Rows[row]=AVAILABLE; COLUMN[P]=AVAILABLE; Leftdiagonal[row+ P] =AVAILABLE; Rightdiagonal[row-p + norm] =AVAILABLE; Positioninrow[row]=-1; Continue; } Else { Break; } } if(Row = = Squares-1) {Howmany+ = 1; Printresults (Positioninrow); P=Positioninrow[row]; Rows[row]=AVAILABLE; COLUMN[P]=AVAILABLE; Leftdiagonal[row+ P] =AVAILABLE; Rightdiagonal[row-p + norm] =AVAILABLE; Positioninrow[row]=-1; Continue; } Else{row++; P=-1; Continue; } } } Public Static voidMain (String args[]) {eightqueensnotrecursive Eightqueens=Neweightqueensnotrecursive (); Eightqueens.putqueen (); System.out.println ("The Queen problem has a total of" + Howmany + "solution"); }}
/** * */ Packageunit1;/** * @author * @versioncreation Time: 2015-10-30 02:41:57 class Description*/ Public classeightqueensrecursive {Private Static Final BooleanAVAILABLE =true; Private intSquares = 8, Norm = squares-1; Private intPositioninrow[] =New int[squares]; Private Boolean[] column =New Boolean[squares]; Private Boolean[] leftdiagonal =New Boolean[2 * squares-1]; Private Boolean[] rightdiagonal =New Boolean[2 * squares-1]; Private Static intHowmany = 0; Publiceightqueensrecursive () {//The initialization work for the//column,leftdiagonal,rigthdiagonal. for(inti = 0; i < squares; i++) {Column[i]=AVAILABLE; Positioninrow[i]=-1; } for(inti = 0; I < 2 * SQUARES-1; i++) {Leftdiagonal[i]=AVAILABLE; Rightdiagonal[i]=AVAILABLE; } } Public voidPrintresults (int[] columns) { intRow, col; System.out.println ("The Eight Queens question" + Howmany + "Method of Solution"); System.out.print ("The result of the eight Queens question is:"); for(inte:columns) {System.out.print (e); } System.out.println ("\ n the specific diagram as shown:"); for(row = 0; row < squares; row++) { for(col = 0; col < squares; col++) { if(col = =Positioninrow[row]) {System.out.print ("@"); } Else{System.out.print ("*"); }} System.out.println (); } System.out.println (); } Public voidPutqueen (introw) { //If a workable solution has been obtained before for(inti = 0; i < squares; i++) { if(Row > Squares-1) Break; if(Column[i] = = AVAILABLE && Leftdiagonal[row + i] = =AVAILABLE&& rightdiagonal[row-i + norm] = =AVAILABLE) {Positioninrow[row]=i; Column[i]= !AVAILABLE; Leftdiagonal[row+ i] =!AVAILABLE; Rightdiagonal[row-i + norm] =!AVAILABLE; if(Row < Squares-1) {putqueen (row-W); } Else{Howmany+ = 1; Printresults (Positioninrow); } Column[i]=AVAILABLE; Leftdiagonal[row+ i] =AVAILABLE; Rightdiagonal[row-i + norm] =AVAILABLE; } } } Public Static voidMain (String args[]) {eightqueensrecursive Eightqueens=Neweightqueensrecursive (); Eightqueens.putqueen (0); System.out.println (The Queen problem found the "+ Howmany +" group solution altogether. "); }}
2, the eight Queens question--backtracking method