给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合。你可以假设二维矩阵的四个边缘都被水包围着。

找到给定的二维数组中最大的岛屿面积。(如果没有岛屿,则返回面积为0。)

示例 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]]

对于上面这个给定矩阵应返回 6。注意答案不应该是11,因为岛屿只能包含水平或垂直的四个方向的‘1’。

示例 2:

[[0,0,0,0,0,0,0,0]]

对于上面这个给定的矩阵, 返回 0。

注意: 给定的矩阵grid 的长度和宽度都不超过 50。

BFS:

class Solution {
public:
vector<vector<int> > visit;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int r;
int c;
int maxAreaOfIsland(vector<vector<int> >& grid) {
r = grid.size();
if(r == 0)
return 0;
c = grid[0].size();
visit = vector<vector<int> >(r, vector<int>(c, 0));
int res = 0;
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
if(visit[i][j] != 1 && grid[i][j] == 1)
{
visit[i][j] = 1;
res = max(res, BFS(grid, i, j));
}
}
}
return res;
} int BFS(vector<vector<int> >& grid, int x, int y)
{
queue<pair<int, int> > q;
q.push(make_pair(x, y));
int cnt = 0;
while(!q.empty())
{
int xx = q.front().first;
int yy = q.front().second;
cnt++;
q.pop();
for(int i = 0; i < 4; i++)
{
int newx = xx + dx[i];
int newy = yy + dy[i];
if(newx < 0 || newx >= r || newy < 0 || newy >= c)
continue;
if(visit[newx][newy] == 1)
continue;
if(grid[newx][newy] == 0)
continue;
visit[newx][newy] = 1;
q.push(make_pair(newx, newy));
}
}
return cnt;
}
};

DFS:

class Solution {
public:
vector<vector<int> > visit;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int r;
int c;
int maxAreaOfIsland(vector<vector<int> >& grid) {
r = grid.size();
if(r == 0)
return 0;
c = grid[0].size();
visit = vector<vector<int> >(r, vector<int>(c, 0));
int res = 0;
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
if(visit[i][j] != 1 && grid[i][j] == 1)
{
res = max(res, DFS(grid, i, j));
}
}
}
return res;
} int DFS(vector<vector<int> >& grid, int x, int y)
{
int cnt = 1;
visit[x][y] = 1;
for(int i = 0; i < 4; i++)
{
int newx = x + dx[i];
int newy = y + dy[i];
if(newx < 0 || newx >= r || newy < 0 || newy >= c)
continue;
if(visit[newx][newy] == 1)
continue;
if(grid[newx][newy] == 0)
continue;
cnt += DFS(grid, newx, newy);
}
return cnt;
}
};

Leetcode695.Max Area of Island岛屿的最大面积的更多相关文章

  1. [LeetCode] 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 岛的最大面积

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

  3. Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island)

    Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island) 深度优先搜索的解题详细介绍,点击 给定一个包含了一些 0 和 1的非空二维数组 grid ...

  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. C#LeetCode刷题之#695-岛屿的最大面积( Max Area of Island)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3736 访问. 给定一个包含了一些 0 和 1的非空二维数组 gr ...

  6. [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 ...

  7. 【leetcode】Max Area of Island

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

  8. [Swift]LeetCode695. 岛屿的最大面积 | 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 ...

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

随机推荐

  1. File- Linux必学的60个命令

    1.作用 件内容判断文件类型,使用权限是所有用户. 2.格式 file通过探测文 file [options] 文件名 3.[options]主要参数 -v:在标准输出后显示版本信息,并且退出. -z ...

  2. wpf listbox touch 整个窗口移动

    工作中遇到遇到,在有listbox中的地方,touch listbox的时候  可以把整个窗体都移动了,解决方案如下: /// <summary> /// prevent the rubb ...

  3. List -- 列表的一些操作

    1,基本操作 2,内建函数 3,方法Method

  4. MaxCompute,基于Serverless的高可用大数据服务

    摘要:2019年1月18日,由阿里巴巴MaxCompute开发者社区和阿里云栖社区联合主办的“阿里云栖开发者沙龙大数据技术专场”走近北京联合大学,本次技术沙龙上,阿里巴巴高级技术专家吴永明为大家分享了 ...

  5. Python字符串切片操作知识详解

    Python字符串切片操作知识详解 这篇文章主要介绍了Python中字符串切片操作 的相关资料,需要的朋友可以参考下 一:取字符串中第几个字符 print "Hello"[0] 表 ...

  6. 转载 ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(一) 整理基础数据

    ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(一) 整理基础数据   最近碰巧发现一款比较好的Web即时通讯前端组件,layim,百度关键字即可,我下面要做的就是基于这个前 ...

  7. MyBatis映射器(一)--多参数传递方式

    在mybatis映射器的接口中,一般在查询时需要传递一些参数作为查询条件,有时候是一个,有时候是多个.当只有一个参数时,我们只要在sql中使用接口中的参数名称即可,但是如果是多个呢,就不能直接用参数名 ...

  8. 通过Angular-cli创建新项目

    前提:已经安装Git 方法一:(推荐) 1.在需要创建项目的文件夹中右键打开 Git Bush Here ,在此输入  ng new ‘项目名’  --skip-install   (如下my-app ...

  9. Java 8 的新特性和Java 的4种引用方式

    一.接口的增强 Java 8允许我们给接口添加一个非抽象的方法实现,只需要使用 default关键字即可,这个特征又叫做扩展方法,示例如下: interface Formula { double ca ...

  10. UVA11613 Acme Corproation

    UVA11613 Acme Corproation 生产销售计划 题目大意 A公司生产一种元素,给出该元素在未来M个月中每个月的单位售价,最大生产量,生产成本,最大销售量和最大存储时间,和每月存储代价 ...