1. 程式人生 > >Max Area of Island——leetcode

Max Area of Island——leetcode

檢查 all nta first sum you tco aof post

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