Sword Finger Offer Problem Solution——13. The robot's range of motion

Topic link

Title description:

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:

Input: m = 2, n = 3, k = 1
Output: 3

Example 2:

Input: m = 3, n = 1, k = 0
Output: 1

prompt:

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

Problem-solving ideas

  At the beginning, I did not understand the meaning of the question, and directly double-looped through the two-dimensional array, which would match the result set 1, and the result was of course an error! After thinking about it, the reason is simple. Requirements of the subject is horizontal and vertical coordinates of the digital sum is less than equal to k , that is, when k is greater than, stop there, even if there are future digital sum of k less than or equal horizontal and vertical coordinates can not be considered in the results, one can imagine the moat , What we are looking for is a connected area, a city~ If we
  want to understand this, we can determine whether to use the BFS or DFS method. Let me talk about BFS, why can I use BFS? Wouldn't it cross the line? We all know that BFS uses a queue. In a word, the elements in the queue are in connected regions, and there may be multiple connected regions in a two-loop two-dimensional array.

BFS

class Solution {
    
    
public:
    int movingCount(int m, int n, int k) {
    
    
        if(n <= 0 || m <= 0)
            return 0;
        if(k == 0)
            return 1;
        
        int dx[4] = {
    
    0, 1, 0, -1};
        int dy[4] = {
    
    1, 0, -1, 0};
        vector<vector<int>> visited(m , vector<int>(n, 0));
        visited[0][0] = 1;

        queue< pair<int,int> > record;
        record.push(make_pair(0,0));
        int result = 1;
        
        while(!record.empty()){
    
    
            auto [x, y] = record.front();
            record.pop();

            for(int i = 0; i < 4; i++){
    
    
                int newX = x + dx[i];
                int newY = y + dy[i];
                if(newX >= m || newY >= n || newX < 0 || newY < 0 || visited[newX][newY] == 1 || !helper(newX, newY, k))
                    continue;
                
                visited[newX][newY] = 1;
                result++;
                record.push(make_pair(newX, newY));
            }
        }

        return result;
    }
private:
    bool helper(int x, int y, int k){
    
    
        int result = 0;
        while(x > 0){
    
    
            result += x % 10;
            if(result > k)
                return false;
            x /= 10;
        }
        while(y > 0){
    
    
            result += y % 10;
            if(result > k)
                return false;
            y /= 10;
        }

        return true;
    }
};


DFS

class Solution {
    
    
public:
    int movingCount(int m, int n, int k) {
    
    
        if(n <= 0 || m <= 0)
            return 0;
        if(k == 0)
            return 1;
        
        vector<vector<int>> visited(m, vector<int>(n,0));
        this->visited = visited;
        return dfs(0, 0, m, n, k);
    }
private:
    vector<vector<int>> visited;
    bool helper(int x, int y, int k){
    
    
        int result = 0;
        while(x > 0){
    
    
            result += x % 10;
            if(result > k)
                return false;
            x /= 10;
        }
        while(y > 0){
    
    
            result += y % 10;
            if(result > k)
                return false;
            y /= 10;
        }

        return true;
    }

    int dfs(int x, int y, int m, int n, int k){
    
    
        if(x >= m || y >= n || visited[x][y] == 1 || !helper(x, y, k))
            return 0;

        visited[x][y] = 1;
        return dfs(x + 1, y, m, n, k) + dfs(x, y + 1, m, n, k) + 1;
    }
};

If there are mistakes or not rigorous, please correct me, thank you very much.
My blog: http://breadhunter.gitee.io

Guess you like

Origin blog.csdn.net/weixin_40807714/article/details/105400942