leetcode-695-Max Area of Island(BFS)

题目描述:

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.

要完成的函数:

int maxAreaOfIsland(vector<vector<int>>& grid)

说明:

1、给定一个二维矩阵,其中只含有0和1,0表示水域,1表示陆地,要求返回这片地方中最大的一块陆地的面积。

2、这其实是一道深度优先搜索或者广度优先搜索的题目。

由于DFS要用到递归,比较麻烦,所以笔者选择了BFS来实现,定义了一个队列。

代码如下:(附详解)

    int maxAreaOfIsland(vector<vector<int>>& grid)
    {
        queue<int>q1;
        int hang=grid.size(),lie=grid[0].size(),sum=0,sum1=0,i=0,j=0,t1=0,t2=0;
        while(i<hang)//每一行的处理
        {
            while(j<lie)//每一列的处理
            {
                if(grid[i][j]==1)//如果搜索到一个1了
                {
                    q1.push(i);//把行坐标塞到队列里面去
                    q1.push(j);//把列坐标塞到队列里面去
                    grid[i][j]=0;//并将这个点改为陆地,避免后面再次搜索到,重复计算
                    sum1=0;//统计当前陆地的面积
                    while(!q1.empty())//当队列非空时,迭代处理
                    {
                        t1=q1.front();//取出行坐标
                        q1.pop();
                        t2=q1.front();//取出列坐标
                        q1.pop();
                        sum1++;
                        if(t1-1>=0&&grid[t1-1][t2]==1)//判断上方有没有陆地
                        {
                            q1.push(t1-1);
                            q1.push(t2);
                            grid[t1-1][t2]=0;//置为0,避免再次搜索到,重复计算
                        }
                        if(t1+1<hang&&grid[t1+1][t2]==1)//判断下方有没有陆地
                        {
                            q1.push(t1+1);
                            q1.push(t2);
                            grid[t1+1][t2]=0;
                        }
                        if(t2-1>=0&&grid[t1][t2-1]==1)//判断左方有没有陆地
                        {
                            q1.push(t1);
                            q1.push(t2-1);
                            grid[t1][t2-1]=0;
                        }
                        if(t2+1<lie&&grid[t1][t2+1]==1)//判断右方有没有陆地
                        {
                            q1.push(t1);
                            q1.push(t2+1);
                            grid[t1][t2+1]=0;
                        }
                    }
                    sum=max(sum,sum1);//取每次陆地面积的最大值
                }
                j++;
            }
            i++;
            j=0;//j=0记得要加上
        }
        return sum;
    }

上述代码实测30ms,beats 80.17% of cpp submissions。

原文地址:https://www.cnblogs.com/king-3/p/9188490.html

时间: 2024-11-13 05:19:08

leetcode-695-Max Area of Island(BFS)的相关文章

(DFS 图的遍历) 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

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

[LeetCode] Sum Root to Leaf Numbers(bfs)

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

[LeetCode] 695. Max Area of Island_Easy tag: DFS/BFS

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

200. Number of Islands + 695. Max Area of Island

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

695. Max Area of Island

Problem 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

【easy】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 isl

Leetcode之广度优先搜索(BFS)专题-1162. 地图分析(As Far from Land as Possible)

BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree Level Order Traversal) 你现在手里有一份大小为 N x N 的『地图』(网格) grid,上面的每个『区域』(单元格)都用 0 和 1 标记好了.其中 0 代表海洋,1 代表陆地,你知道距离陆地区域最远的海洋区域是是哪一个吗?请返回该海洋区域到离它最近的陆地区域的距离. 我们这里说的距离是『曼哈顿距离』( Manhattan Distance):(x0, y0) 

Leetcode之广度优先搜索(BFS)专题-994. 腐烂的橘子(Rotting Oranges)

BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree Level Order Traversal) 在给定的网格中,每个单元格可以有以下三个值之一: 值 0 代表空单元格: 值 1 代表新鲜橘子: 值 2 代表腐烂的橘子. 每分钟,任何与腐烂的橘子(在 4 个正方向上)相邻的新鲜橘子都会腐烂. 返回直到单元格中没有新鲜橘子为止所必须经过的最小分钟数.如果不可能,返回 -1. 示例 1: 输入:[[2,1,1],[1,1,0],[0,1,1