使用递归,做法和65题的找路径基本一致
public class Solution {
public int movingCount(int threshold, int rows, int cols)
{
boolean[] isvisted=new boolean[rows*cols];
for(boolean v:isvisted){
v=false;
}
int count=0;
count=dfs(threshold,rows,cols,0,0,isvisted);
return count;
}
public int dfs(int threshold,int rows,int cols,int row,int col,boolean[] isvisted){
if(row<0||col<0||row>=rows||col>=cols||isvisted[row*cols+col]==true||threshold<help(row)+help(col)){
return 0;
}
isvisted[row*cols+col]=true;
return 1+dfs(threshold,rows,cols,row+1,col,isvisted)
+dfs(threshold,rows,cols,row-1,col,isvisted)
+dfs(threshold,rows,cols,row,col+1,isvisted)
+dfs(threshold,rows,cols,row,col-1,isvisted);
}
public int help(int n){
int sum=0;
while(n>0){
sum+=n%10;
n=n/10;
}
return sum;
}
}
终于刷完啦,啊啊啊啊啊啊啊啊啊啊啊啊啊啊~~~~煎熬的一个周终于过去啦~~~~