leetcode-695-Max Area of Island(BFS)
题目描述:
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)的更多相关文章
- 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 ...
- [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 ...
- leetcode 695 Max Area of Island 岛的最大面积
这个题使用深度优先搜索就可以直接遍历 DFS递归方法: class Solution { public: vector<vector<,},{,-},{,},{,}}; int maxAr ...
- 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 用了第一种方式, ...
- 【leetcode】Max Area of Island
国庆中秋长假过完,又要开始上班啦.先刷个题目找找工作状态. Given a non-empty 2D array grid of 0's and 1's, an island is a group o ...
- 【LeetCode】695. Max Area of Island 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- [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 ...
- 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 ...
- 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 ...
随机推荐
- jdbc和Java中的日期问题
JDBC中的日期Java.sql.Date 是继承自Java中的Java.util.Date,在实现插入的时候可以 将Java.util.Date类型的时间转换成毫秒数,date.getTime(), ...
- 马婕 2014MBA专硕考试 词汇每日一练(转)
2013-6-8 1. To ensure its sustained progress in economy, the government has _______ a series of poli ...
- Hello_Area_Description 任务三:Project Tango采集区域描述数据
Permission Dialogs for Users in Java在Java中用户使用的权限对话框 Tango works by using visual cues from the devic ...
- eclipse启动tomcat出现内存溢出错误 java.lang.OutOfMemoryError: PermGen space
发布工程后,启动tomcat出现如下内存溢出错误: java.lang.OutOfMemoryError: PermGen space ... java.lang.OutOfMemoryError: ...
- ZOJ1648 Circuit Board 2017-04-18 20:31 34人阅读 评论(0) 收藏
Circuit Board Time Limit: 2 Seconds Memory Limit: 65536 KB On the circuit board, there are lots ...
- TCP的几个状态(SYN/FIN/ACK/PSH/RST)
在TCP层,有个FLAGS字段,这个字段有以下几个标识:SYN, FIN, ACK, PSH, RST, URG. 其中,对于我们日常的分析有用的就是前面的五个字段. 含义: SYN 表示建立连接, ...
- R12.1.3 patch9239090
参考文档:Oracle E-Business Suite Release 12.1.3 Readme [ID 1080973.1]1.调整参数_disable_fast_validate=TRUEpg ...
- task4:结对项目-词频统计
结对人:周楠 思路:利用TreeMap实现key字典序,然后输出到LinkedList,然后用Comparator,实现字典值从大到小排序,但是key实现值相同的key字典序的想出的实现方法,但是一直 ...
- hibernate 中 fetch=FetchType.LAZY 懒加载失败处理
对这种懒加载问题,最后的做法是利用Spring提供的一个针对Hibernate的一个支持类,其主要意思是在发起一个页面请求时打开Hibernate的Session,一直保持这个Session,使得Hi ...
- Tcp编程常见问题及解决方法总结
问题1.粘包问题 解决方法一:TCP提供了强制数据立即传送的操作指令push,TCP软件收到该操作指令后,就立即将本段数据发送出去,而不必等待发送缓冲区满: 解决方法二:发送固定长度的消息 解决方法三 ...