给定一个包含了一些 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:

  1. class Solution {
  2. public:
  3. vector<vector<int> > visit;
  4. int dx[4] = {1, -1, 0, 0};
  5. int dy[4] = {0, 0, 1, -1};
  6. int r;
  7. int c;
  8. int maxAreaOfIsland(vector<vector<int> >& grid) {
  9. r = grid.size();
  10. if(r == 0)
  11. return 0;
  12. c = grid[0].size();
  13. visit = vector<vector<int> >(r, vector<int>(c, 0));
  14. int res = 0;
  15. for(int i = 0; i < r; i++)
  16. {
  17. for(int j = 0; j < c; j++)
  18. {
  19. if(visit[i][j] != 1 && grid[i][j] == 1)
  20. {
  21. visit[i][j] = 1;
  22. res = max(res, BFS(grid, i, j));
  23. }
  24. }
  25. }
  26. return res;
  27. }
  28. int BFS(vector<vector<int> >& grid, int x, int y)
  29. {
  30. queue<pair<int, int> > q;
  31. q.push(make_pair(x, y));
  32. int cnt = 0;
  33. while(!q.empty())
  34. {
  35. int xx = q.front().first;
  36. int yy = q.front().second;
  37. cnt++;
  38. q.pop();
  39. for(int i = 0; i < 4; i++)
  40. {
  41. int newx = xx + dx[i];
  42. int newy = yy + dy[i];
  43. if(newx < 0 || newx >= r || newy < 0 || newy >= c)
  44. continue;
  45. if(visit[newx][newy] == 1)
  46. continue;
  47. if(grid[newx][newy] == 0)
  48. continue;
  49. visit[newx][newy] = 1;
  50. q.push(make_pair(newx, newy));
  51. }
  52. }
  53. return cnt;
  54. }
  55. };

DFS:

  1. class Solution {
  2. public:
  3. vector<vector<int> > visit;
  4. int dx[4] = {1, -1, 0, 0};
  5. int dy[4] = {0, 0, 1, -1};
  6. int r;
  7. int c;
  8. int maxAreaOfIsland(vector<vector<int> >& grid) {
  9. r = grid.size();
  10. if(r == 0)
  11. return 0;
  12. c = grid[0].size();
  13. visit = vector<vector<int> >(r, vector<int>(c, 0));
  14. int res = 0;
  15. for(int i = 0; i < r; i++)
  16. {
  17. for(int j = 0; j < c; j++)
  18. {
  19. if(visit[i][j] != 1 && grid[i][j] == 1)
  20. {
  21. res = max(res, DFS(grid, i, j));
  22. }
  23. }
  24. }
  25. return res;
  26. }
  27. int DFS(vector<vector<int> >& grid, int x, int y)
  28. {
  29. int cnt = 1;
  30. visit[x][y] = 1;
  31. for(int i = 0; i < 4; i++)
  32. {
  33. int newx = x + dx[i];
  34. int newy = y + dy[i];
  35. if(newx < 0 || newx >= r || newy < 0 || newy >= c)
  36. continue;
  37. if(visit[newx][newy] == 1)
  38. continue;
  39. if(grid[newx][newy] == 0)
  40. continue;
  41. cnt += DFS(grid, newx, newy);
  42. }
  43. return cnt;
  44. }
  45. };

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. Mysql8+mybatisGenerator (mysql 8的逆向工程)

    最近试了一下mysql8的逆向工程工具 1.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOC ...

  2. ES6 class继承

    ES6 class继承 class类的继承 class可以通过extends关键字实现继承,这笔ES5的通过修改原型连实现继承要清晰和方便很多. class Point{ } class ColorP ...

  3. 详解如何挑战4秒内百万级数据导入SQL Server(转)

      对于大数据量的导入,是DBA们经常会碰到的问题,在这里我们讨论的是SQL Server环境下百万级数据量的导入,希望对大家有所帮助.51CTO编辑向您推荐<SQL Server入门到精通&g ...

  4. combobox 的onLoadSuccess执行两次解决办法和 取值赋值

    加红色字部分 jsp <input class="easyui-combobox" id="keshi" name="keshi" v ...

  5. 谈谈数据库sql编写

    本文主要给初学者关于关系数库的一个浮光掠影式的介绍,如果想深入理解,必须对于下文提到的每个内容单独深入学习! it-information technology的简称,中文是信息机技术,信息其实就是数 ...

  6. 建造者模式(Builder)(生成器模式)(框架化)

    建造者模式将一个复杂对象的构建与其表示分离. 将复杂对象进行框架化,将同类的对象编造进同一个制造流程.同类·对象会有一样的框架. 而由于各部分的实现细节有所不同,所生产出来的产品会有所不同.从而有不同 ...

  7. RabbitMq知识点总结

    一.RabbitMQ简介 AMQP,即Advanced Message Queuing Protocol,一个提供统一消息服务的应用层标准高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间 ...

  8. mybatis框架学习:

    一.什么是框架 它是我们软件开发中的一套解决方案,不同的框架解决的是不同的问题 使用框架的好处: 框架封装了很多的细节,使开发者可以使用极简的方式实现功能 大大提高开发效率 二.三层框架 表现层: 用 ...

  9. Liferay 7:Liferay DXP全套教程内附源码

    分享是美德 都是英文教程,有不明白的问题可以随时咨询我. http://www.javasavvy.com/liferay-7-hooks-tutorials/

  10. Frank Dellaert Slam Speech 20190708

    Georgia Institue of Tecknology 3D Models from Community Databases Spatiotemporal Reconstruction 4D C ...