gpt4 book ai didi

c++ - 一个简单程序中的奇怪的引用传递问题

转载 作者:搜寻专家 更新时间:2023-10-31 02:11:44 25 4
gpt4 key购买 nike

我正在实现数独解算器并使用 2D vector 并使用引用传递它,但仍然在 main 结束时我尝试打印 2D vector ,它打印初始 2D vector 。

#include <iostream>
#include <vector>
#include <map>

using namespace std;

void display(vector<vector<int>>& _board) {
for (auto row: _board) {
for (auto col: row) {
cout << col << " ";
}
cout << endl;
}
}


bool isBoardSolved(vector<vector<int>>& _board) {
for (auto row: _board) {
for (auto col: row) { //style[1] of 2D vector traversal
if (col == 0) {
return false;
}
}
}
return true;
}

map<int, int> findOptions(vector<vector<int>>& _board, int _row, int _col) {
map<int, int> options;
int x, y;
for (int digit = 1; digit < 10; ++digit) {
options[digit] = 0; //state 0 means available as options
}
//col in a row
for (y = 0; y < 9; ++y) {
if (_board[_row][y] != 0) {
options[_board[_row][y]] = 1;
}
}
//row in a col
for (x = 0; x < 9; ++x) {
if (_board[x][_col] != 0) {
options[_board[x][_col]] = 1;
}
}
//in a rectangular 3*3 matrix
if (_row <= 2)
x = 0;
else if (_row > 2 && _row <= 5)
x = 3;
else
x = 6;
if (_col <= 2)
y = 0;
else if (_col > 2 && _col <= 5)
y = 3;
else
y = 6;



for (int i = x; i < x + 3; ++i) {
for (int j = y; j < y + 3; ++j) {
if (_board[i][j] != 0) {
options[_board[i][j]] = 1;
}
}
}
return options;

}

void solveBoard(vector<vector<int>>& _board) {
int row = 0;
int col = 0;
bool flag = false;
if (isBoardSolved(_board)) {
// cout << "Solved Sudoku Board" << endl;
// display(_board); //gives correct answer when I print it here
return;
}
else {
for (int x = 0; x < 9; ++x) { //not using the style[1] because I need explicit index of empty slot
flag = false;
for (int y = 0; y < 9; ++y) {
if (_board[x][y] == 0) {
row = x;
col = y;
flag = true;
break;
}
}
if (flag)
break;
}
}

auto options = findOptions(_board, row, col);
for (auto digit: options) {
if (digit.second != 1) {
_board[row][col] = digit.first;
solveBoard(_board);
}
}
_board[row][col] = 0;
}

int main(int argc, char *argv[]) {
vector<vector<int>> board(9, vector<int>(9, 0));

board[0][3] = 3;
board[0][6] = 2;
board[2][1] = 7;
board[2][2] = 8;
board[2][3] = 0;
board[2][4] = 6;
board[2][6] = 3;
board[2][7] = 4;
board[3][1] = 4;
board[3][2] = 2;
board[3][3] = 5;
board[3][4] = 1;
board[4][0] = 1;
board[4][1] = 0;
board[4][2] = 6;
board[4][6] = 4;
board[4][7] = 0;
board[4][8] = 9;
board[5][4] = 8;
board[5][5] = 6;
board[5][6] = 1;
board[5][7] = 5;
board[6][1] = 3;
board[6][2] = 5;
board[6][4] = 9;
board[6][6] = 7;
board[6][7] = 6;
board[7][3] = 7;
board[8][2] = 9;
board[8][5] = 5;

cout << "Given Sudoku Board" << endl;
display(board);
solveBoard(board);
cout << "Solved Sudoku Board" << endl;
display(board); //gives unchanged answer when i print it here
}

我做错了什么以及如何改正它。

当我尝试这个时:

void change(vector<vector<int>>& _b){
_b[0][1] = 99;
}
int main(){

vector<vector<int>> b(1, vector<int>(9, 1));
cout<<b[0][1]<<endl;
change(b);
cout<<b[0][1];
return 0;
}

这会显示 2D vector b 的更改值。

最佳答案

您没有在 solveBoard() 中正确退出递归调用堆栈。请注意 solveBoard() 的新 bool 函数签名被返回以指示调用链提前退出。还要注意三个不同的返回点,具体取决于您所在的位置。

bool solveBoard(vector<vector<int>>& _board) {
int row = 0;
int col = 0;
bool flag;
if (isBoardSolved(_board)) {
// cout << "Solved Sudoku Board" << endl;
// display(_board); //gives correct answer when I print it here
return true;
}
else {
for (int x = 0; x < 9; ++x) { //not using the style[1] because I need explicit index of empty slot
flag = false;
for (int y = 0; y < 9; ++y) {
if (_board[x][y] == 0) {
row = x;
col = y;
flag = true;
break;
}
}
if (flag)
break;
}
}

auto options = findOptions(_board, row, col);
for (auto digit : options) {
if (digit.second != 1) {
_board[row][col] = digit.first;
if (solveBoard(_board))
return true;
}
}
_board[row][col] = 0;
return false;
}

关于c++ - 一个简单程序中的奇怪的引用传递问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43150953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com