给定一个阈值threshold,机器人运动的范围是当前运动的坐标点的各个 位上的数字相加小于等于 threshold
public class Solution {
int count = 0;
public int movingCount(int threshold, int rows, int cols){
int[][] nums = new int[rows][cols];
helper(nums,threshold,0,0);
return count;
}
public int helper(int[][] nums,int threshold,int row,int cols){
if(row<0 || row > nums.length - 1 || cols<0 || cols > nums[0].length - 1 || nums[row][cols] == 1){
return 0;
}
if(sumBitNum(row) + sumBitNum(cols) > threshold){
return 0;
}
nums[row][cols] = 1;
count++;
helper(nums,threshold,row + 1,cols);
helper(nums,threshold,row - 1,cols);
helper(nums,threshold,row,cols + 1);
helper(nums,threshold,row,cols - 1);
return count;
}
public int sumBitNum(int num){
int sum = 0;
while(num>0){
sum += num%10;
num = num / 10;
}
return sum;
}
}