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 ...
随机推荐
- mysqlbinlog: unknown variable 'default-character-set=utf8'
[xxx@dbhost log]$ mysqlbinlog mysql-bin. mysqlbinlog: unknown variable 'default-character-set=utf8' ...
- Quick BI 的模型设计与生成SQL原理剖析
一.摘要 随着物联网的告诉发展,数据量呈现井喷式的增长,如何来分析和使用这些数据,使数据产生商业价值,已经变得越来越重要.值得高兴的是,当前越来越多的人已经意识到了用数据分析决定商业策略的重要性,也都 ...
- EasyUI Tree与Datagrid联动
效果图 这是一个简单的solr检索的例子 输入关键词,显示树 选择一个节点,得到该节点下文档信息 代码: JSP: 重点是标红的URL传递 <body> <d ...
- spfa模版
#include<bits/stdc++.h> using namespace std; int n,m;//点边 int beginn; ],v[],w[]; ],nextt[]; ]; ...
- phpqrcode.php 生成二维码图片用于推广
<?php /* * PHP QR Code encoder * * This file contains MERGED version of PHP QR Code library. * It ...
- Django项目:CRM(客户关系管理系统)--77--67PerfectCRM实现CRM课程出勤排名
# classtop_urls.py # ————————64PerfectCRM实现CRM课程排名详情———————— from django.conf.urls import url from b ...
- c语言一个例子对比php学习随记
今天开始学写一些简单的C例子,发现很多和PHP不一样的地方,做个笔记. #include <stdio.h> int main(void) { int a,b; printf(" ...
- JSON操作总结(转自陈新汉)
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...
- 转:IO模型-- 同步和阻塞,异步和非阻塞的区别
源地址 http://hi.baidu.com/deep_pro/item/db0c581af1c1f17e7b5f2534 这些词之间的区别难倒了很多人,还有什么同步阻塞, 同步非阻塞, 异步阻塞, ...
- 玩转vue的slot内容分发
vue的内容分发非常适合"固定部分+动态部分"的组件的场景,固定部分可以是结构固定,也可以是逻辑固定,比如下拉loading,下拉loading只是中间内容是动态的,而拉到底部都会 ...