Sword Finger Offer 13. Robot's range of motion (C++) DFS+backtracking

There is a square with m rows and n columns on the ground, from coordinates [0,0] to coordinates [m-1,n-1]. A robot starts to move from the grid of coordinates [0, 0]. It can move to the left, right, up, and down one grid at a time (cannot move outside the grid), and cannot enter the sum of row coordinates and column coordinates greater than k lattice. For example, when k is 18, the robot can enter the square [35, 37] because 3+5+3+7=18. But it cannot enter square [35, 38] because 3+5+3+8=19. How many grids can the robot reach?

Example 1:

输入:m = 2, n = 3, k = 1
输出:3

Example 2:

输入:m = 3, n = 1, k = 0
输出:1

prompt:

1 <= n,m <= 100
0 <= k <= 20

Problem-solving ideas:

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
According to the structure and connectivity of the reachable solutions, the easy-to-extract robot can access all the reachable solutions just by moving to the right and down.

Inside a triangle: all connected, easy to prove;
connected place of two triangles: if the solution in a triangle is reachable, it must be connected with the triangle on the left or above (that is, intersect), that is, the robot must be able to walk into it from the left or above triangle.

Insert picture description here

Depth-first traversal of DFS ideas

Insert picture description here
Visited in Java/C++ code is an auxiliary matrix, and in Python it is Set.

class Solution {
    
    
public:
    int movingCount(int m, int n, int k) {
    
    
        vector<vector<bool>> visited(m, vector<bool>(n, 0));//标记当前单元格,走过表示ture
        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;//终止条件:
        // 当 ① 行列索引越界 或 ② 数位和超出目标值 k 或 ③ 当前元素已访问过 时,返回 0 ,代表不计入可达解。
        visited[i][j] = true;
        //计算当前元素的 下、右 两个方向元素的数位和,(i + 1, j)在(i , j)下方
        //i + 1, j, (i + 1) % 10 != 0 ? si + 1 : si - 8, sj,的原因,行列索引 i加1了,而 j不变,因此继续用sj
        // i, j + 1, si, (j + 1) % 10 != 0 ? sj + 1 : sj - 8  同理可得
        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);
    }
};

Complexity analysis:

Let the number of rows and columns of the matrix be M and N respectively.

Time complexity O(MN): In the worst case, the robot traverses all cells of the matrix. At this time, the time complexity is O(MN).
Space complexity O(MN): In the worst case, the indexes of all cells of the matrix are stored in Set visited, using O(MN) extra space.

Insert picture description here

Author: jyd
link: https: //leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/solution/mian-shi-ti-13-ji-qi-ren -de-yun-dong-fan-wei-dfs-b/
Source: LeetCode (LeetCode)
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/qq_30457077/article/details/114984660