zoukankan      html  css  js  c++  java
  • leetcode 200 岛屿数量

    简介

    使用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;
        }
    }
    
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    CodeForces
    CodeForces
    Comet OJ
    CodeForces
    2019年第五届计蒜之道复赛总结
    2019计蒜之道初赛4 B. 腾讯益智小游戏—矩形面积交(简单)(矩形交集)
    2019计蒜之道初赛3 D. 阿里巴巴协助征战SARS(困难)(大数取余+欧拉降幂)
    2018计蒜之道复赛 贝壳找房函数最值(贪心+优先队列)
    牛客想开了大赛2 A-【六】平面(切平面)
    2018年第九届蓝桥杯国赛试题(JavaA组)
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/14808220.html
Copyright © 2011-2022 走看看