题目描述:

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。

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

  1. 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) conn ...

  2. [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) conn ...

  3. leetcode 695 Max Area of Island 岛的最大面积

    这个题使用深度优先搜索就可以直接遍历 DFS递归方法: class Solution { public: vector<vector<,},{,-},{,},{,}}; int maxAr ...

  4. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

  5. 【leetcode】Max Area of Island

    国庆中秋长假过完,又要开始上班啦.先刷个题目找找工作状态. Given a non-empty 2D array grid of 0's and 1's, an island is a group o ...

  6. 【LeetCode】695. Max Area of Island 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...

  7. [leetcode]python 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) conn ...

  8. 695. Max Area of Island最大岛屿面积

    [抄题]: 求最多的联通的1的数量 Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (repre ...

  9. 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 surro ...

随机推荐

  1. Golang之struct

    1.用来定义复杂数据结构 2.struct里面可以包含多个字段(属性) 3.struct类型可以定义方法,注意和函数的区分. 4.struct类型是值类型 5.struct类型可以嵌套 6.Go语言没 ...

  2. python socket编程入门(编写server实例)-乾颐堂

    python 编写server的步骤: 1. 第一步是创建socket对象.调用socket构造函数.如: socket = socket.socket( family, type ) family参 ...

  3. jquery对象的遍历$(selector).each()

    <!DOCTYPE html> <html> <head> <script language="javascript" src=" ...

  4. sql查询磁盘空间并发预警邮件

    检测磁盘空间,如果低于设置的预警值则发出一封预警邮件,这样的事情可以用SQL server的作业可以做,关键SQL语句如下例子所示: DECLARE @TableText NVARCHAR(MAX)= ...

  5. python 多线程简介

    Thread类定义了以下常用方法与属性: Thread.getName() \Thread.setName():老方式用于获取和设置线程的名称,官方建议用Thread.name替代 Thread.id ...

  6. Scheduling the Delivery of Local Notifications

    [Scheduling the Delivery of Local Notifications] Apps can use local notifications to display alerts, ...

  7. ARKit对安卓的提示 ARKit与Google Tango

    我们知道安卓是Google开发的,那么关于AR谷歌有哪些作为呢?就是开发了Google Tango,尽管Tango还未开源,但是用户可以免费使用,可是一般的安卓手机是无法运行的,它对硬件有要求,这对它 ...

  8. Android-UIUtils-工具类

    UIUtils工具类,主要是处理和Activity有关,和界面显示层有关的公共方法: package common.library.utils; import android.app.Activity ...

  9. ASP.NET Core2调用Azure云上的PowerBI报表展示

    在开发企业应用中,报表功能是当之无愧的重头戏,如何将数据通过合适的报表呈现出来成为每个项目人员必需面临的问题.而找到一款合适的报表往往都需要考率价格.开发.风格.支撑等因素.那么,我在这里给大家介绍一 ...

  10. Lucene的数值索引以及范围查询

    对文本搜索引擎的倒排索引(数据结构和算法).评分系统.分词系统都清楚掌握之后,本人对数值索引和搜索一直有很大的兴趣,最近对Lucene对数值索引和范围搜索做了些学习,并将主要内容整理如下: 1. Lu ...