首页 > 其他 > 详细

Max Area of Island——leetcode

时间:2018-03-18 13:47:26      阅读:213      评论:0      收藏:0      [点我收藏+]

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

 

Max Area of Island——leetcode

原文:https://www.cnblogs.com/jiadyang/p/8595070.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!