LeetCode 200, 694

Posted 約束の空

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 200, 694相关的知识,希望对你有一定的参考价值。

典型的搜索问题。694是200的拓展,多了怎么保存岛屿的特征的问题。两道题既可以用DFS做,也可以用BFS做。

解题中用到了 pair<int, int> 和 make_pair(i, j) 来记录坐标,相较于自己创建一个结构体,更加方便。auto关键字,用于申明类型,类型会自动推断,如果类型比较复杂,用auto申明会方便不少。

DFS和BFS结构都比较固定,DFS的写法与之前总结的框架结构保持一致,熟能生巧。

 

200. Number of Islands

DFS:

class Solution {
public:
    int di[4]={0,0,1,-1};
    int dj[4]={1,-1,0,0};
    
    int numIslands(vector<vector<char>>& grid) {
        int count=0;
        for (int i=0;i<grid.size();++i){
            for (int j=0;j<grid[0].size();++j){
                if(grid[i][j]==1){
                    ++count;
                    dfs(grid,i,j);
                }
            }
        }
        return count;
    }
    
    void dfs(vector<vector<char>> &grid, int i, int j){
        if (i<0 || i>=grid.size() || j<0 || j>=grid[0].size() || grid[i][j]!=1) return;
        grid[i][j] = 0;
        for (int k=0;k<4;++k){
            dfs(grid,i+di[k],j+dj[k]);
        }
    }
};

 

BFS:

class Solution {
public:
    int di[4]={0,0,1,-1};
    int dj[4]={1,-1,0,0};
    
    int numIslands(vector<vector<char>>& grid) {
        int count=0;
        for (int i=0;i<grid.size();++i){
            for (int j=0;j<grid[0].size();++j){
                if(grid[i][j]==1){
                    ++count;
                    
                    queue<pair<int,int>> q;
                    q.push({i,j});
                    grid[i][j] = 0;

                    while (!q.empty()){
                        auto tmp=q.front(); q.pop();
                        for (int k=0;k<4;++k){
                            int ii=tmp.first+di[k], jj=tmp.second+dj[k];
                            if (ii>=0 && ii<grid.size() && jj>=0 && jj<grid[0].size() && grid[ii][jj]==1){
                                q.push({ii,jj});
                                grid[ii][jj] = 0;
                            }
                        }
                    }
                    
                }
            }
        }
        return count;
    }

};

 

 

694. Number of Distinct Islands

可以通过记录相对于搜索起点的相对坐标来判断岛屿是否相同(搜索的顺序都是固定的)。

DFS:

class Solution {
public:
    set<vector<vector<int>>> islands;
    
    int di[4]={0,0,1,-1};
    int dj[4]={1,-1,0,0};
    
    int numDistinctIslands(vector<vector<int>>& grid) {
        for (int i=0;i<grid.size();++i){
            for (int j=0;j<grid[0].size();++j){
                if (grid[i][j]==1){
                    vector<vector<int>> island;
                    dfs(grid,i,j,i,j,island);
                    islands.insert(island);
                }
            }
        }
        return islands.size();
    }
    
    void dfs(vector<vector<int>>& grid, int i0, int j0, int i, int j, vector<vector<int>> &island){
        if (i<0 || i>=grid.size() || j<0 || j>=grid[0].size() || grid[i][j]!=1) return;
        island.push_back({i-i0,j-j0});
        grid[i][j] = 0;
        for (int k=0;k<4;++k){
            dfs(grid,i0,j0,i+di[k],j+dj[k],island);
        }
    }
};

 

BFS:

class Solution {
public:
    set<vector<pair<int,int>>> islands;
    
    int di[4]={0,0,1,-1};
    int dj[4]={1,-1,0,0};
    
    int numDistinctIslands(vector<vector<int>>& grid) {
        for (int i=0;i<grid.size();++i){
            for (int j=0;j<grid[0].size();++j){
                if (grid[i][j]==1){
                    vector<pair<int,int>> island;
                    
                    queue<pair<int,int>> q;
                    q.push({i,j});
                    grid[i][j] = 0;
                    island.push_back({0,0});

                    while (!q.empty()){
                        auto tmp=q.front(); q.pop();
                        for (int k=0;k<4;++k){
                            int ii=tmp.first+di[k], jj=tmp.second+dj[k];
                            if (ii>=0 && ii<grid.size() && jj>=0 && jj<grid[0].size() && grid[ii][jj]==1){
                                q.push({ii,jj});
                                grid[ii][jj] = 0;
                                island.push_back({ii-i,jj-j});
                            }
                        }
                    }
                    islands.insert(island);
                }
            }
        }
        return islands.size();
    }
};

 

以上是关于LeetCode 200, 694的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 694. Number of Distinct Islands

[leetcode]694. Number of Distinct Islands你究竟有几个异小岛?

七个办法只有一个有效:200 PORT command successful. Consider using PASV.425 Failed to establish connection.(代码片段

DFS 算法秒杀岛屿系列题目

200道大数据面试常考Leetcode算法题--数组篇02(python带代码解析)

leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段