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

相关推荐

09-26 10:54
浙江大学 运营
亲切的00后在笔试:我:逗你玩的,怎么还当真了
点赞 评论 收藏
分享
谁知道呢_:bro不如吃顿疯狂星期四
点赞 评论 收藏
分享
勇敢的90后想交流:我愿意付费上班,楼主你就安心字节待着吧,我是真的喜欢上班
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务