题目理解清楚:机器人从(0,0)出发,问可以走到多少格子,不是求一条路径的最大值而是所以可以走的路径中不重复的格子数
public class Solution {
public int ans = 0;
public int threshold = 0;
public int rows = 0,cols = 0;
public int[][] visit = new int[101][101];
public void dfs(int i,int j,int count){
if(i<0||j<0||i>=rows||j>=cols||visit[i][j] == 1)return;
int temp = 0;
String s = String.valueOf(i)+String.valueOf(j);
for(int k = 0; k < s.length(); k++)temp+=s.charAt(k)-'0';
if(temp >threshold)return;
ans++;
visit[i][j] = 1;
dfs(i+1,j,count+1);
dfs(i,j+1,count+1);
dfs(i-1,j,count+1);
dfs(i,j-1,count+1);
}
public int movingCount(int threshold, int rows, int cols) {
this.threshold = threshold;
this.rows = rows;
this.cols = cols;
dfs(0,0,1);
return ans;
}
}