回溯做法,遇到一个问题,形参前面&,一开始并不知道作用,后来发现是将形参与实参地址绑定,形参变化,实参也跟着变。
代码:

class Solution {
private:
    vector<vector<int>> ret;
public:
    int check(int n){
        int sum = 0;
        while(n){
            sum += n%10;
            n = n/10;
        }
        return sum;
    }

    int sea(int threshold, int rows, int cols, int x, int y, vector<vector<int>> &m){
        if(threshold < (check(x) + check(y)) || x<0 || y<0 || x>rows-1 || y>cols-1 || ret[x][y] == 1){
            return 0;
        }
        ret[x][y] = 1;
        return 1 + sea(threshold, rows, cols, x-1, y, ret) + sea(threshold, rows, cols, x+1, y, ret) + sea(threshold, rows, cols, x, y-1, ret) + sea(threshold, rows, cols, x, y+1, ret);
    }

    int movingCount(int threshold, int rows, int cols) {
        ret = vector<vector<int>> (rows, vector<int>(cols,0));
        return sea(threshold, rows, cols, 0, 0, ret);
    }
};