8 퀸즈 문제를 코딩하는 데 문제가 있습니다. 내가 그것을 해결하는 데 도움이되는 수업을 코딩했는데, 어떤 이유로, 나는 잘못된 것을하고있다. 나는 어떤 일이 일어나기로되어 있는지 이해합니다.8 재발로 인한 비 공격 퀸즈 알고리즘
또한 재귀를 사용하여 해결해야하지만 읽은 백 트랙킹을 사용하는 방법에 대한 단서가 없으므로 위치가 합법적인지 여부를 확인하는 데 메소드에서 사용했습니다.
내 보드는 8 행 8 열 등 String [] [] board = { { "O", "O"...
등입니다. 개념적으로 잘못된 것이 있거나 심각한 자바 실수를하는 경우 다음과 같이 말하십시오. D 감사!
public void solve() {
int Queens = NUM_Queens - 1;
while (Queens > 0) {
for (int col = 0; col < 8; col++) {
int row = -1;
boolean c = false;
while (c = false && row < 8) {
row ++;
c = checkPos (row, col);
}
if (c == true) {
board[row][col] = "Q";
Queens--;
}
else
System.out.println("Error");
}
}
printBoard();
}
// printing the board
public void printBoard() {
String ret = "";
for (int i = 0; i < 8; i++) {
for (int a = 0; a < 8; a++)
ret += (board[i][a] + ", ");
ret += ("\n");
}
System.out.print (ret);
}
// checking if a position is a legitimate location to put a Queen
public boolean checkPos (int y, int x) {
boolean r = true, d = true, u = true, co = true;
r = checkPosR (y, 0);
co = checkPosC (0, x);
int col = x;
int row = y;
while (row != 0 && col != 0) { //setting up to check diagonally downwards
row--;
col--;
}
d = checkPosDD (row, col);
col = x;
row = y;
while (row != 7 && col != 0) { //setting up to check diagonally upwards
row++;
col--;
}
d = checkPosDU (row, col);
if (r = true && d = true && u = true && co = true)
return true;
else
return false;
}
// checking the row
public boolean checkPosR (int y, int x) {
if (board[y][x].contentEquals("Q"))
return false;
else if (board[y][x].contentEquals("O") && x == 7)
return true;
else //if (board[y][x].contentEquals("O"))
return checkPosR (y, x+1);
}
// checking the column
public boolean checkPosC (int y, int x) {
if (board[y][x].contentEquals("Q"))
return false;
else if (board[y][x].contentEquals("O") && y == 7)
return true;
else //if (board[y][x].contentEquals("O"))
return checkPosR (y+1, x);
}
// checking the diagonals from top left to bottom right
public boolean checkPosDD (int y, int x) {
if (board[y][x].contentEquals("Q"))
return false;
else if (board[y][x].contentEquals("O") && (x == 7 || y == 7))
return true;
else //if (board[y][x].contentEquals("O"))
return checkPosR (y+1, x+1);
}
// checking the diagonals from bottom left to up right
public boolean checkPosDU (int y, int x) {
if (board[y][x].contentEquals("Q"))
return false;
else if (board[y][x].contentEquals("O") && (x == 7 || y == 0))
return true;
else //if (board[y][x].contentEquals("O"))
return checkPosR (y-1, x+1);
}
}
행마다 여왕이 하나만있을 수 있으므로 보드를'int [8] '로 표현하십시오. 여기서 각 항목은 행의 여왕의 열 위치입니다. 그것은 많은 일을 단순화합니다. – NPE
출력/오류는 무엇입니까? – Aashray
코드 관련 문제가 있습니까? 예상되는 출력은 무엇이며 실제 출력은 무엇입니까? – Jayamohan