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

标签 c++ pass-by-reference

我正在实现数独解算器并使用 2D vector 并使用引用传递它,但是,当在主体末尾时,我尝试打印 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/

相关文章:

c - 将数组从文件 1 中的方法传递到文件 2 中的方法,而不在方法中使用额外参数

c - 传递/修改多维数组以在 C 中运行

c++ - 为什么我没有收到匹配的函数调用错误?

C++类返回对自身的引用

Javascript 泛化多个对象的函数 (Raphael JS)

java - 将内存分配给另一个 Java 类的对象

c++ - 是否有可能让 "named constructor"返回一个私有(private)构造的、不可移动的、不可复制的 std::optional<T>?

C++ opengl着色器编译失败

c++ - clang6 实现 std::可选 吗?

java - 如何在Java中对原语进行等效的通过引用传递