N Queen Problem description:
Place n queens on an n x n chessboard, so that every queen cannot attack other queens.
Typical Case of deep traversal with priority.
Program input:
Number of n (required> 4)
Any position on the board
Program output:
Board coordinates meeting problem requirements
The program code is as follows:
The Node class is used to encapsulate the Queen's board location information.
[Java]
Public class Node {
Private int x;
Private int y;
Public Node (int x, int y ){
This. x = x;
This. y = y;
}
Public int getX (){
Return x;
}
Public void setX (int x ){
This. x = x;
}
Public int getY (){
Return y;
}
Public void setY (int y ){
This. y = y;
}
@ Override
Public String toString (){
Return "(" + x + "," + y + ")";
}
@ Override
Public int hashCode (){
Final int prime = 31;
Int result = 1;
Result = prime * result + x;
Result = prime * result + y;
Return result;
}
@ Override
Public boolean equals (Object obj ){
If (this = obj)
Return true;
If (obj = null)
Return false;
If (getClass ()! = Obj. getClass ())
Return false;
Node other = (Node) obj;
If (x! = Other. x)
Return false;
If (y! = Other. y)
Return false;
Return true;
}
}
The Searcher class is used to query the N queen coordinates at the specified starting position.
[Java]
Public class Searcher {
Private int num = 0;
Public Searcher (int num ){
This. num = num;
}
Public List <Node> search (Node node ){
List <Node> solution = new ArrayList <Node> ();
If (qualified (node, solution )){
Return solution;
} Else {
Return null;
}
}
/**
* Sequential Traversal
*/
Private boolean qualified (Node node, List <Node> solution ){
If (attach (node, solution )){
Return false;
}
Solution. add (node );
If (solution. size () = num) {// whether it is the last Node
Return true;
}
// Obtain the subnode of the node
Boolean res = false;
For (Node child: obtainChild (node )){
If (qualified (child, solution )){
Res = true;
Break;
}
}
If (! Res ){
Solution. remove (node );
}
Return res;
}
Private List <Node> obtainChild (Node node ){
List <Node> res = new ArrayList <Node> ();
For (int I = 0; I <num; I ++ ){
If (I = node. getX () {// filter the same row
Continue;
}
For (int j = 0; j <num; j ++ ){
If (j = node. getY () {// filter the same column
Continue;
}
If (Math. abs (node. getX ()-I) = Math. abs (node. getY ()-j) {// filter the diagonal line;
Continue;
}
Res. add (new Node (I, j ));
}
}
Return res;
}
Private boolean attach (Node node, List <Node> nodes ){
If (nodes. size () = 0) {// skip the first node
Return false;
}
For (Node tempNode: nodes ){
If (node. getX () = tempNode. getX () | // same row
Node. getY () = tempNode. getY () | // Same Column
Math. abs (node. getX ()-tempNode. getX () = Math. abs (node. getY ()-tempNode. getY () {// diagonal line
Return true;
}
}
Return false;
}
} Www.2cto.com
Main class for encoding Test
[Java]
Public class Main {
Public static void main (String [] args ){
Searcher searcher = new Searcher (6 );
List <Node> res = searcher. search (new Node (2, 0 ));
System. out. println (res );
}
}
Print Output: [(), ()]