八皇后问题
八皇后问题描述
在8×8格的国际象棋上摆放8个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上。
图解四皇后问题
由于八皇后问题解决太过于繁琐,因此我们问题简单化,利用四皇后演示解决步骤,并且完成代码实现。
八皇后代码实现
public class EightQueen {
//定义一个解的编号
private static int count = 0;
//定义棋盘的尺寸
private static final int SIZE = 4; //四皇后、八皇后等
public static void main(String[] args) {
int[][] arr = new int[SIZE][SIZE];
eightQueen(0,arr);
}
private static void eightQueen(int row, int[][] arr) {
if (row == SIZE){
count++;
System.out.println("这是第" + count + "种解法:");
print(arr);
}else {
int[][] newArr = copyOf(arr);
for (int col = 0;col < SIZE; col++){
if (noDangerous(row,col,newArr)){
//先清空之前同级别的可能
for (int c = 0; c < SIZE; c++){
newArr[row][c] = 0;
}
newArr[row][col] = 1;
eightQueen(row + 1,newArr);
}
}
}
}
private static boolean noDangerous(int row, int col, int[][] arr) {
//正上方
for (int r = row - 1; r >= 0; r--){
if (arr[r][col] == 1){
return false;
}
}
//左上方
for (int r = row - 1,c = col - 1; r >= 0 && c >= 0; r--,c--){
if (arr[r][c] == 1){
return false;
}
}
//右上方
for (int r = row - 1,c = col + 1; r >= 0 && c < SIZE; r--,c++){
if (arr[r][c] == 1){
return false;
}
}
return true;
}
private static int[][] copyOf(int[][] arr) {
int[][] newArr = new int[SIZE][SIZE];
for (int i = 0;i < SIZE; i++){
for (int j = 0;j < SIZE; j++){
newArr[i][j] = arr[i][j];
}
}
return newArr;
}
private static void print(int[][] arr) {
for (int i = 0;i < SIZE; i++){
for (int j = 0;j < SIZE; j++){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
运行结果截图