zoukankan      html  css  js  c++  java
  • (DFS BFS 图的遍历) leetcode 695. Max Area of Island

    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是陆地,0是海洋。这个用DFS会简单些。

    C++代码:

    class Solution {
    public:
        vector<vector<int>> grid;
        //注意dfs()里面的参数还需要添加vector<vector<int> > &grid,来保证能够dfs里面的grid的变化后,maxAreaOfIsland()里面的grid也能跟着变化。
        int dfs(vector<vector<int> > &grid,int r, int c){
            if(r >= 0 && r < grid.size() && c >= 0 && c < grid[0].size() && grid[r][c] == 1){
                grid[r][c] = 0;
                return 1 + dfs(grid,r,c-1) + dfs(grid,r,c+1) + dfs(grid,r-1,c) + dfs(grid,r+1,c);
            }
            return 0;
        }
        int maxAreaOfIsland(vector<vector<int>>& grid) {
            int m = grid.size();
            int n = grid[0].size();
            if(m == 0 || n == 0) return 0;
            int ans = 0;
            for(int i = 0; i < m; i++){
                for(int j = 0; j < n; j++){
                    if(grid[i][j] != 0)
                        ans = max(ans,dfs(grid,i,j));
                }
            }
            return ans;
        }
    };    

    也可以用BFS(这个是有点套路,多做几次就会熟练了)

    C++代码:

    int dx[] = {0,1,0,-1};
    int dy[] = {1,0,-1,0};
    class Solution {
    public:
        typedef pair<int,int> pii;
        queue<pii> q;
        int maxAreaOfIsland(vector<vector<int>>& grid) {
            int m = grid.size();
            int n = grid[0].size();
            if(m == 0 || n == 0) return 0;
            int res = 0;
            for(int i = 0; i < m; i++){
                for(int j = 0; j < n; j++){
                    if(grid[i][j] == 0) continue;
                    int cnt = 0;
                    q.push(make_pair(i,j));
                    grid[i][j] = 0;
                    while(!q.empty()){
                        auto t = q.front();q.pop();
                        cnt++;
                        for(int i = 0; i < 4; i++){
                            int xx = t.first + dx[i];
                            int yy = t.second +dy[i];
                            if(xx >= 0 && xx < m && yy >= 0 && yy < n && grid[xx][yy] == 1){
                                grid[xx][yy] = 0;
                                q.push(make_pair(xx,yy));
                            }
                        }
                    }
                    res = max(res,cnt);
                }
            }
            return res;
        }
    };
  • 相关阅读:
    Postman+Newman+Git+JenKins+钉钉(接口自动化测试持续集成)CI_知识回顾与整理
    GitHub 使用入门
    python中json文件处理涉及的四个函数json.dumps()和json.loads()、json.dump()和json.load()的区分
    python实现:重复列表按重复次数排序
    python中lambda匿名函数的用法
    Python中各种符号的意义
    OSI七层协议和TCP/IP的4层、5层协议
    Jmeter中传参输入位置[chrome中区分是否json传参]
    Jmeter中http cookie Manager、Http Header Manager
    jQuery 获取屏幕高度、宽度
  • 原文地址:https://www.cnblogs.com/Weixu-Liu/p/10867417.html
Copyright © 2011-2022 走看看