地上有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;
}
}