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 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被水包围着. 找到给定的二维数组中 ...
随机推荐
- 【Hive】自定义函数
Hive的自定义函数无法满足实际业务的需要,所以为了扩展性,Hive官方提供了自定义函数来实现需要的业务场景. 1.定义 (1)udf(user defined function): 自定义函数,特 ...
- 005——VUE中的v-text与v-html的使用
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 启动代码之开iCache
1.什么是cache,有什么用 cache是一种内存,叫高速缓存.从容量来说:CPU < 寄存器 < cache < DDR从速度来说:CPU > 寄存器 > ca ...
- 使用ettercap构建arp欺骗构建
0.准备: 安装:ettercap apt-get install cmake libncurses5-dev libssl-dev libpcap-dev libnet1-dev git clone ...
- commond prompt CD
如果要进入目前所在盘符的其他路径用cd命令可进入,但如果从c盘进入d盘等,是不用cd命令的. 如目前在c盘的任意目录,需要切换到d盘的根目录,用:“d:”命令(不含引号),如图: cd命令简介: 第一 ...
- Windows10使用Chocolatey安装mysql之后无法使用的解决办法
问题背景:使用了一台新的虚拟机,并且安装了Chocolatey作为Windows的包管理器,之后安装mysql 那么问题发生了,使用mysql命令根本没有任何反应,也不报错,但是安装的时候是提示安装成 ...
- JavaScript中的二分法插入算法
算法主体部分 var OnlineUser = { //list : 待查找的数组 //key : 待插入的值 //order : 数组的顺序 1:从小到大 0:从大到小 //start : 开始查找 ...
- everything 全盘文件查找工具及正则表达式的使用
首先需要开启 everything 工具在(字符串)查找时,对正则表达式功能的支持: [菜单栏]⇒ [Search]⇒ 勾选[Enable Regex] ctrl + i:字符大小写敏感/不敏感 1. ...
- SqlServer 数据库/数据表 拆分(分布式)【转】
通过某种特定的条件,将存放在同一个数据库中的数据分散存放到多个数据库上,实现分布存储,通过路由规则路由访问特定的数据库,这样一来每次访问面对的就不是单台服务器了,而是N台服务器,这样就可以降低单台机器 ...
- LeetCode Degree of an Array
原题链接在这里:https://leetcode.com/problems/degree-of-an-array/description/ 题目: Given a non-empty array of ...