我有一個功能,應該檢查數獨板上某個座標的可能答案。然而,我只需要你關注變量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