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: As I saw the problem, I feel that I can use dfs to search all continuous '1's and mark them. Scanning row by row, from column to column, when meeting a '1', then mark all its continuous block of '1's. After that, all of its connecting '1's are labeled. Then find the next '1', and mark its connecting '1's, until the right bottom item. 

    So I add a visited vector vector to label the visited items. But it exceeded time limite. 

     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         vector<vector<bool> > visited(row, vector<bool>(col, false));
     9         for(int i = 0; i < row; i++){
    10             for(int j = 0; j < col; j++){
    11                 if(grid[i][j] == '1' && !visited[i][j]){
    12                     helper(grid, visited, i, j);
    13                     result++;
    14                 }
    15             }
    16         }
    17         return result;
    18     }
    19     
    20     void helper(vector<vector<char> > grid, vector<vector<bool> >& visited, int currentX, int currentY){
    21         if(currentX >= grid.size() || currentX < 0 || currentY >= grid[0].size() || currentY < 0 || 
    22            grid[currentX][currentY] == '0' || visited[currentX][currentY])
    23            return;
    24         
    25         visited[currentX][currentY] = true;
    26         helper(grid, visited, currentX + 1, currentY);
    27         helper(grid, visited, currentX - 1, currentY);
    28         helper(grid, visited, currentX, currentY + 1);
    29         helper(grid, visited, currentX, currentY - 1);
    30     }
    31 };
    View Code

    This version deletes the visited vector vector because we can simply replace '1' by any non-0 or non-1 value. It passed with 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 };
  • 相关阅读:
    [JavaScript]使用setTimeout减少多余事件
    Spring.NET教程(二)——环境搭建(基础篇) (转)
    IIS开启GZIP压缩效率对比及部署方法 (转)
    提高表格操作的十五款jQuery插件
    SQLServer和Oracle常用函数对比
    [hystar整理]Entity Framework 教程
    Remoting方法重载遇到的一个问题
    异变: input的背景background
    实时股票数据接口
    发现并解决ASP.NET内存耗尽(OOM),让服务器"永不重启"
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/5209143.html
Copyright © 2011-2022 走看看