就一个起点,没必要回溯
int movingCount(int threshold, int rows, int cols){if(!rows || !cols)return 0;vector<vector<bool>> visited(rows, vector<bool>(cols, false));return backtrack(threshold, rows, cols, 0, 0, visited);}int backtrack(int threshold, int rows, int cols, int r, int c, vector<vector<bool>> & visited){int count = 0;if(canEnter(threshold, r, c) && r >= 0 && r < rows && c >= 0 && c < cols && !visited[r][c]){visited[r][c] = true;count = 1 + backtrack(threshold, rows, cols, r + 1, c, visited) + backtrack(threshold, rows, cols, r, c + 1, visited) + backtrack(threshold, rows, cols, r - 1, c, visited) + backtrack(threshold, rows, cols, r, c - 1, visited);}return count;}bool canEnter(int threshold, int r, int c){int cnt = 0;while(r != 0){cnt += r % 10;r = r / 10;}while(c != 0){cnt += c % 10;c = c / 10;}if(cnt <= threshold)return true;else return false;}
