Sword finger offer 13 robot reachable range

topic

https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/submissions/

Ideas

1. Define an identity array mark which has been traversed
2. Recursively traverse from the left and right

 public int  m, n;
    public int[][] visited;
    public int movingCount(int m, int n, int k) {
        this.m = m;
        this.n = n;
        visited = new int[m][n];
        return dfs(0, 0,   k);
    }

    private int dfs(int i, int j,  int k) {
        if (getDigitsSum(i, j) > k || i > m - 1 || j > n - 1 || visited[i][j] == 1) {
            return 0;
        }
        visited[i][j] = 1;
        //向左边和右边深度递归
        return 1 + dfs(i + 1, j,  k) + dfs(i, j + 1,  k);  
    }

    private   int getDigitsSum(int a, int b) {
        int result = 0;
        while (a != 0) {
            result += a % 10;
            a /= 10;
        }
        while (b != 0) {
            result += b % 10;
            b /= 10;
        }
        return result;
    }

problem

What is the difference between depth-first and breadth-first?

Published 33 original articles · praised 37 · 110,000 views

Guess you like

Origin blog.csdn.net/hagle_wang/article/details/105175199