开始时比较简单的想法就是直接将每个点的位相加,若小于等于阈值,则累计数++。
这种做法在方格的长宽小于10时是生效的,而当方格的长宽之一大于等于10后就会出问题。
简答举个例子,(0,10)位数和为1,假设阈值为1,机器人显然不能到达该点,但通过上面的逻辑判断则会将累计数++。
所以解决办法应该是模拟机器人的路径,统计机器人走过的所有点。
模拟机器人行动时,使用一个visited数组记录机器人走过的点,当走到一个点已经走过,或其位数和大于阈值时,则回溯。
否则,计数值++,将该点标记为已走过,递归调用运动函数,分别模拟向右走和向下走。

class Solution {
public:
    int movingCount(int threshold, int rows, int cols) {
        if(cols == 0 && rows == 0){
            return 0;
        }
        vector<bool> array(cols, false);
        vector<vector<bool>> visited(rows, array);
        int count = 0;
        countS(threshold, count, 0, 0, visited);
        return count;
    }
    void countS(int threshold, int &count, int row, int col, vector<vector<bool>>& visited){
        if(row > visited.size() - 1 || col > visited.at(0).size() - 1){
            return;
        }
        int num = 0;
        if(row == 100){
            num += 1;
        }
        else{
            num = num + row/10 + row%10;
        }
        if(col == 100){
            num += 1;
        }
        else{
            num = num + col/10 + col%10;
        }
        if(num <= threshold && !visited.at(row).at(col)){
            count++;
            visited.at(row).at(col) = true;
            countS(threshold, count, row + 1, col, visited);
            countS(threshold, count, row, col + 1, visited);
        }
    }
};