码迷,mamicode.com
首页 > 其他好文 > 详细

Number of Islands

时间:2016-02-23 11:17:07      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

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 };

 

Number of Islands

标签:

原文地址:http://www.cnblogs.com/amazingzoe/p/5209143.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!