class Solution {
public:
int movingCount(int threshold, int rows, int cols)
{
int ** mark = new int * [rows]; //记录是否已经走过
for(int i = 0; i < rows; ++i){
mark[i] = new int[cols];
}
return DFS(0, 0, rows, cols, mark, threshold);
}
private:
int DFS(int x, int y, int rows, int cols, int** mark, int threshold){
if(x >= rows || y >= cols || mark[x][y] == 1 || numOfSum(x)+numOfSum(y) > threshold){
return 0;
}
mark[x][y] = 1;
return DFS(x+1, y, rows, cols, mark, threshold) + DFS(x, y+1, rows, cols, mark, threshold) + 1;
}
int numOfSum(int x){
int sum = 0;
while(x){
sum += x%10;
x /= 10;
}
return sum;
}
};
【day-9】剑指Offer-机器人的运动范围
最新推荐文章于 2024-08-11 23:10:13 发布
本文介绍了一种解决机器人运动范围问题的算法实现。通过深度优先搜索(DFS)遍历矩阵内的可达位置,并利用辅助函数计算坐标值的各位数之和,判断是否超出阈值。文章提供了完整的C++代码示例,帮助读者理解算法细节。
1729

被折叠的 条评论
为什么被折叠?



