nQueen问题java实现

最近一个项目刚刚结束,稍稍清闲了下来。其实我对自己的编程技术一直不太自信,作为程序员,在大学期间竟然都没选修过算法设计,真可谓三脚猫功夫。

正因如此,我反而一直保留着一本算法设计的书(数据结构,高数啥的大学毕业的那年遗忘在宿舍了 - -!),近来随手翻翻,找了个问题(nQueen)自己练手,用java实现之,请各位轻拍。

问题简单描述下:在n*n的棋盘上放置n个棋子(Queen)使得同一列,行,斜线上有且仅有一个Queen。更多该问题的背景可以百度之。

 
 1 /**
 2  * 请调用静态方法NQueen.calc(int n)方法计算n皇后问题
 3  * @author Lovememo
 4  *  
 5  */
 6 public class NQueen {
 7     private int[][] chessBoard;
 8     private int succeedSum;      //记录解的个数
 9     
10     private void setQueen(int m, int n, boolean flag) {
11         if(flag == true) 
12             this.chessBoard[m][n] = 1;
13          else
14             this.chessBoard[m][n] = 0;
15     }
16     
17     private boolean isSolution() {
18         int sum = 0;
19         for(int i=0; i<this.chessBoard.length; i++) {
20             for(int j=0; j<this.chessBoard.length; j++) {
21                 sum += this.chessBoard[i][j];
22             }
23         }
24         if(sum == this.chessBoard.length)
25             return true;
26         else
27             return false;
28     }
29     
30     private boolean canBeQueen(int m, int n) {
31         for(int i=0; i<this.chessBoard.length; i++) {
32             for(int j=0; j<this.chessBoard.length; j++) {
33                 if(1 == this.chessBoard[i][j]) {
34                     //横坐标相同,纵坐标相同,或者在同一斜线上,则不能为Queen
35                     if(m==i || n==j || Math.abs(m-i) == Math.abs(n-j)) 
36                         return false;
37                 }
38             }
39         }
40         return true;
41     }
42     
43     //递归回溯计算
44     private void calculate(int startNum) {
45         int chessBoardLength = this.chessBoard.length;
46         if(startNum == chessBoardLength-1) { //设置跳出递归条件
47             for(int i=0; i<chessBoardLength; i++) {
48                 if(this.canBeQueen(startNum, i)) {
49                     this.setQueen(startNum, i, true);
50                     if(this.isSolution()) {
51                         this.succeedSum += 1;
52                         this.printChess();                        
53                     }
54                     this.setQueen(startNum, i, false);
55                     return;
56                 }
57             }
58             return;
59         }
60         
61         for(int i=0; i<chessBoardLength; i++) {
62             if(this.canBeQueen(startNum, i)) {
63                 this.setQueen(startNum, i, true);
64                 this.calculate(startNum + 1);
65                 this.setQueen(startNum, i, false);
66             }
67         }        
68     }    
69     
70     //将n皇后问题的一个解打印出来
71     private void printChess() {
72         for(int i=0; i<this.chessBoard.length; i++) {
73             for(int j=0; j<this.chessBoard.length; j++) {
74                 System.out.print(this.chessBoard[i][j] + " ");
75             }
76             System.out.println();
77         }
78         System.out.println();
79     }
80 
81     private NQueen(int n) {        
82         this.chessBoard = new int[n][n];
83         this.succeedSum = 0;
84     }
85     
86     public static void calc(int n) {
87         NQueen nq = new NQueen(n);
88         nq.calculate(0);
89         System.out.println(nq.succeedSum);
90     }
91 
92     public static void main(String[] args) {
93         NQueen.calc(8);
94         
95     }
96 }

 

posted @ 2013-03-24 12:24  lovememo  阅读(230)  评论(0编辑  收藏  举报