Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
N皇后问题。从函数c+返回数组+; P>试图解决C++中的N-Quess问题。我遇到了这个错误,我会得到正确的答案,但是当返回数组指针时,我得到的是垃圾号而不是我的答案。奇怪的是,我使用递归来解决这个问题,我只在使用递归时得到垃圾编号(即使它正确地通过了递归)。数组在返回之前是完全正确的 int* Successor(int board[], int n) { bool currentlyLegal = isLegalPosition(board, n); int firstZero = -1; int templast; for(int i = 0; i < n; i++) { if(board[i] == 0) { firstZero = i; break; } } if(currentlyLegal) { if(firstZero != -1) { for(int i = 1; i < n; i++) { board[firstZero] = i; if(isLegalPosition(board, n) && i != lastNum) { return board; } } lastNum = -1; board[firstZero - 1] = 0; Successor(board, n); } else { templast = board[n - 1]; for(int i = board[n - 1]; i < n; i++) { board[n - 1] = i; if(isLegalPosition(board, n) && i != board[n-1] && i != lastNum) { return board; } } lastNum = templast; board[n - 1] = 0; Successor(board, n); } } else { if(firstZero != -1) { if(firstZero != 0) { for(int i = board[firstZero - 1]; i < n; i++) { board[firstZero - 1] = i; if(isLegalPosition(board, n) && i != board[firstZero - 1]) { return board; } } lastNum = -1; board[firstZero - 1] = 0; Successor(board, n); } else { board[0] = 1; return board; } } else { templast = board[n - 1]; for(int i = board[n - 1]; i < n; n++) { board[n - 1] = i; if(isLegalPosition(board, n) && i != board[n - 1] && i != templast) { return board; } } lastNum = templast; board[n - 1] = 0; Successor(board, n); } } } int*后继(int board[],int n){ bool currentlyllegal=isLegalPosition(板,n); int firstZero=-1; int模板; 对于(int i=0;i_C++_Arrays_Return_N Queens - Fatal编程技术网

N皇后问题。从函数c+返回数组+; P>试图解决C++中的N-Quess问题。我遇到了这个错误,我会得到正确的答案,但是当返回数组指针时,我得到的是垃圾号而不是我的答案。奇怪的是,我使用递归来解决这个问题,我只在使用递归时得到垃圾编号(即使它正确地通过了递归)。数组在返回之前是完全正确的 int* Successor(int board[], int n) { bool currentlyLegal = isLegalPosition(board, n); int firstZero = -1; int templast; for(int i = 0; i < n; i++) { if(board[i] == 0) { firstZero = i; break; } } if(currentlyLegal) { if(firstZero != -1) { for(int i = 1; i < n; i++) { board[firstZero] = i; if(isLegalPosition(board, n) && i != lastNum) { return board; } } lastNum = -1; board[firstZero - 1] = 0; Successor(board, n); } else { templast = board[n - 1]; for(int i = board[n - 1]; i < n; i++) { board[n - 1] = i; if(isLegalPosition(board, n) && i != board[n-1] && i != lastNum) { return board; } } lastNum = templast; board[n - 1] = 0; Successor(board, n); } } else { if(firstZero != -1) { if(firstZero != 0) { for(int i = board[firstZero - 1]; i < n; i++) { board[firstZero - 1] = i; if(isLegalPosition(board, n) && i != board[firstZero - 1]) { return board; } } lastNum = -1; board[firstZero - 1] = 0; Successor(board, n); } else { board[0] = 1; return board; } } else { templast = board[n - 1]; for(int i = board[n - 1]; i < n; n++) { board[n - 1] = i; if(isLegalPosition(board, n) && i != board[n - 1] && i != templast) { return board; } } lastNum = templast; board[n - 1] = 0; Successor(board, n); } } } int*后继(int board[],int n){ bool currentlyllegal=isLegalPosition(板,n); int firstZero=-1; int模板; 对于(int i=0;i

N皇后问题。从函数c+返回数组+; P>试图解决C++中的N-Quess问题。我遇到了这个错误,我会得到正确的答案,但是当返回数组指针时,我得到的是垃圾号而不是我的答案。奇怪的是,我使用递归来解决这个问题,我只在使用递归时得到垃圾编号(即使它正确地通过了递归)。数组在返回之前是完全正确的 int* Successor(int board[], int n) { bool currentlyLegal = isLegalPosition(board, n); int firstZero = -1; int templast; for(int i = 0; i < n; i++) { if(board[i] == 0) { firstZero = i; break; } } if(currentlyLegal) { if(firstZero != -1) { for(int i = 1; i < n; i++) { board[firstZero] = i; if(isLegalPosition(board, n) && i != lastNum) { return board; } } lastNum = -1; board[firstZero - 1] = 0; Successor(board, n); } else { templast = board[n - 1]; for(int i = board[n - 1]; i < n; i++) { board[n - 1] = i; if(isLegalPosition(board, n) && i != board[n-1] && i != lastNum) { return board; } } lastNum = templast; board[n - 1] = 0; Successor(board, n); } } else { if(firstZero != -1) { if(firstZero != 0) { for(int i = board[firstZero - 1]; i < n; i++) { board[firstZero - 1] = i; if(isLegalPosition(board, n) && i != board[firstZero - 1]) { return board; } } lastNum = -1; board[firstZero - 1] = 0; Successor(board, n); } else { board[0] = 1; return board; } } else { templast = board[n - 1]; for(int i = board[n - 1]; i < n; n++) { board[n - 1] = i; if(isLegalPosition(board, n) && i != board[n - 1] && i != templast) { return board; } } lastNum = templast; board[n - 1] = 0; Successor(board, n); } } } int*后继(int board[],int n){ bool currentlyllegal=isLegalPosition(板,n); int firstZero=-1; int模板; 对于(int i=0;i,c++,arrays,return,n-queens,C++,Arrays,Return,N Queens,递归调用不会返回任何内容。打开所有警告后,您应该会看到“并非所有控制路径都返回值”的内容。将递归调用更改为 return Successor( board, n ); 然后一个问题突然出现在我的脑海里。为什么他需要一个返回值,而函数只能返回board,据我所知,它不能返回。可能是一个踏脚的地方?

递归调用不会返回任何内容。打开所有警告后,您应该会看到“并非所有控制路径都返回值”的内容。将递归调用更改为

return Successor( board, n );

然后一个问题突然出现在我的脑海里。为什么他需要一个返回值,而函数只能返回
board
,据我所知,它不能返回。可能是一个踏脚的地方?