地上有1个m行和n列的方格。1个机器人从坐标0,0的格子开始移动,每次只能向左,右,上,下4个方向移动1格,但是不能进入行坐标和列坐标的数位之和大于k的格子
能走1直走
走过的不在走
public class Solution {
int count=0;
public int movingCount(int threshold, int rows, int cols)
{
if(0>threshold || rows<=0 || cols<=0)
return count;
if(threshold ==0)
return 1;
int[][] A = new int[rows][cols]; // 默许是 0
movingCount(A,threshold,0,0,rows,cols);
return count;
}
public void movingCount(int[][] A,int k,int i,int j, int rows, int cols){
if(i<0 || i>= rows || j<0 || j>=cols)
return;
int s = getDigitSum(i,j);
if(s<=k){
if(A[i][j] ==0){
count++;
A[i][j] = 1;
movingCount(A,k,i,j+1,rows,cols);
movingCount(A,k,i,j-1,rows,cols);
movingCount(A,k,i-1,j,rows,cols);
movingCount(A,k,i+1,j,rows,cols);
}
}
}
public int getDigitSum(int a,int b){
return getDigitSum(a) + getDigitSum(b);
}
public int getDigitSum(int num){
int sum = 0;
while(num >0){
sum +=num%10;
num/=10;
}
return sum;
}
}