Max Area of Island——leetcode

Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)

Example 1:

[[0,0,1,0,0,0,0,1,0,0,0,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,1,1,0,1,0,0,0,0,0,0,0,0],
 [0,1,0,0,1,1,0,0,1,0,1,0,0],
 [0,1,0,0,1,1,0,0,1,1,1,0,0],
 [0,0,0,0,0,0,0,0,0,0,1,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,0,0,0,0,0,0,1,1,0,0,0,0]]

Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.

 

Example 2:

[[0,0,0,0,0,0,0,0]]

Given the above grid, return 0.

 

Note: The length of each dimension in the given grid does not exceed 50.

求矩阵里连在一起的1的和,斜的连接不算。解题思路自然是dfs和bfs,太长时间荒废了= =于是乘此机会复习了一下这方面知识,参考了一下discuss

 1 //dfs
 2 
 3 class Solution {
 4 public:
 5     int maxAreaOfIsland(vector<vector<int>>& grid) {
 6         
 7         int r = grid.size();
 8         if (r == 0)
 9             return 0;
10         int c = grid[0].size();
11 
12         int area = 0;
13 
14         for (int i = 0; i < r; ++i)
15         {
16             for (int j = 0; j < c; ++j)
17             {
18                 if (grid[i][j] == 1)
19                 {
20                     area = max(area, dfs(grid, i, j));
21                 }
22             }
23         }
24 
25         return area;
26     }
27 
28 
29 private:
30     int dfs(vector<vector<int>>& grid, int row, int col)
31     {
32         vector<vector<int>> dir{ {-1,0},{1,0},{0,1},{0,-1} };//上下左右四个方向
33         int area = 1;
34         int m = grid.size();
35         int n = grid[0].size();
36         --grid[row][col];
37         for (int i = 0; i < 4; ++i)
38         {
39             int r = row + dir[i][0], c = col + dir[i][1];
40 
41             if (r >= 0 && r < m && c>=0 && c < n && grid[r][c] == 1)//边界检查&标记检查
42             {
43                 area += dfs(grid, r, c);
44             }
45         }
46 
47         return area;
48     }
49 };
 1 //bfs
 2 
 3 class Solution {
 4 public:
 5     int maxAreaOfIsland(vector<vector<int>>& grid) {
 6 
 7         int r = grid.size();
 8         if (r == 0)
 9             return 0;
10         int c = grid[0].size();
11 
12         int area = 0;
13 
14         for (int i = 0; i < r; ++i)
15         {
16             for (int j = 0; j < c; ++j)
17             {
18                 if (grid[i][j] == 1)
19                 {
20                     area = max(area, dfs(grid, i, j));
21                 }
22             }
23         }
24 
25         return area;
26     }
27 
28 private:
29     int dfs(vector<vector<int>>& grid, int row, int col)
30     {
31         vector<vector<int>> dir{ { -1,0 },{ 1,0 },{ 0,1 },{ 0,-1 } };//上下左右四个方向
32         int area = 1;
33         int m = grid.size();
34         if (m == 0)
35             return 0;
36         int n = grid[0].size();
37         --grid[row][col];//mark
38 
39         queue<pair<int, int>> q;
40         q.push({ row,col });
41 
42         while (q.size())
43         {
44             int x = q.front().first, y = q.front().second;
45             q.pop();
46 
47             for (int i = 0; i < 4; ++i)
48             {
49                 int r = x + dir[i][0], c = y + dir[i][1];
50 
51                 if (r >= 0 && r < m && c >= 0 && c < n && grid[r][c] == 1)//边界检查&标记检查
52                 {
53                     area++;
54                     --grid[r][c];
55                     q.push({ r, c });
56                 }
57             }
58         }
59 
60         return area;
61     }
62 };

在之后看discuss的时候看到了一种比较“现代”的解法,用到了c++11的lambda表达式和function类模板:

//dfs
class Solution {
public:
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size(), maxarea = 0;
        auto valid = [&](int i, int j) { return 0 <= i && i < m && 0 <= j && j < n && grid[i][j]; };
        std::function<int(int, int)> dfs = [&](int i, int j){ return !valid(i, j) ? 0 : grid[i][j]-- + dfs(i, j+1) + dfs(i+1, j) + dfs(i, j-1) + dfs(i-1, j);};
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                maxarea = max(maxarea, dfs(i, j));
        return maxarea;
    }
};

 

posted @ 2018-03-18 12:47  还是说得清点吧  阅读(125)  评论(0编辑  收藏  举报