LeetCode 5347. FIG grid so that at least a minimum cost valid path (BFS shortest path, hard)

1. Topic

Give you a grid map grid of mxn. Each grid has a grid number, corresponding to the direction of the grid from the next to go. grid[i][j]The numbers may be the following situations:

  • 1, the next step right away, that is, from you will grid[i][j]comegrid[i][j + 1]
  • 2. Next go to the left, that is, from you will grid[i][j]comegrid[i][j - 1]
  • 3, the next step to go down, that is, from you will grid[i][j]comegrid[i + 1][j]
  • 4, the next step to go up, that is, from you grid[i][j]walked grid[i - 1][j]
    there may be a note grid graph invalid numbers , as they may point to areas outside the grid.

At first, you will start from the grid (0,0) of the upper left corner. We define a valid path starting from the grid (0,0), corresponding to each step along the digital direction, culminating in the lower right corner of the grid (m - 1, n - 1 ) the end of the path. Effective path need not be the shortest path .

You can spend 1 cost = the cost of modifying a grid of numbers, but the numbers in each grid only be modified once .

Please return to let you have at least one valid trellis path of minimum cost .

Example 1:
Here Insert Picture Description

输入:grid = [[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]
输出:3
解释:你将从点 (0, 0) 出发。
到达 (3, 3) 的路径为: (0, 0) --> (0, 1) --> (0, 2) --> (0, 3) 
花费代价 cost = 1 使方向向下 --> (1, 3) --> (1, 2) --> (1, 1) --> (1, 0) 
花费代价 cost = 1 使方向向下 --> (2, 0) --> (2, 1) --> (2, 2) --> (2, 3) 
花费代价 cost = 1 使方向向下 --> (3, 3)
总花费为 cost = 3.

Example 2:
Here Insert Picture Description

输入:grid = [[1,1,3],[3,2,2],[1,1,4]]
输出:0
解释:不修改任何数字你就可以从 (0, 0) 到达 (2, 2)

Example 3:
Here Insert Picture Description

输入:grid = [[1,2],[4,3]]
输出:1

示例 4:
输入:grid = [[2,2,2],[2,2,2]]
输出:3

示例 5:
输入:grid = [[4]]
输出:0
 
提示:
m == grid.length
n == grid[i].length
1 <= m, n <= 100

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid
copyright of the collar button all network . Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

2. Problem Solving

  • BFS breadth-first search, shortest path
  • The arrow can go places all join the queue, and marked visited
  • Then the queue is removed, the reverse path to the four directions, and no way to add new access points to go
class Solution {
public:
    int minCost(vector<vector<int>>& grid) {
        int m = grid.size(), n= grid[0].size();
        int i, j, x = 0, y = 0, a, b, k, flip = 0, size;
        vector<vector<int>> dir = {{0,0},{0,1},{0,-1},{1,0},{-1,0}};
        vector<vector<bool>> visited(m,vector<bool>(n,false));
        queue<pair<int,int>> q;
        while(x>=0 && x<m && y>=0 && y<n && !visited[x][y])
        {
            q.push({x,y});
            visited[x][y] = true;
            i = x + dir[grid[x][y]][0];
            j = y + dir[grid[x][y]][1];
            x = i, y = j;
        }
        if(visited[m-1][n-1])
            return 0;
        while(!q.empty())
        {
            size = q.size();
            flip++;
            while(size--)
            {
                i = q.front().first;
                j = q.front().second;
                q.pop();
                for(k = 1; k <= 4; k++)
                {
                    x = i + dir[k][0];
                    y = j + dir[k][1];
                    while(x>=0 && x<m && y>=0 && y<n && !visited[x][y])
                    {
                        q.push({x,y});
                        visited[x][y] = true;
                        a = x + dir[grid[x][y]][0];
                        b = y + dir[grid[x][y]][1];
                        x = a, y = b;
                    }
                }
            }
            if(visited[m-1][n-1])
                return flip;
        }
        return flip;
    }
};

Here Insert Picture Description

Published 680 original articles · won praise 586 · views 180 000 +

Guess you like

Origin blog.csdn.net/qq_21201267/article/details/104598714