题目描述:

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. js获得URL中的参数

    js获得URL中的参数 function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + &quo ...

  2. Python深度学习之安装theano(windows)

    前方预警:windows的坑太多了,抛弃用linux吧 安装theano,提前清空自己的python环境吧,坑太多了,anaconda会自动安装path 一,首先安装python包管理anaconda ...

  3. Java 中的类

    public class Test{ public static void main(String[] args){ Animal a1 = new Dog(); a1.shout();//编译通过 ...

  4. 安装python-empy

    sudo python setup.py install

  5. ubuntu系统中解决github下载速度慢问题

    如果你在使用github,出现访问和下载速度慢问题,可以通过修改host解决.记得不要踩坑哦!分四步: 第一步:查询速度快的IP地址 在http://tool.chinaz.com/dns中查询下面三 ...

  6. Linux下javaweb环境搭建

    步骤: 1.使用远程工具连接上服务器,例如xsheel(ssh).filezilla(ftp) 2.JDK安装及相关配置 3.Mysql安装及相关配置 4.Tomcat安装及相关配置 5.项目部署及启 ...

  7. UVaLive 3126 Taxi Cab Scheme (最小路径覆盖)

    题意:有 n 个客人,要从 si 到 ti,每个人有一个出发时间,现在让你安排最少和出租车去接,在接客人时至少要提前一分钟到达客人的出发地点. 析:把每个客人看成一个结点,然后如果用同一个出租车接的话 ...

  8. 复杂HTML页面解析

    1.层叠样式表CSS可以让html元素呈现出差异化,网络爬虫可以通过class属性的值,轻松分出不同标签 findAll函数通过标签的名称和属性来查找标签 from urllib.request im ...

  9. 用WORD2007发布博客文章

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  10. Spring配置bean的方法(工厂方法和Factorybean)

    通过工厂方法配置bean 通过调用静态工厂方法创建bean 通过静态工厂方法创建bean是将对象创建的过程封装到静态方法中.当客户端需要对象时,只需要简单地调用静态方法,而不关心创建对象的细节. 要声 ...