0.code
class Solution {
public int movingCount(int m, int n, int k) {
int[][] visit = new int[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
visit[i][j]=0;
}
}
return movingCountHelp(visit,0,0,m,n,k);
}
public int movingCountHelp(int[][] visit,int i,int j, int m, int n, int k){
if(i>=m||j>=n||visit[i][j]==1){
return 0;
}
visit[i][j]=1;
if(getSum(i)+getSum(j)>k){
return 0;
}
return 1+movingCountHelp(visit,i+1,j,m,n,k)+movingCountHelp(visit,i,j+1,m,n,k);
}
public int getSum(int ans){
int sum = 0;
while(true){
sum = sum+ans%10;
ans = ans/10;
if(ans==0){
break;
}
}
return sum;
}
}