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);
    }
}
全部评论

相关推荐

豆泥🍀:同26届,加油,我也还没找到查看图片
点赞 评论 收藏
分享
05-14 20:34
门头沟学院 Java
窝补药贝八股:管他们,乱说,反正又不去,直接说680
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务