我有一个功能,应该检查数独板上某个坐标的可能答案。然而,我只需要你关注变量first
。由于某种原因,first
设置为false,我不知道为什么。为什么这个变量被设置为false?
功能:
void displayPossible(int board[][9], char input[], int &row, int &col)
{
bool first = true; // variable instantiated and set to true
cout << "First " << first << endl;
bool possible[9]; // I dont touch `first` at all
computeValues(board, possible, row, col); // between these two lines..
cout << "First " << first << endl; // by this point it is false. WHY!?
cout << endl;
cout << "Possible: ";
for(int i = 0; i < 9; i++)
cout << possible[i];
cout << endl;
cout << "First " << first << endl;
cout << "The possible values for '" << input << "' are: ";
// if I say 'first = true' right here, i get my expected outcome
for(int i = 0; i < 9; i++)
{
if(possible[i] && first == true)
{
first = false;
cout << i;
}
else if(possible[i] && first == false)
cout << ", " << i;
else
;
}
cout << endl;
}
输出:
First 1
First 0
Possible: 000010001
First 0
The possible values for 'd1' are: , 4, 8
计算值:
void computeValues(int board[][9], bool possible[], int row, int col)
{
for(int i = 0; i < 9; i++)
possible[i] = true;
for(int iRow = 0; iRow < 9; iRow++)
possible[board[iRow][col]] = false;
for(int iCol = 0; iCol < 9; iCol++)
possible[board[row][iCol]] = false;
for(int iRow = 0; iRow < 2; iRow++)
for(int iCol = 0; iCol < 2; iCol++)
possible[board[row/3*3 + iRow][col/3*3 + iCol]] = false;
if(board[row][col] != 0)
possible[board[row][col]] = true;
}
几乎肯定是因为'computeValues'有一个覆盖它不应该触及的内存的bug,并且这会影响'first',因为它位于'possible'旁边的堆栈上。但是没有'computeValues'的代码是不可能的。 – Jon
您可能会不小心将它覆盖在'computeValues'中,例如通过溢出'可能[]'。我们可以看到'computeValues'吗? – Rup
请您发布computeValues代码 – kol