leetcode695
public class Solution
{
public int MaxAreaOfIsland(int[,] grid)
{
var row = grid.GetLength();//
var coloum = grid.GetLength();//
bool[,] Visited = new bool[row, coloum];
Queue<KeyValuePair<int, int>> Q = new Queue<KeyValuePair<int, int>>(); int max = ;
for (int i = ; i < row; i++)
{
for (int j = ; j < coloum; j++)
{
int island = ;
if (!Visited[i, j])
{
Q.Enqueue(new KeyValuePair<int, int>(i, j));
}
while (Q.Any())
{
var pair = Q.Dequeue();
if (Visited[pair.Key, pair.Value])//此节点已经处理过
{
continue;
}
else
{
Visited[pair.Key, pair.Value] = true;//处理当前节点
if (grid[pair.Key, pair.Value] == )
{
island++;
//将此节点是1,则将这个节点的上下左右都取出来,
var up_point = new KeyValuePair<int, int>(pair.Key - , pair.Value);
var left_point = new KeyValuePair<int, int>(pair.Key, pair.Value - );
var down_point = new KeyValuePair<int, int>(pair.Key + , pair.Value);
var right_point = new KeyValuePair<int, int>(pair.Key, pair.Value + );
//如果是合法数值,则进队
if (up_point.Key >= && !Visited[up_point.Key, up_point.Value] && grid[up_point.Key, up_point.Value] == )
{
Q.Enqueue(up_point);
}
if (left_point.Value >= && !Visited[left_point.Key, left_point.Value] && grid[left_point.Key, left_point.Value] == )
{
Q.Enqueue(left_point);
}
if (down_point.Key <= row - && !Visited[down_point.Key, down_point.Value] && grid[down_point.Key, down_point.Value] == )
{
Q.Enqueue(down_point);
}
if (right_point.Value <= coloum - && !Visited[right_point.Key, right_point.Value] && grid[right_point.Key, right_point.Value] == )
{
Q.Enqueue(right_point);
}
}
} }
//当前岛屿查询结束,更新最大岛
if (max < island)
{
max = island;
}
}
} return max;
}
}
本题属于分支限界的题目,广度优先进行搜索。利用访问的数组Visited,记录已经走过的路径,以减少重复计算。
leetcode695的更多相关文章
- [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 ...
- Leetcode695.Max Area of Island岛屿的最大面积
给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被水包围着. 找到给定的二维数组中 ...
随机推荐
- 【spark】常用转换操作:keys 、values和mapValues
1.keys 功能: 返回所有键值对的key 示例 val list = List("hadoop","spark","hive",&quo ...
- WCF基础:绑定(二)
在WCF的绑定体系中,经常会碰到ICommunicationObject接口,无论是IChannel接口还是IChannelListener/IChannelFactory接口都继承了ICommuni ...
- MVC 框架中的缓存
在程序中加入缓存的目的很多是为了提高程序的性能,提高数据的查找效率,在MVC框架中也引入了非常多的缓存,比如Controller的匹配查找,Controller,ControllerDescripto ...
- LeetCode OJ:Search a 2D Matrix(二维数组查找)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- thinking java
public class CrossContainerIteration{ public static void display(Iterator<Pet> it){ while(it.h ...
- mac 开启SSH服务
SSH服务适用于所有类UNIX系统,例如Ubuntu.CentOS.RedHat,包括Mac OX,在这里简单介绍一下它的部分适用方法. 首先,介绍一下Mac OX,因为SSH在苹果系统上嵌入了,不需 ...
- (二)canvas边框问题
lineWidth 设置边框的大小 fillStyle 设置div的颜色 strokeStyle 设置边框的颜色 注: 边框在不设置的情况下默认为1px 黑色,但是x,y轴的距离是以图形的正中心为原始 ...
- Vue脚手架搭建过程
1.使用npm全局安装vue-cli(前提是你已经安装了nodejs,否则你连npm都用不了),在cmd中输入一下命令 npm install --global vue-cli 安装完成后,创建自己的 ...
- SMMS 2016 啟用深色主題
1.用文本類編輯器 打開C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\ManagementStudio目錄下的 ssms.pkg ...
- C#获取文件的MD5码
using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Send ...