LeetCode 200, 694

Typical search problem. 694 is the expansion of 200, and there are more questions about how to preserve the characteristics of the island. Two questions can be done either with DFS or with BFS.

The pair<int, int> and make_pair(i, j) are used to record the coordinates in solving the problem, which is more convenient than creating a structure yourself. The auto keyword is used to declare the type, and the type will be automatically inferred. If the type is more complicated, it will be much more convenient to declare it with auto.

The structures of DFS and BFS are relatively fixed. The writing method of DFS is consistent with the framework structure summarized before, and practice makes perfect.

 

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

Whether the islands are the same can be determined by recording the relative coordinates relative to the search starting point (the search order is all fixed).

DFS:

class Solution {
public:
    set<vector<vector<int>>> islands;
    
    int of [ 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 of [ 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();
    }
};

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325100718&siteId=291194637