기본적으로 n-queen 문제를 해결하는 기본 프로그램을 작성해야하지만 입력 한 번호가 11보다 큰 경우 세그먼트 화 오류가 발생합니다. .재귀 n-queen 프로그램에서 C++ 세그먼트 화 오류 오류 발생
온라인에서 읽은이 오류는 일반적으로 메모리를 처리 할 때 결함이있는 논리로 인해 발생하지만 일반적으로 잘못된 것을 파악하지 못하는 것 같습니다.
void generateBoard(int board[],int column,int length,int count)
{
if(column == 0 && board[0]<length) //prevents outputting the results infinitely
{
++board[0];
generateBoard(board, ++column, length, count);
}
else
{
bool lineNotFound = true;
int row = board[column];
while(lineNotFound && row < length)
{
++row; //temporary value for a column value candidate
lineNotFound = false;
for(int i = 0; i < column && !lineNotFound; ++i)
{
if(board[i] == row || (board[i]+column-i) == row || (board[i]-column+i) == row) // check diagonal and horizontal
{
lineNotFound = true;
}
else
{
board[column] = row;
}
}
}
if(column == length-1 && !lineNotFound) // at last column and valid board
{
output(board,length,++count);
generateBoard(board,column,length,count);
}
else if(!lineNotFound) // not at last column, but valid position found
{
generateBoard(board,++column,length,count);
}
else if(column != 0) //no valid columns, go back a step
{
board[column] = 0;
generateBoard(board,--column,length,count);
}
}
}
는 그 코드의 큰 덩어리입니다 실현,하지만 난 그게 문제의 아이디어를 얻을 모두를 게시하는 데 필요한 것 같아요.
아이디어가 있으십니까? : s
저는 프로그래밍 C++을 처음 사용하기 때문에 디버깅을 시작할 위치를 모르겠습니다.
세분화 오류가 발생하는 함수를 호출하는 데 사용하는 매개 변수 값을 쓸 수 있습니까? –
"프로그래밍 C++을 처음 사용하기 때문에 디버깅을 시작할 위치를 알 수 없습니다." 디버거에서 프로그램을 실행하여 시작하십시오! 그것으로 당신은 어떤 라인이 폭발하는지 알 수 있습니다. 그것은 무엇이 잘못되었는지 알아내는 데 필요한 정보를 제공해야합니다. –
int board [boardSize]; 채우기 (board, board + boardSize, 0); generateBoard (board, 0, boardSize, 0); 그래서이 경우 generateBoard (board, 0, 11,0); –