题解 | #机器人的运动范围#

机器人的运动范围

http://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8

    public int movingCount(int threshold, int rows, int cols) {
        boolean[][] isV = new boolean[rows][cols];
        int num =  dfs(0,0,isV,threshold,rows,cols);
        return num;
    }
    public int dfs(int i,int j,boolean[][] isV,int threshold,int rows,int cols){
        if(i < 0 || i >= rows || j < 0 || j >= cols || zhuhe(i,j) > threshold || isV[i][j]){
            return 0;
        }
        isV[i][j] = true;
       int num = dfs(i+1,j,isV,threshold,rows,cols) + 
                 dfs(i-1,j,isV,threshold,rows,cols) +
                 dfs(i,j+1,isV,threshold,rows,cols) +
                 dfs(i,j-1,isV,threshold,rows,cols) +
                  1;
        return num;
    }
    public int zhuhe(int i,int j){
        int num = 0;
        while(i != 0){
           num = num + i % 10;
            i = i / 10;
       }
        while(j != 0){
           num = num + j % 10;
            j = j / 10;
       }
        return num;
    }
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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