zoukankan      html  css  js  c++  java
  • 【刷题-LeetCode】200 Number of Islands

    1. Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

    Example 1:

    Input: grid = [
      ["1","1","1","1","0"],
      ["1","1","0","1","0"],
      ["1","1","0","0","0"],
      ["0","0","0","0","0"]
    ]
    Output: 1
    

    Example 2:

    Input: grid = [
      ["1","1","0","0","0"],
      ["1","1","0","0","0"],
      ["0","0","1","0","0"],
      ["0","0","0","1","1"]
    ]
    Output: 3
    

    解1 bfs

    class Solution {
    public:
        int dx[4] = {-1, 1, 0, 0};
        int dy[4] = {0, 0, -1, 1};
        bool valid(int x, int y, int m, int n){
            if(x < 0 || x >= m || y < 0 || y >= n)return false;
            return true;
        }
        int numIslands(vector<vector<char>>& grid) {
            if(grid.size() == 0)return 0;
            vector<vector<bool>> vis(grid.size(), vector<bool>(grid[0].size(), false));
            int ans = 0;
            for(int i = 0; i < grid.size(); ++i){
                for(int j = 0; j < grid[0].size(); ++j){
                    if(vis[i][j] == false && grid[i][j] == '1'){
                        ans++;
                        bfs(grid, vis, i, j);
                        //dfs(grid, vis, i, j);
                    }
                }
            }
            return ans;
        }
        void bfs(vector<vector<char>>& grid, vector<vector<bool>>& vis, 
                 int x, int y){
            queue<pair<int, int>>q;
            q.push(make_pair(x,y));
            vis[x][y] = true;
            while(!q.empty()){
                int tmpx = q.front().first, tmpy = q.front().second;
                q.pop();
                for(int i = 0; i < 4; ++i){
                    int tmpxx = tmpx + dx[i], tmpyy = tmpy + dy[i];
                    if(valid(tmpxx, tmpyy, grid.size(), grid[0].size())
                       && !vis[tmpxx][tmpyy] && grid[tmpxx][tmpyy]=='1'){
                        q.push(make_pair(tmpxx, tmpyy));
                        vis[tmpxx][tmpyy] = true;
                    }
                }
            }
        }
    };
    

    解2 dfs

    	void dfs(vector<vector<char>>& grid, vector<vector<bool>>& vis, 
                 int x, int y){
            vis[x][y] = true;
            for(int i = 0; i < 4; ++i){
                int tmpx = x + dx[i], tmpy = y + dy[i];
                if(valid(tmpx, tmpy, grid.size(), grid[0].size())
                  && !vis[tmpx][tmpy] && grid[tmpx][tmpy] == '1'){
                    dfs(grid, vis, tmpx, tmpy);
                }
            }
        }
    
  • 相关阅读:
    定时任务
    ---Ubuntu 连接手机tethering
    ---thunar 文件浏览器的使用!
    ---recovery ui 修改
    ---wordpress 升级遇到的问题!
    ---BD PROCHOT
    ---安装/修复Ubuntu/Ach Linux efi 启动 !!!(包括双系统启动)
    ---sgdisk 分区表
    --- cache PV 丢失或损坏 怎么办
    ---lvm2分区修复问题!
  • 原文地址:https://www.cnblogs.com/vinnson/p/13300859.html
Copyright © 2011-2022 走看看