The ninth day of algorithm review: Breadth-first search/Depth-first search--3

Table of contents

1, 01 matrix

 The solution is here:

2. Rotten oranges

1. Multi-source breadth first search

Ideas

Complexity analysis

1, 01 matrix

542. 01 Matrix - LeetCode https://leetcode.cn/problems/01-matrix/?plan=algorithms&plan_progress=gzwnnxs

 The solution is here:

01 Matrix - 01 Matrix - LeetCode https://leetcode.cn/problems/01-matrix/solution/01ju-zhen-by-leetcode-solution/

2. Rotten oranges

994. Rotting Oranges - LeetCode https://leetcode.cn/problems/rotting-oranges/?plan=algorithms&plan_progress=gzwnnxs

1. Multi-source breadth first search


Ideas

It is observed that for all rotten oranges, they are actually equivalent to nodes in the same layer in breadth-first search.

Assume that these rotten oranges are fresh at the beginning, and there is a rotten orange (we call it the super source point) that will turn these oranges into rotten in the next second, and the time of this rotten orange at the beginning is −1, then according to According to the breadth-first search algorithm, in the next minute, which is the 0th minute, this rotten orange will turn them all into rotten oranges, and then continue to expand outward, so in fact these rotten oranges are nodes on the same layer. Then during breadth-first search, we can put all these rotten oranges into the queue for breadth-first search. In the end, the shortest time for each fresh orange to be rotten, dis[x][y], is actually based on this super source point. Results obtained from a breadth-first search starting from Rotten Oranges.

In order to confirm whether all fresh oranges are rotten, you can record a variable cnt to represent the number of fresh oranges in the current grid. During the breadth-first search, if there are fresh oranges that are rotten, cnt-=1. At the end of the search, if cnt is greater than 0, indicating that there are fresh oranges that are not rotten, return −1, otherwise just return the maximum value of the time when all fresh oranges were rotten, or you can change the value of rotten fresh oranges from 1 to 2. Finally, check whether the value in the grid is 1, that is, fresh oranges.

class Solution {
    int cnt;
    int dis[10][10];
    int dir_x[4]={0, 1, 0, -1};
    int dir_y[4]={1, 0, -1, 0};
public:
    int orangesRotting(vector<vector<int>>& grid) {
        queue<pair<int,int> >Q;
        memset(dis, -1, sizeof(dis));
        cnt = 0;
        int n=(int)grid.size(), m=(int)grid[0].size(), ans = 0;
        for (int i = 0; i < n; ++i){
            for (int j = 0; j < m; ++j){
                if (grid[i][j] == 2){
                    Q.push(make_pair(i, j));
                    dis[i][j] = 0;
                }
                else if (grid[i][j] == 1) cnt += 1;
            }
        }
        while (!Q.empty()){
            pair<int,int> x = Q.front();Q.pop();
            for (int i = 0; i < 4; ++i){
                int tx = x.first + dir_x[i];
                int ty = x.second + dir_y[i];
                if (tx < 0|| tx >= n || ty < 0|| ty >= m|| ~dis[tx][ty] || !grid[tx][ty]) continue;
                dis[tx][ty] = dis[x.first][x.second] + 1;
                Q.push(make_pair(tx, ty));
                if (grid[tx][ty] == 1){
                    cnt -= 1;
                    ans = dis[tx][ty];
                    if (!cnt) break;
                }
            }
        }
        return cnt ? -1 : ans;
    }
};

Complexity analysis

Time complexity: O(nm)
is the time to perform a breadth-first search, where n=grid.length, m=grid[0].length.

Space complexity: O(nm)
requires an additional dis array to record the shortest time for each fresh orange to rot, with a size of O(nm), and the maximum number of states stored in the queue during breadth-first search will not exceed nm, and a maximum of O(nm) space, so the final space complexity is O(nm).

Guess you like

Origin blog.csdn.net/m0_63309778/article/details/126753608