- package endual;
-
- public class Queen {
-
- private final int size = 8;
- private int[] location ;
- private int[] colsOccupied ;
- private int[] cross1Occuptied ;
- private int[] cross2Occuptied ;
- private static int count ;
-
-
-
- private static final int STATUS_OCCUPIED = 1 ;
- private static final int STATUS_OCCUPY_CANCELED = 0 ;
-
- public Queen() {
-
- this.location = new int[this.size];
- this.colsOccupied = new int[this.size];
- this.cross1Occuptied = new int[2*this.size];
- this.cross2Occuptied = new int[2*this.size];
- }
-
- public void printLocation() {
-
- System.out.println("以下是皇后在棋盘上的第" +count+"种方式的摆放");
- for (int i=0; i <this.size; i++) {
-
- System.out.println("行:" + i + " 列:" + this.location[i]) ;
- }
- }
-
-
- private boolean isOccupied(int i, int j) {
-
- if(this.colsOccupied[j] == 1) {
- return true ;
- }
-
- if(this.cross1Occuptied[i-j+this.size-1] == 1) {
- return true ;
- }
-
- if (this.cross2Occuptied[i+j] == 1) {
- return true ;
- }
-
- return false ;
- }
-
-
-
- private void setStatus(int i, int j, int flag){
-
- this.colsOccupied[j] = flag ;
- this.cross1Occuptied[i-j+this.size-1] = flag ;
- this.cross2Occuptied[i+j] = flag ;
-
-
- }
-
-
- public void place(int i) {
-
- for (int j=0; j < this.size; j++) {
-
- if (!this.isOccupied(i, j)) {
-
- this.location[i] = j ;
- this.setStatus(i, j, this.STATUS_OCCUPIED) ;
-
- if (i < this.size-1) {
- this.place(i+1) ;
-
- }else {
- this.count++ ;
- this.printLocation() ;
- }
-
- this.setStatus(i, j, STATUS_OCCUPY_CANCELED) ;
-
- }
-
- }
-
- }
-
- public void start() {
-
- this.place(0) ;
- }
-
- }
八皇后
原文:http://www.cnblogs.com/xuke123/p/5885766.html