八皇后问题java代码
时间: 2023-08-31 07:19:08 浏览: 142
### 回答1:
这取决于您想要的代码复杂程度。但是,一般来说,在Java中解决八皇后问题的代码可以如下所示: public class EightQueens {
static int GRID_SIZE = 8; /*
* Returns a 2D array with all positions
* set to 0
*/
static int[][] initializeGrid() {
int grid[][] = new int[GRID_SIZE][GRID_SIZE];
for (int row = 0; row < GRID_SIZE; row++) {
for (int col = 0; col < GRID_SIZE; col++) {
grid[row][col] = 0;
}
}
return grid;
} /*
* Prints the given 2D array
*/
static void printGrid(int grid[][]) {
for (int row = 0; row < GRID_SIZE; row++) {
for (int col = 0; col < GRID_SIZE; col++) {
System.out.print(grid[row][col] + " ");
}
System.out.println();
}
} /*
* Checks if it is safe to place a queen at
* position (row,col).
*/
static boolean isSafe(int grid[][], int row, int col) {
int i, j; /*
* Check this row on left side
*/
for (i = 0; i < col; i++)
if (grid[row][i] == 1)
return false; /*
* Check upper diagonal on left side
*/
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (grid[i][j] == 1)
return false; /*
* Check lower diagonal on left side
*/
for (i = row, j = col; j >= 0 && i < GRID_SIZE; i++, j--)
if (grid[i][j] == 1)
return false; return true;
} /*
* Solves the N Queen problem using
* Backtracking.
*/
static boolean solveNQUtil(int grid[][], int col) {
/*
* base case: If all queens are placed
* then return true
*/
if (col >= GRID_SIZE)
return true; /*
* Consider this column and try placing
* this queen in all rows one by one
*/
for (int i = 0; i < GRID_SIZE; i++) {
/*
* Check if queen can be placed on
* board[i][col]
*/
if (isSafe(grid, i, col)) {
/*
* Place this queen in board[i][col]
*/
grid[i][col] = 1; /* recur to place rest of the queens */
if (solveNQUtil(grid, col + 1))
return true; /*
* If placing queen in board[i][col]
* doesn't lead to a solution, then
* remove queen from board[i][col]
*/
grid[i][col] = 0; // BACKTRACK
}
} /*
* If queen can not be place in any row in
* this column col then return false
*/
return false;
} /*
* This function solves the N Queen problem using
* Backtracking. It mainly uses solveNQUtil() to
* solve the problem. It returns false if queens
* cannot be placed, otherwise return true and
* prints placement of queens in the form of 1s.
* Please note that there may be more than one
* solutions, this function prints one of the
* feasible solutions.
*/
static void solveNQ() {
int grid[][] = initializeGrid(); if (solveNQUtil(grid, 0) == false) {
System.out.println("Solution does not exist");
return;
} printGrid(grid);
} public static void main(String args[]) {
solveNQ();
}
}
### 回答2:
八皇后问题是一个经典的算法问题,其目标是在8×8的棋盘上放置8个皇后,使得任意两个皇后不在同一行、同一列或同一对角线上。
下面是一个解决八皇后问题的Java代码:
```java
public class EightQueens {
private static final int SIZE = 8; // 棋盘大小
private static final int EMPTY = 0; // 空位置
private static final int QUEEN = 1; // 皇后位置
private int[][] board; // 棋盘
public EightQueens() {
board = new int[SIZE][SIZE];
}
// 打印棋盘
public void printBoard() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == QUEEN) {
System.out.print("Q ");
} else {
System.out.print(". ");
}
}
System.out.println();
}
System.out.println();
}
// 检查位置是否安全
private boolean isSafe(int row, int col) {
// 检查同一列
for (int i = 0; i < row; i++) {
if (board[i][col] == QUEEN) {
return false;
}
}
// 检查左上对角线
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) {
if (board[i][j] == QUEEN) {
return false;
}
}
// 检查右上对角线
for (int i = row, j = col; i >= 0 && j < SIZE; i--, j++) {
if (board[i][j] == QUEEN) {
return false;
}
}
return true;
}
// 回溯算法求解
private boolean solve(int row) {
if (row >= SIZE) {
return true;
}
for (int col = 0; col < SIZE; col++) {
if (isSafe(row, col)) {
board[row][col] = QUEEN;
if (solve(row + 1)) {
return true;
}
board[row][col] = EMPTY; // 回溯
}
}
return false;
}
public static void main(String[] args) {
EightQueens eightQueens = new EightQueens();
if (eightQueens.solve(0)) {
eightQueens.printBoard();
} else {
System.out.println("No solution found!");
}
}
}
```
以上代码使用了回溯算法解决八皇后问题。首先创建一个8×8的棋盘(二维数组)作为问题的解空间。然后定义了`isSafe`方法来检查位置是否安全,即是否满足不在同一行、同一列和同一对角线的条件。接着使用递归函数`solve`来尝试放置一个皇后,如果该位置安全,则继续递归放置下一个皇后,如果无法找到合适的位置,则进行回溯。最后在`main`函数中调用`solve`方法来求解问题,并打印出解。如果找到解,则输出棋盘的位置,否则输出"No solution found!"。
这段代码可以解决八皇后问题,并输出一个合法的棋盘位置。
### 回答3:
八皇后问题是一个经典的问题,要求在8x8的国际象棋棋盘上摆放8个皇后,使得每个皇后都不能互相攻击,即任意两个皇后不能处于同一行、同一列或同一对角线上。
以下是一个用Java语言实现的解决八皇后问题的代码:
```java
public class EightQueens {
private static final int BOARD_SIZE = 8;
private static int[] queens; // 存储皇后所在列的位置
public static void main(String[] args) {
queens = new int[BOARD_SIZE];
solve(0); // 从第0行开始逐行放置皇后
}
private static void solve(int row) {
if (row == BOARD_SIZE) {
printSolution();
} else {
for (int col = 0; col < BOARD_SIZE; col++) {
if (isValid(row, col)) {
queens[row] = col;
solve(row + 1);
}
}
}
}
private static boolean isValid(int row, int col) {
for (int i = 0; i < row; i++) {
if (queens[i] == col || queens[i] - queens[row] == row - i || queens[i] - queens[row] == i - row) {
return false;
}
}
return true;
}
private static void printSolution() {
System.out.println("一个有效的解决方案:");
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
if (queens[row] == col)
System.out.print("Q ");
else
System.out.print(". ");
}
System.out.println();
}
System.out.println();
}
}
```
这段代码使用回溯法来解决八皇后问题。通过递归遍历每一行的每一个列,如果在当前位置放置皇后不会导致攻击,则将该位置视为一个解决方案的一部分,并进入下一行。如果所有行都放置了皇后,即找到一个有效解决方案,则将其打印出来。
最后,通过调用 `solve(0)` 来开始求解八皇后问题。程序会输出所有找到的解决方案。
请注意,这段代码只能找到一个解决方案,如果需要找到所有的解决方案,则需要进行一些修改。
阅读全文