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

相关推荐

仁者伍敌:牛子这些人还会点一个自动回复,boss都不带回复的
点赞 评论 收藏
分享
05-09 12:23
已编辑
华南理工大学 Java
野猪不是猪🐗:给他装的,双九+有实习的能看的上这种厂我直接吃⑨✌们拿它练练面试愣是给他整出幻觉了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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