leetcode上看到的题解
机器人的运动范围
http://www.nowcoder.com/questionTerminal/6e5207314b5241fb83f2329e89fdecc8
class Solution {
int m,n,k;
boolean[][] visited;
public int movingCount(int threshold, int rows, int cols) {
if(k<0)
return 0;
this.m = rows;
this.n = cols;
this.k = threshold;
//记录每一个坐标是否走过
this.visited = new boolean[m][n];
return dfs(0,0,0,0);
}
/**
* 该方法表示以(i,j)为当前坐标可以到达的格子数
* i和j:表示当前机器人坐标;bottom,right:表示当前坐标的i位数和以及j位数和:例如当前(10,6):bottom=1;right=6;
*/
public int dfs(int i,int j,int bottom,int right){
if(i>=m || j>=n || bottom+right>k || visited[i][j])
return 0;
visited[i][j] = true;;
return 1+dfs(i+1,j,(i+1)%10==0?bottom-8:bottom+1,right)+dfs(i,j+1,bottom,(j+1)%10==0?right-8:right+1);
}
}