zoukankan      html  css  js  c++  java
  • 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:

    11110
    11010
    11000
    00000

    Answer: 1

    Example 2:

    11000
    11000
    00100
    00011

    Answer: 3

    Analyse: BFS or DFS. If found a '1', label that element as visited, and find all elements could be reached from that element and label them. 

    Runtime: Iteration 12ms

     1 class Solution {
     2 public:
     3     int numIslands(vector<vector<char>>& grid) {
     4         if(grid.empty() || grid[0].empty()) return 0;
     5         
     6         int result = 0;
     7         for(int i = 0; i < grid.size(); i++) {
     8             for(int j = 0; j < grid[i].size(); j++) {
     9                 if(grid[i][j] == '1') {
    10                     grid[i][j] = '2';
    11                     bfs(grid, i, j);
    12                     result++;
    13                 }
    14             }
    15         }
    16         return result;
    17     }
    18     
    19     void bfs(vector<vector<char> >& grid, int i, int j) {
    20         queue<pair<int, int> > qu;
    21         qu.push(make_pair(i, j));
    22         
    23         int m = grid.size(), n = grid[0].size();
    24         while(!qu.empty()) {
    25             pair<int, int> node = qu.front();
    26             i = node.first;
    27             j = node.second;
    28             qu.pop();
    29             // check whether its neighbour is island
    30             if(i > 0 && grid[i - 1][j] == '') {
    31                 qu.push(make_pair(i - 1, j));
    32                 grid[i - 1][j] = '2';
    33             }
    34             if(j > 0 && grid[i][j - 1] == '1') {
    35                 qu.push(make_pair(i, j - 1));
    36                 grid[i][j - 1] = '2';
    37             }
    38             if(j < n - 1 && grid[i][j + 1] == '1') {
    39                 qu.push(make_pair(i, j + 1));
    40                 grid[i][j + 1] = '2';
    41             }
    42             if(i < m - 1 && grid[i + 1][j] == '1') {
    43                 qu.push(make_pair(i + 1, j));
    44                 grid[i + 1][j] = '2';
    45             }
    46         }
    47     }
    48 };
    View Code

    Runtime: Recursive 8ms.

     1 class Solution {
     2 public:
     3     int numIslands(vector<vector<char>>& grid) {
     4         int result;
     5         if(grid.empty() || grid[0].empty()) return result;
     6         
     7         int row = grid.size(), col = grid[0].size();
     8         for(int i = 0; i < row; i++){
     9             for(int j = 0; j < col; j++){
    10                 if(grid[i][j] == '1'){
    11                     helper(grid, i, j);
    12                     result++;
    13                 }
    14             }
    15         }
    16         return result;
    17     }
    18     
    19     void helper(vector<vector<char> > &grid, int x, int y){
    20         if(x >= grid.size() || x < 0 || y >= grid[0].size() || y < 0 || grid[x][y] != '1')
    21             return;
    22             
    23         grid[x][y] = '2';
    24         helper(grid, x + 1, y);
    25         helper(grid, x - 1, y);
    26         helper(grid, x, y + 1);
    27         helper(grid, x, y - 1);
    28     }
    29 };
  • 相关阅读:
    java web 入门实例servlet篇(显示后台数据库列表,删除某一条记录并显示)
    我的成长比价系列:java web开发过程中遇到的错误一:sql语句换行错误
    spring mvc + velocity 搭建实例程序maven版本并且使用的是tomcat容器而不是jetty(step by step)
    spring mvc学习笔记(一)web.xml文件配置的一点重要信息
    与数据库连接的页面增删改查 的easyui实现(主要是前端实现)
    oracle 11g 空表导出
    EMCA和EMCTL的简单用法
    vs2010补丁
    CAS 策略已被 .NET Framework 弃用
    sqlserver2008 链接服务器 2000
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/5759327.html
Copyright © 2011-2022 走看看