8皇后Java递归
我正在为AP计算机科学课程的期末考试进行练习。无论如何,我刚刚学习了递归,我需要帮助将8皇后问题的几个方法简化为一个 我需要将第一行第八行方法合并为一个方法本身。该程序可以运行并打印所有92个解决方案,但它只需要简化8皇后Java递归,java,Java,我正在为AP计算机科学课程的期末考试进行练习。无论如何,我刚刚学习了递归,我需要帮助将8皇后问题的几个方法简化为一个 我需要将第一行第八行方法合并为一个方法本身。该程序可以运行并打印所有92个解决方案,但它只需要简化 public class NonAttacking { private static int [][] board = new int[8][8]; private static int count = 0; public void rowOne(int
public class NonAttacking
{
private static int [][] board = new int[8][8];
private static int count = 0;
public void rowOne(int y)
{
int x = 0;
if(onBoard(x, y) != -1)
{
if(validMove(x, y) == 0) //if the move is valid (0) then it puts a 1 at those coordinates and moves on to the next method (RowTwo)
{
board[x][y] = 1;
rowTwo(0);
}
board[x][y] = 0;
rowOne(y + 1);
}
}
public void rowTwo(int col)
{
int x = 1;
if(onBoard(x, col) != -1)
{
if(validMove(x, col) == 0)
{
board[x][col] = 1;
rowThree(0);
}
board[x][col] = 0;
rowTwo(col + 1);
}
}
public void rowThree(int col)
{
int x = 2;
if(onBoard(x, col) != -1)
{
if(validMove(x, col) == 0)
{
board[x][col] = 1;
rowFour(0);
}
board[x][col] = 0;
rowThree(col + 1);
}
}
public void rowFour(int col)
{
int x = 3;
if(onBoard(x, col) != -1)
{
if(validMove(x, col) == 0)
{
board[x][col] = 1;
rowFive(0);
}
board[x][col] = 0;
rowFour(col + 1);
}
}
public void rowFive(int col)
{
int x = 4;
if(onBoard(x, col) != -1)
{
if(validMove(x, col) == 0)
{
board[x][col] = 1;
rowSix(0);
}
board[x][col] = 0;
rowFive(col + 1);
}
}
public void rowSix(int col)
{
int row = 5;
if(onBoard(row, col) != -1)
{
if(validMove(row, col) == 0)
{
board[row][col] = 1;
rowSeven(0);
}
board[row][col] = 0;
rowSix(col + 1);
}
}
public void rowSeven(int col)
{
int row = 6;
if(onBoard(row, col) != -1)
{
if(validMove(row, col) == 0)
{
board[row][col] = 1;
rowEight(0);
}
board[row][col] = 0;
rowSeven(col + 1);
}
}
public void rowEight(int col)
{
int row = 7;
if(onBoard(row, col) != -1)
{
if(validMove(row, col) == 0)
{
board[row][col] = 1;
count++;
System.out.println(toString());
}
board[row][col] = 0;
rowEight(col + 1);
}
}
public static int validMove(int row, int col)
{
for (int i = 0; i < 8; i++)
{
if(onBoard(row, i) == 1) //checks side to side
{
return -1;
}
if(onBoard(i, col) == 1) //checks up and down
{
return -1;
}
}
//Check Diagonally
for(int i = 0; i < 8; i++)
{
if(onBoard(row - i, col - i) == 1) //checks up and left
{
return -1;
}
if(onBoard(row - i, col + i) == 1) //checks up and right
{
return -1;
}
if(onBoard(row + i, col - i) == 1) //checks down and left
{
return -1;
}
if(onBoard(row + i, col + i) == 1) //checks down and right
{
return -1;
}
}
return 0; //if it works
}
public static int onBoard(int row, int col)
{
if(row < 0 || col < 0 || row > 7 || col > 7) //checks if it is valid on the board
{
return -1;
}
return board[row][col]; //returns what the value is at the valid point
}
public String toString()
{
System.out.println(print());
String ans = "Solution: ";
ans += count;
return ans;
}
public static String print()
{
String sol = "Solution: " + count;
String result = "\n";
for (int row = 0; row < board.length; row++)
{
for(int column = 0; column < board[row].length; column++)
{
result += board[row][column] + "\t";
}
result += "\n";
}
return result;
}
}
`如果您只想用一个方法替换这8个方法,那么应该根据需要执行以下操作,使用rowN(0,0)开始
public void rowN(int n, int y)
{
if(onBoard(n, y) != -1)
{
if(validMove(n, y) == 0) //if the move is valid (0) then it puts a 1 at those coordinates and moves on to the next method (RowTwo)
{
board[n][y] = 1;
if(n<7) {
rowN(n+1, 0);
}
else {
count++;
System.out.println(toString());
}
}
board[n][y] = 0;
rowN(n, y + 1);
}
}
public void rowN(整数n,整数y)
{
如果(车载(n,y)!=-1)
{
如果(validMove(n,y)==0)//如果移动是有效的(0),那么它在这些坐标处放置一个1,并移动到下一个方法(第二行)
{
董事会[n][y]=1;
如果(nRowne方法仍然在底部被调用,那是怎么回事?我只是给rowN起个名字吗?任何你调用Rowne
的地方,你调用rowN
并使用n
参数1。我确实将代码行Rowne(y+1);
重命名为rowN(x,(y+1))
它只产生了这样的输出:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
我的错我已经编辑了答案,你现在应该需要从rowN(0,0,0)开始相反。希望这能解决这个问题。在这个答案中,你混合使用了x
和n
,但只要你把它们都改成同一个变量,这似乎是可行的。
public void rowN(int n, int y)
{
if(onBoard(n, y) != -1)
{
if(validMove(n, y) == 0) //if the move is valid (0) then it puts a 1 at those coordinates and moves on to the next method (RowTwo)
{
board[n][y] = 1;
if(n<7) {
rowN(n+1, 0);
}
else {
count++;
System.out.println(toString());
}
}
board[n][y] = 0;
rowN(n, y + 1);
}
}