N-Queens
基本解法,使用二维数组,for循环两次,效率低
import java.util.*;
public class Queens {
private int chessBoard[][];
private int N;
Queens(int N) {
this.N = N;
this.chessBoard = new int[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
this.chessBoard[i][j] = 0;
}
}
}
private boolean is_attacked(int r, int c) {
for (int i = 0; i < N; i++) {
if (this.chessBoard[r][i] == 1) return true;
if (this.chessBoard[i][c] == 1) return true;
for (int j = 0; j < N; j++) {
if (i + j == r + c && this.chessBoard[i][j] == 1) return true;
else if (i - j == r - c && this.chessBoard[i][j] == 1) return true;
}
}
return false;
}
public boolean N_Queen(int queens_left) {
if (queens_left == 0) return true;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (!this.is_attacked(i, j)) {
this.chessBoard[i][j] = 1;
if (N_Queen(queens_left - 1)) {
return true;
} else {
this.chessBoard[i][j] = 0;
}
}
}
}
return false;
}
public void printChessBoard() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print(this.chessBoard[i][j]+" ");
}
System.out.print("\n");
}
}
public static void main(String args[]) throws Exception {
Scanner s = new Scanner(System.in);
int N = Integer.valueOf(s.nextLine());
Queens queen = new Queens(N);
if(queen.N_Queen(N)) {
System.out.println("YES");
queen.printChessBoard();
}
else {
System.out.println("NO");
}
}
}
改进后使用一维数组存储棋盘信息,数组下标表示列,对应元素表示行。
import java.util.*;
public class Queens {
private int chessBoard[];
private int N;
Queens(int N) {
this.N = N;
this.chessBoard = new int[N];
}
private boolean is_attacked(int c) {
for (int i = 0; i < c; i++) {
if (this.chessBoard[i] == this.chessBoard[c]) return true;
if (this.chessBoard[i] + i == this.chessBoard[c] + c) return true;
if (this.chessBoard[i] - i == this.chessBoard[c] - c) return true;
}
return false;
}
public boolean N_Queen(int x) {
if (x == N) return true;
for (int i = 0; i < N; i++) {
this.chessBoard[x] = i;
if (!this.is_attacked(x)) {
if(N_Queen(x + 1)){
return true;
}
}
}
return false;
}
public void printChessBoard() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i == this.chessBoard[j]) {
System.out.print("1 ");
} else {
System.out.print("0 ");
}
}
System.out.print("\n");
}
}
public static void main(String args[]) throws Exception {
Scanner s = new Scanner(System.in);
int N = Integer.valueOf(s.nextLine());
Queens queen = new Queens(N);
if (queen.N_Queen(0)) {
System.out.println("YES");
queen.printChessBoard();
} else {
System.out.println("NO");
}
}
}
经典的八皇后问题
public class Queens8 {
private int chessBoard[];//数组下标表示列c,内存元素表示行r
private int N;
private int solution;
Queens8(int N) {
this.chessBoard = new int[N];
this.N = N;
this.solution = 0;
}
private boolean is_attacked(int c) {
for (int i = 0; i < c; i++) {
if (this.chessBoard[i] == this.chessBoard[c]) return true;
if (this.chessBoard[i] + i == this.chessBoard[c] + c) return true;
if (this.chessBoard[i] - i == this.chessBoard[c] - c) return true;
}
return false;
}
public void N_Queen(int x) {
if (x == N) {
solution++;
return;
}
for (int i = 0; i < N; i++) {
this.chessBoard[x] = i;
if (!this.is_attacked(x)) {
N_Queen(x + 1);
}
}
}
public static void main(String args[]) throws Exception {
Queens8 queens = new Queens8(8);
queens.N_Queen(0);
System.out.print(queens.solution);
}
}