题解 | #矩阵中的路径#

机器人的运动范围

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

题目理解清楚:机器人从(0,0)出发,问可以走到多少格子,不是求一条路径的最大值而是所以可以走的路径中不重复的格子数

public class Solution {
    
    public int ans = 0;
    public int threshold = 0;
    public int rows = 0,cols = 0;
    public int[][] visit = new int[101][101];
    
    public void dfs(int i,int j,int count){
        if(i<0||j<0||i>=rows||j>=cols||visit[i][j] == 1)return;
        
        int temp = 0;
        String s = String.valueOf(i)+String.valueOf(j);
        for(int k = 0; k < s.length(); k++)temp+=s.charAt(k)-'0';
        if(temp >threshold)return;
        ans++;
         
        visit[i][j] = 1;
        dfs(i+1,j,count+1);
        dfs(i,j+1,count+1);
        dfs(i-1,j,count+1);
        dfs(i,j-1,count+1);
    } 
    public int movingCount(int threshold, int rows, int cols) {
        this.threshold = threshold;
        this.rows = rows;
        this.cols = cols;
        dfs(0,0,1);
        return ans;
    }
}
全部评论

相关推荐

asdasdasda...:19岁,不容易啊可能升个本会好点,现在学历歧视太严重了
点赞 评论 收藏
分享
废物一个0offer:认真的吗二本本科找人工智能岗位
点赞 评论 收藏
分享
CARLJOSEPH...:宝宝你戾气太大了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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