LeetCode(剑指 Offer)- 13. 机器人的运动范围

题目链接:点击打开链接

题目大意:

解题思路

相关企业

  • Facebook

AC 代码

  • Java
// 解决方案(1)
class Solution {

    private int row, col, count, limit;

    private boolean[][] path, no;

    private int[] calc;

    public int movingCount(int m, int n, int k) {
        limit = k;
        count = 0;
        row = m;
        col = n;
        path = new boolean[m][n];
        no = new boolean[m][n];
        calc = new int[m * n];
        dfs(0, 0);
        return count;
    }

    private void dfs(int i, int j) {
        if (i < 0 || i >= row || j < 0 || j >= col || path[i][j]) {
            return;
        }

        path[i][j] = true;

        if (cal(i) + cal(j) <= limit) {
            count++;
        } else {
            no[i][j] = true;
            // 此时站在限制位置, 必须马上回退, 这个最容易忘记
            return;
        }

        // 接下来 4 个 if 为的是不进限制位置
        if (i+1 < row && !no[i+1][j]) {
            dfs(i + 1, j);
        }

        if (i-1 >= 0 && !no[i-1][j]) {
            dfs(i - 1, j);
        }

        if (j+1 < col && !no[i][j+1]) {
            dfs(i, j + 1);
        }

        if (j-1 >= 0 && !no[i][j-1]) {
            dfs(i, j - 1);
        }
    }

    private int cal(int num) {
        int tnum = num;
        if (calc[tnum] != 0) {
            return calc[tnum];
        }
        int sum = 0;
        while (num != 0) {
            sum += num % 10;
            num /= 10;
        }
        calc[tnum] = sum;
        return sum;
    }
}

// 解决方案(2)
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);
    }
}

// 解决方案(3)
class Solution {
    public int movingCount(int m, int n, int k) {
        boolean[][] visited = new boolean[m][n];
        int res = 0;
        Queue<int[]> queue= new LinkedList<int[]>();
        queue.add(new int[] { 0, 0, 0, 0 });
        while(queue.size() > 0) {
            int[] x = queue.poll();
            int i = x[0], j = x[1], si = x[2], sj = x[3];
            if(i >= m || j >= n || k < si + sj || visited[i][j]) continue;
            visited[i][j] = true;
            res ++;
            queue.add(new int[] { i + 1, j, (i + 1) % 10 != 0 ? si + 1 : si - 8, sj });
            queue.add(new int[] { i, j + 1, si, (j + 1) % 10 != 0 ? sj + 1 : sj - 8 });
        }
        return res;
    }
}
  • C++
// 解决方案(1)
class Solution {
public:
    int movingCount(int m, int n, int k) {
        vector<vector<bool>> visited(m, vector<bool>(n, 0));
        return dfs(0, 0, 0, 0, visited, m, n, k);
    }
private:
    int dfs(int i, int j, int si, int sj, vector<vector<bool>> &visited, int m, int n, int k) {
        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, visited, m, n, k) +
                   dfs(i, j + 1, si, (j + 1) % 10 != 0 ? sj + 1 : sj - 8, visited, m, n, k);
    }
};

// 解决方案(2)
class Solution {
public:
    int movingCount(int m, int n, int k) {
        vector<vector<bool>> visited(m, vector<bool>(n, 0));
        int res = 0;
        queue<vector<int>> que;
        que.push({ 0, 0, 0, 0 });
        while(que.size() > 0) {
            vector<int> x = que.front();
            que.pop();
            int i = x[0], j = x[1], si = x[2], sj = x[3];
            if(i >= m || j >= n || k < si + sj || visited[i][j]) continue;
            visited[i][j] = true;
            res++;
            que.push({ i + 1, j, (i + 1) % 10 != 0 ? si + 1 : si - 8, sj });
            que.push({ i, j + 1, si, (j + 1) % 10 != 0 ? sj + 1 : sj - 8 });
        }
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/Dream_Weave/article/details/124322132