Java 如何使用递归回溯来解决8个皇后?
更新代码:我的最后一个问题是如何获得它,以便我的程序能够打印8x8板的所有92种解决方案,而不会有任何皇后互相攻击?到目前为止,我的代码只打印一个解决方案。例如,当我将其更改为4x4板时,我只有一个解决方案,而我认为应该有两个Java 如何使用递归回溯来解决8个皇后?,java,arrays,recursion,stack,call,Java,Arrays,Recursion,Stack,Call,更新代码:我的最后一个问题是如何获得它,以便我的程序能够打印8x8板的所有92种解决方案,而不会有任何皇后互相攻击?到目前为止,我的代码只打印一个解决方案。例如,当我将其更改为4x4板时,我只有一个解决方案,而我认为应该有两个 public class Queen{ public static void display(int[] board){ int n = board.length; for(int column = 0; column < n; co
public class Queen{
public static void display(int[] board){
int n = board.length;
for(int column = 0; column < n; column++){
for(int row = 0; row < n; row++){
if(board[column] == row)
System.out.print('Q');
else
System.out.print('x');
}
System.out.print('\n');
}
}
public static int[] solveQueens(int n){
int board[] = new int[n];
placeQueen(board,0);
return board;
}
private static boolean placeQueen(int[] board, int column){
int n = board.length;
if (n == column){
return true;
}else{
for (int row = 0; row < n; row++){
int i; //remove
for (i = 0; i < column; i++){ //for (int i)
if (board[i] == row || i - column == board[i] - row || column - i == board[i] - row){
break;
}
}
if (i == column){
board[column] = row;
if (placeQueen(board, column + 1))
return true;
}
}
}
return false;
}
public static void main(String args[]){
int finished[] = solveQueens(8);
display(finished);
}
}
旧代码:
我需要使用递归回溯来解决8皇后问题。n-皇后问题是一个难题,需要将n个国际象棋皇后放置在n×n棋盘上,以便没有皇后相互攻击
我需要编写一个publicsolvequeens(intn)
方法来解决nxn板的问题
我还需要编写一个私有递归placeQueen(board,column)
方法来尝试在指定列中放置queen
这是我目前的代码:
public class Queen{
public static int[] solveQueens(int n){
int board[] = new int[n];
int finished[] = placeQueen(board,0);
return finished;
}
private static int[] placeQueen(int[] board, int column){
int n = board.length;
int row = column;
if (n == column){
return board;
}else{
for (row = n; row < n; row++){
board[column] = row;
if (board[n] == 0 && board[n-1] == 0 && board[n+1] == 0){
board[n] = 1;
placeQueen(board, column+1);
}
}
for (row = n; row < n; row++){
board[row] = column;
if (board[n] == 0 && board[n-1] == 0 && board[n+1] == 0){
board[n] = 1;
placeQueen(board, column+1);
}
}
}
return board;
}
public static void main(String args[]){
int finished[] = solveQueens(8);
for (int item: finished){
System.out.print(item);
}
}
}
有没有关于如何设置我的8x8板以及如何放置皇后以避免它们相互攻击的解释?我在SolveQueen和placeQueen中做了一些更改:
public class Queen{
public static int[] solveQueens(int n){
int board[] = new int[n];
placeQueen(board,0); //it fills the board
return board;
}
private static boolean placeQueen(int[] board, int column){
int n = board.length;
int row;
if (n == column){
return true;
}else{
for (row = 0; row < n; row++){
int c;
for (c=0; c<column; c++){
if (board[c]==row || c-column==board[c]-row || column-c==board[c]-row){
//check if it is safe position (we know 2 queens couldn't place in a same column or row or diameter
break;
}
}
if (c==column){ //if previous loop didn't break...=> it is safe position
board[column]=row;
if (placeQueen(board, column+1)) //if it is possible to fill the rest of board //else: test the next row
return true;
}
}
}
return false;
}
public static void main(String args[]){
int finished[] = solveQueens(8);
for (int item: finished){
System.out.print(item);
}
}
}
公共类皇后{
公共静态int[]皇后(int n){
int board[]=新int[n];
placeQueen(板,0);//它填充板
返回板;
}
专用静态布尔placeQueen(int[]板,int列){
int n=电路板长度;
int行;
if(n==列){
返回true;
}否则{
用于(行=0;行
您有两个for循环,如for(row=n;row
。这些循环不会运行,因为条件从一开始就是假的。您的意思是row=0
?如果您不知道如何使用调试器(1)学习,则需要(2)在学习之前,坚持使用System.out.println()
语句来跟踪代码的执行情况,并通知您一些变量的值。对于这个难题,有很多解决方案板。最好在代码中添加注释,以便立即理解。@WillNess我已经更新了代码,我的问题是我想看到8x8板的所有92个解决方案,但我的代码只打印1个解决方案。您知道如何打印整个8x8板吗?例如,也可以打印8行的x?您可以更改主功能并添加显示功能:公共静态无效显示(int[]板){int n=board.length;for(int c=0;cThanks man!非常感谢!是的!在placeQueen函数中,在if(n==column){return true;}
中,您可以调用display方法,而不是return true,然后返回false!因此它显示了所有答案。。。
----jGRASP exec: java Queen
00000000
public class Queen{
public static int[] solveQueens(int n){
int board[] = new int[n];
placeQueen(board,0); //it fills the board
return board;
}
private static boolean placeQueen(int[] board, int column){
int n = board.length;
int row;
if (n == column){
return true;
}else{
for (row = 0; row < n; row++){
int c;
for (c=0; c<column; c++){
if (board[c]==row || c-column==board[c]-row || column-c==board[c]-row){
//check if it is safe position (we know 2 queens couldn't place in a same column or row or diameter
break;
}
}
if (c==column){ //if previous loop didn't break...=> it is safe position
board[column]=row;
if (placeQueen(board, column+1)) //if it is possible to fill the rest of board //else: test the next row
return true;
}
}
}
return false;
}
public static void main(String args[]){
int finished[] = solveQueens(8);
for (int item: finished){
System.out.print(item);
}
}
}