스도쿠 퍼즐의 각 사각형을 재귀 적으로 검사하여 합법적으로 만들려면 다음과 같은 함수를 사용합니다. 실행했을 때 세그멘테이션 오류가 계속 발생하므로 어디에서 부러 졌는지 확인하기 위해 전체 체크를합니다. 어쨌든, 그것은 루프에 머무르며 끝나지 않고 계속 addSquare 함수를 계속해서 호출합니다. 어떻게 멈추게합니까? 루프 1 개 루프 2 ifstatement2 루프 3 ifstatement3 loop1은 ... 130964 년 이후는 분할 고장이 (코어 덤프 "라고까지 :재귀 함수의 루프가 무한 이유는 무엇입니까?
터미널에서 실행bool DoTheWork::addSquare(int& depth)
{
depth++;
cout << depth << endl;
if(board.checkZeroes()==false){ //if the game is won, return true
cout << "ifstatement1" << endl;
return true;
}
else {
for(int i = 0; i < 10; i++) {
cout << "loop1" << endl;
for(int j = 0; j < 10; j++) {
cout << "loop2" << endl;
if(this->board.getSquare(i,j)==0) { //go through each
cout << "ifstatement2" << endl;
for(int k = 1; k < 10; k++) {
cout << "loop3" << endl;
//try each number in that square for legality
board.setSquare(i,j,k);
//set that square to the number you are currently on
if(board.isLegal()==false) {
cout << "ifstatement3" << endl;
board.unsetSquare(i,j);
}
//if the board is not legal for that number, unset that square
if(addSquare(depth)==true) {
cout << "ifstatement4" << endl;
return true;
}
//recursive function, if method is true then it will return true
board.unsetSquare(i,j);
}
}
}
}
}
return false;
} // bool DoTheWork::addSquare(int& depth)
, 그것은 다음을 인쇄) "
"ifstatement3 "이후의 숫자는 깊이가 증가 할 때마다 1 씩 증가합니다. checkZeroes 포함
이아래 기능 :
bool Board::checkZeroes()
{
bool zeroPresent = false;
//assume there are no zeroes, easier for coding
for(int i=0; i<9; i++) {
for(int j=0; j<9; j++) {
if(theBoard[i][j] == 0){
//go through each value of theBoard, if any are 0 return true
zeroPresent = true;
}
}
}
return zeroPresent;
} // int Board::checkZeroes()
문제를 복제하기에 충분한 코드가 필요합니다. 실제 출력물을 보는 것도 도움이 될 것입니다. –
분할 오류가 발생하면 'cout'이 플러시되지 않을 수 있습니다. * 전체 출력을 볼 수 없습니다. – zmbq
@zmbq 그는 모든'cout' 문에'endl'을 가지고 있고,'endl'은 내뿜습니다. –