LeetCode 1020 飞地的数量[dfs] HERODING的LeetCode之路

Posted HERODING23

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 1020 飞地的数量[dfs] HERODING的LeetCode之路相关的知识,希望对你有一定的参考价值。


解题思路:
一道万金油的题目,可以用深度优先或者广度优先的方法去解决,这里只提供深度优先的代码。既然是找无法离开的陆地,那么不如把所有的能离开的陆地找到,然后剩下的都是无法离开的陆地。深度优先思路是从四周陆地开始找,通过四个方向,把所有能够离开的陆地都找到并标记,最后统计所有没标记的陆地,代码如下:

class Solution 
private:
    int m, n;
    vector<vector<int>> direction = 1, 0, -1, 0, 0, 1, 0, -1;
    vector<vector<bool>> visit;
public:
    int numEnclaves(vector<vector<int>>& grid) 
        m = grid.size();
        n = grid[0].size();
        visit = vector<vector<bool>>(m, vector<bool>(n, false));
        for(int i = 0; i < m; i ++) 
            dfs(grid, i, 0);
            dfs(grid, i, n - 1);
        
        for(int j = 0; j < n; j ++) 
            dfs(grid, 0, j);
            dfs(grid, m - 1, j);
        
        int ans = 0;
        for(int i = 0; i < m; i ++) 
            for(int j = 0; j < n; j ++) 
                if(!visit[i][j] && grid[i][j] == 1) 
                    ans ++;
                
            
        
        return ans;
    

    void dfs(vector<vector<int>>& grid, int i, int j) 
        if(i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0 || visit[i][j]) 
            return;
        
        visit[i][j] = true;
        for(auto dir : direction) 
            dfs(grid, i + dir[0], j + dir[1]);
        
    
;

当然了广度优先也同样适用,首先从网格边界上的每个陆地单元格开始广度优先搜索,访问所有和网格边界相连的陆地单元格,然后遍历整个网格,统计飞地的数量。这里给的是官方代码:

class Solution 
public:
    vector<vector<int>> dirs = -1, 0, 1, 0, 0, -1, 0, 1;

    int numEnclaves(vector<vector<int>>& grid) 
        int m = grid.size(), n = grid[0].size();
        vector<vector<bool>> visited = vector<vector<bool>>(m, vector<bool>(n, false));
        queue<pair<int,int>> qu;
        for (int i = 0; i < m; i++) 
            if (grid[i][0] == 1) 
                visited[i][0] = true;
                qu.emplace(i, 0);
            
            if (grid[i][n - 1] == 1) 
                visited[i][n - 1] = true;
                qu.emplace(i, n - 1);
            
        
        for (int j = 1; j < n - 1; j++) 
            if (grid[0][j] == 1) 
                visited[0][j] = true;
                qu.emplace(0, j);
            
            if (grid[m - 1][j] == 1) 
                visited[m - 1][j] = true;
                qu.emplace(m - 1, j);
            
        
        while (!qu.empty()) 
            auto [currRow, currCol] = qu.front();
            qu.pop();
            for (auto & dir : dirs) 
                int nextRow = currRow + dir[0], nextCol = currCol + dir[1];
                if (nextRow >= 0 && nextRow < m && nextCol >= 0 && nextCol < n && grid[nextRow][nextCol] == 1 && !visited[nextRow][nextCol]) 
                    visited[nextRow][nextCol] = true;
                    qu.emplace(nextRow, nextCol);
                
            
        
        int enclaves = 0;
        for (int i = 1; i < m - 1; i++) 
            for (int j = 1; j < n - 1; j++) 
                if (grid[i][j] == 1 && !visited[i][j]) 
                    enclaves++;
                
            
        
        return enclaves;
    
;


作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/number-of-enclaves/solution/fei-di-de-shu-liang-by-leetcode-solution-nzs3/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

还有一种方法是并查集,找到所有与边界陆地相连的连通分量,剩下的连通分量就都不能离开。同样是官方代码:

class UnionFind 
public:
    UnionFind(const vector<vector<int>> & grid) 
        int m = grid.size(), n = grid[0].size();
        this->parent = vector<int>(m * n);
        this->onEdge = vector<bool>(m * n, false);
        this->rank = vector<int>(m * n);
        for (int i = 0; i < m; i++) 
            for (int j = 0; j < n; j++) 
                if (grid[i][j] == 1) 
                    int index = i * n + j;
                    parent[index] = index;
                    if (i == 0 || i == m - 1 || j == 0 || j == n - 1) 
                        onEdge[index] = true;
                    
                
            
        
    

    int find(int i) 
        if (parent[i] != i) 
            parent[i] = find(parent[i]);
        
        return parent[i];
    

    void uni(int x, int y) 
        int rootx = find(x);
        int rooty = find(y);
        if (rootx != rooty) 
            if (rank[rootx] > rank[rooty]) 
                parent[rooty] = rootx;
                onEdge[rootx] = onEdge[rootx] | onEdge[rooty];
             else if (rank[rootx] < rank[rooty]) 
                parent[rootx] = rooty;
                onEdge[rooty] = onEdge[rooty] | onEdge[rootx];
             else 
                parent[rooty] = rootx;
                onEdge[rootx] = onEdge[rootx] | onEdge[rooty];
                rank[rootx]++;
            
        
    

    bool isOnEdge(int i) 
        return onEdge[find(i)];
    
private:
    vector<int> parent;
    vector<bool> onEdge;
    vector<int> rank;    
;

class Solution 
public:
    int numEnclaves(vector<vector<int>>& grid) 
        int m = grid.size(), n = grid[0].size();
        UnionFind uf(grid);
        for (int i = 0; i < m; i++) 
            for (int j = 0; j < n; j++) 
                if (grid[i][j] == 1) 
                    int index = i * n + j;
                    if (j + 1 < n && grid[i][j + 1] == 1) 
                        uf.uni(index, index + 1);
                    
                    if (i + 1 < m && grid[i + 1][j] == 1) 
                        uf.uni(index, index + n);
                    
                
            
        
        int enclaves = 0;
        for (int i = 1; i < m - 1; i++) 
            for (int j = 1; j < n - 1; j++) 
                if (grid[i][j] == 1 && !uf.isOnEdge(i * n + j)) 
                    enclaves++;
                
            
        
        return enclaves;
    
;


作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/number-of-enclaves/solution/fei-di-de-shu-liang-by-leetcode-solution-nzs3/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

以上是关于LeetCode 1020 飞地的数量[dfs] HERODING的LeetCode之路的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 力扣官方题解 - 1020. 飞地的数量

DFS 算法秒杀岛屿系列题目

DFS 算法秒杀五道岛屿问题

LEETCODE:飞地的数量(广度优先遍历)

Leetcode-1031 Number of Enclaves(飞地的数量)

Leetcode 1020. Number of Enclaves