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

相关推荐

码农索隆:卡学历都不行了,开始卡颜值了
点赞 评论 收藏
分享
难怪不开摄像头,全是简单的性格题,比大疆友善多了
NULL10086:今早上发的测评,我这还没做呢,官网上已经显示挂了
投递大疆等公司7个岗位
点赞 评论 收藏
分享
MinJerous:虽然我一直说 计算机不怎么卡学历 但是至少得一本
点赞 评论 收藏
分享
陆续:不可思议 竟然没那就话 那就我来吧 :你是我在牛客见到的最美的女孩
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-10 14:10
啊啊啊啊好幸福,妈妈是我找工作发疯前的一束光
黑皮白袜臭脚体育生:看了这篇帖子之后已经第一百次质问老妈,仍然没有得到我的老妈是老板的回答
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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