Java implements Queen n

Source: Internet
Author: User

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.

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;}@Overridepublic String toString() {return "(" + x + "," + y + ")";}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + x;result = prime * result + y;return result;}@Overridepublic 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.

Public class searcher {private int num = 0; Public searcher (INT num) {This. num = num;} public list <node> Search (node) {list <node> solution = new arraylist <node> (); If (qualified (node, solution )) {return solution;} else {return NULL ;}/ *** first traversal */private Boolean qualified (node, list <node> solution) {If (attach (node, solution) {return false;} solution. add (node); If (solution. size () = num) {// whether the return is true for the last node ;} // Obtain the node's subnode 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) {list <node> res = new arraylist <node> (); for (INT I = 0; I <num; I ++) {if (I = node. getx () {// filter the continue of the same row;} For (Int J = 0; j <num; j ++) {If (j = node. gety () {// filter the continue of the same column;} 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, list <node> nodes) {If (nodes. size () = 0) {// skip the first node return false;} For (node tempnode: nodes) {If (node. getx () = tempnode. getx () | // node in the same row. gety () = tempnode. gety () | // math in the same column. ABS (node. getx ()-tempnode. getx () = math. ABS (node. gety ()-tempnode. gety () {// return true;} return false ;}}

Main class for encoding Test

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: [(), ()]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.