机器人的运动范围--深度优先遍历dfs

题目描述

在这里插入图片描述

代码

class Solution {
    
    

        int m, n, k;
        boolean[][] visited;
        public int movingCount(int m, int n, int k) {
    
    
            this.m = m; this.n = n; this.k = k;
            this.visited = new boolean[m][n];
            return dfs(0, 0, 0, 0);
        }
        
        public int dfs(int i, int j, int si, int sj) {
    
    
            if(i >= m || j >= n || k < si + sj || visited[i][j]) return 0;
            visited[i][j] = true;
            return 1 + dfs(i + 1, j, (i + 1) % 10 != 0 ? si + 1 : si - 8, sj) 
                     + dfs(i, j + 1, si, (j + 1) % 10 != 0 ? sj + 1 : sj - 8);
        }

}

猜你喜欢

转载自blog.csdn.net/weixin_44809329/article/details/113914513
今日推荐