Leetcode695.Max Area of Island岛屿的最大面积
给定一个包含了一些 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岛屿的最大面积的更多相关文章
- [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 ...
- leetcode 695 Max Area of Island 岛的最大面积
这个题使用深度优先搜索就可以直接遍历 DFS递归方法: class Solution { public: vector<vector<,},{,-},{,},{,}}; int maxAr ...
- Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island)
Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island) 深度优先搜索的解题详细介绍,点击 给定一个包含了一些 0 和 1的非空二维数组 grid ...
- 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 用了第一种方式, ...
- C#LeetCode刷题之#695-岛屿的最大面积( Max Area of Island)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3736 访问. 给定一个包含了一些 0 和 1的非空二维数组 gr ...
- [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 ...
- 【leetcode】Max Area of Island
国庆中秋长假过完,又要开始上班啦.先刷个题目找找工作状态. Given a non-empty 2D array grid of 0's and 1's, an island is a group o ...
- [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 ...
- 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 ...
随机推荐
- Mysql8+mybatisGenerator (mysql 8的逆向工程)
最近试了一下mysql8的逆向工程工具 1.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOC ...
- ES6 class继承
ES6 class继承 class类的继承 class可以通过extends关键字实现继承,这笔ES5的通过修改原型连实现继承要清晰和方便很多. class Point{ } class ColorP ...
- 详解如何挑战4秒内百万级数据导入SQL Server(转)
对于大数据量的导入,是DBA们经常会碰到的问题,在这里我们讨论的是SQL Server环境下百万级数据量的导入,希望对大家有所帮助.51CTO编辑向您推荐<SQL Server入门到精通&g ...
- combobox 的onLoadSuccess执行两次解决办法和 取值赋值
加红色字部分 jsp <input class="easyui-combobox" id="keshi" name="keshi" v ...
- 谈谈数据库sql编写
本文主要给初学者关于关系数库的一个浮光掠影式的介绍,如果想深入理解,必须对于下文提到的每个内容单独深入学习! it-information technology的简称,中文是信息机技术,信息其实就是数 ...
- 建造者模式(Builder)(生成器模式)(框架化)
建造者模式将一个复杂对象的构建与其表示分离. 将复杂对象进行框架化,将同类的对象编造进同一个制造流程.同类·对象会有一样的框架. 而由于各部分的实现细节有所不同,所生产出来的产品会有所不同.从而有不同 ...
- RabbitMq知识点总结
一.RabbitMQ简介 AMQP,即Advanced Message Queuing Protocol,一个提供统一消息服务的应用层标准高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间 ...
- mybatis框架学习:
一.什么是框架 它是我们软件开发中的一套解决方案,不同的框架解决的是不同的问题 使用框架的好处: 框架封装了很多的细节,使开发者可以使用极简的方式实现功能 大大提高开发效率 二.三层框架 表现层: 用 ...
- Liferay 7:Liferay DXP全套教程内附源码
分享是美德 都是英文教程,有不明白的问题可以随时咨询我. http://www.javasavvy.com/liferay-7-hooks-tutorials/
- Frank Dellaert Slam Speech 20190708
Georgia Institue of Tecknology 3D Models from Community Databases Spatiotemporal Reconstruction 4D C ...