나는 스도쿠 퍼즐을 취하고 해결하는 프로그램을 작성하려고합니다. 그러나,이 라인에서 StackOverflow의 오류로 실행 해요 :StackOverflow 오류가 발생했습니다
Move nMove = new Move(current.nextMove(current, sboard).i, current.nextMove(current, sboard).j);
은 이동이 유효한지 여부를 확인하는 방법 isLegal 있습니다. 이동이 유효하고 다음 이동이 유효하면 스택에 추가됩니다. 유효하지만 다음 이동이 유효하지 않은 경우 올바른 번호를 계속 검색해야합니다. 무엇이 원인인지 확실하지 않습니다.
import java.util.Stack;
public class Board {
Stack<Move> stack = new Stack<Move>();
int boardSize = 9;
public int[][] sboard = {{2,0,0,3,9,5,7,1,6},
{5,7,1,0,2,8,3,0,9},
{9,3,0,7,0,1,0,8,2},
{6,8,2,0,3,9,1,0,4},
{3,5,9,1,7,4,6,2,8},
{7,1,0,8,6,0,9,0,3},
{8,6,0,4,1,7,2,9,5},
{1,9,5,2,8,6,4,3,7},
{4,2,0,0,0,0,8,6,1}};
public Board() {
//for every cell in board:
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
//get the value of each cell
int temp = getCell(i,j);
//if cell is empty:
if (temp == 0) {
//print out location of cell
System.out.print ("("+i+", "+j+") ");
//guess values for that cell
solve(i, j);
}
}
}
}
//places a value into specified cell
public void setCell(int value, int row, int col) {
sboard[row][col] = value;
}
//returns value contained at specified cell
public int getCell(int row, int col) {
return sboard[row][col];
}
//if value is legal, continue
public boolean isLegal(int value, int row, int col) {
int r = (row/boardSize) * boardSize;
int c = (col/boardSize) * boardSize;
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
if (value == getCell(i, col) || value == getCell(row, j)) {
return false;
}
}
}
return true;
}
//guesses values for empty cells
public boolean solve(int i, int j) {
//set location as current
Move current = new Move(i, j);
Move nMove = new Move(current.nextMove(current, sboard).i, current.nextMove(current, sboard).j);
//guesses values 1 through 9 that are legal
for (int k = 1; k <= 9; k++) {
//if a legal value is found and the next move is possible:
if(isLegal(k, i, j) && solve(nMove.i, nMove.j)) {
//add current to stack
stack.push(current);
//enter the value k into the cell
setCell(k, i, j);
//print new value
System.out.print(sboard[i][j]+"\n");
//return as true
return true;
}
else if (stack.empty()){
}
//if next move is not possible
else if(!solve(nMove.i, nMove.j)){
//remove last "solved" location from stack
stack.pop();
//solve last location again
solve(stack.peek());
}
}
return false;
}
public void solve(Move m) {
solve(m.i, m.j);
}
public static void main(String[] args) {
Board b = new Board();
}
};
class Move {
int i, j;
public Move(int i, int j) {
this.i = i;
this.j = j;
}
public int i() { return i;}
public int j() { return j;}
public Move nextMove(Move current, int[][] sboard){
for (int i = current.i; i < 9; i++) {
for (int j = current.j; j < 9; j++) {
//get the value of each cell
int temp = sboard[i][j];
if (temp == 0) {
return new Move(i, j);
}
}
}
return current;
}
};
일반적으로 stackoverflow 오류는 종료되지 않는 재귀 함수로 인해 발생합니다. solve() 함수는 재귀 적으로 보인다 ... 기본 조건을 생각하고 종료해야 할 때/자신을 호출하는 것을 멈추어야한다. – mpen
당신은 확실히 당신의 생성자로부터'solve' 함수 호출을 옮기고 싶습니다. – Perception
그래, 아마 재귀 루프 때문일거야. 예외를 잡아 내고 ex.printStackTrace()를 호출하여 메소드 계층 구조를 살펴볼 수 있습니다. – oldSkool