简介

使用BFS算法
不知道莫名超时了

code

class Solution {
public:
    struct point{
        int i;
        int j;
        point(int i_, int j_){
            i = i_;
            j = j_;
        }
    };
    void bfs(int i, int j, vector<vector<bool>>& visit, vector<vector<char>>& grid){
        queue<point> q;
        q.push(point(i,j));
        while(!q.empty()){
            point tmp = q.front();
            visit[tmp.i][tmp.j] = true;
            q.pop();
            // 上
            if(tmp.i-1>=0 && grid[tmp.i-1][tmp.j] == '1' && visit[tmp.i-1][tmp.j]==false){
                q.push(point(tmp.i-1, tmp.j));
            }
            // 下
            if(tmp.i+1 < grid.size() && grid[tmp.i+1][tmp.j] == '1' && visit[tmp.i+1][tmp.j] == false){
                q.push(point(tmp.i+1, tmp.j));
            }
            // 左
            if(tmp.j-1>=0 && grid[tmp.i][tmp.j-1] == '1' && visit[tmp.i][tmp.j-1] == false){
                q.push(point(tmp.i, tmp.j - 1));
            }
            // 右
            if(tmp.j+1 < grid[0].size() && grid[tmp.i][tmp.j+1] == '1' && visit[tmp.i][tmp.j+1] == false){
                q.push(point(tmp.i, tmp.j + 1));
            }
        }
    }
    int numIslands(vector<vector<char>>& grid) {
        if(grid.size() == 0){
            return 0;
        }
        vector<vector<bool>> visit(grid.size(), vector<bool>(grid[0].size(), false));
        for(int i=0; i<grid.size(); i++){
            for(int j=0; j<grid[0].size(); j++){
                if(grid[i][j] == '0') visit[i][j] = true;
            }
        }
        int index = 0;
        for(int i=0; i<grid.size(); i++){
            for(int j=0; j<grid[0].size(); j++){
                if(visit[i][j] == false){
                    bfs(i, j, visit, grid);
                    index++;
                }
            }
        }
        return index;


    }
};

c++

一定程度加速版本

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        if(grid.size() == 0){
            return 0;
        }
        int index = 0;
        int n = grid.size();
        int n0 = grid[0].size();
        for(int i=0; i<n; i++){
            for(int j=0; j<n0; j++){
                if(grid[i][j] == '1'){
        queue<pair<int, int>> q;
        q.push({i,j});
        while(!q.empty()){
            auto tmp = q.front();
            int first = tmp.first;
            int second = tmp.second;
            grid[first][second] = '0';
            q.pop();
            // 上
            if(first-1>=0 && grid[first-1][second] == '1' ){
                q.push({first-1, second});
                grid[first-1][second] = '0';
            }
            // 下
            if(first+1 < n && grid[first+1][second] == '1'){
                q.push({first+1, second});
                grid[first+1][second] = '0';
            }
            // 左
            if(second-1>=0 && grid[first][second-1] == '1' ){
                q.push({first, second - 1});
                grid[first][second-1] = '0';
            }
            // 右
            if(second+1 < n0 && grid[first][second+1] == '1'){
                q.push({first, second + 1});
                grid[first][second+1] = '0'; // 这部分有减枝的作用。
            }
        }
                    index++;
                }
            }
        }
        return index;


    }
};

java 版本dfs

class Solution {
    void dfs(char [][]grid, int r, int c){
        int nr = grid.length;
        int nc = grid[0].length;

        if(r < 0 || c < 0 || r >= nr || c >= nc || grid[r][c] == '0') {
            return;
        }
        grid[r][c] = '0';
        dfs(grid, r - 1, c);
        dfs(grid, r + 1, c);
        dfs(grid, r, c - 1);
        dfs(grid, r, c + 1);
    }
    public int numIslands(char[][] grid) {
        if (grid == null || grid.length == 0){
            return 0;
        }
        int nr = grid.length;
        int nc = grid[0].length;
        int num_islands = 0;
        for(int r = 0; r < nr; ++r){
            for(int c = 0; c < nc; ++c){
                if(grid[r][c] == '1'){
                    ++num_islands;
                    dfs(grid, r, c);
                }
            }
        }
        return num_islands;
    }
}
posted on 2021-05-25 12:18  HDU李少帅  阅读(40)  评论(0编辑  收藏  举报