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的更多相关文章

  1. [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 ...

  2. Leetcode695.Max Area of Island岛屿的最大面积

    给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被水包围着. 找到给定的二维数组中 ...

随机推荐

  1. cassandra框架模型之一——Colum排序,分区策略 Token,Partitioner bloom-filter,HASH

    转自:http://asyty.iteye.com/blog/1202072 一.Cassandra框架二.Cassandra数据模型 Colum / Colum Family, SuperColum ...

  2. python基础之递归,声明式编程,面向对象(一)

    在函数内部,可以调用其他函数,如果一个函数在内部调用自身本身,这个函数就是递归函数.递归效率低,需要在进入下一次递归时保留当前的状态,解决方法是尾递归,即在函数的最后一步(而非最后一行)调用自己,但是 ...

  3. LeetCode OJ:Count Primes(质数计数)

    Count the number of prime numbers less than a non-negative number, n. 计算小于n的质数的个数,当然就要用到大名鼎鼎的筛法了,代码如 ...

  4. JavaScript中的二分法插入算法

    算法主体部分 var OnlineUser = { //list : 待查找的数组 //key : 待插入的值 //order : 数组的顺序 1:从小到大 0:从大到小 //start : 开始查找 ...

  5. 旧书重温:0day2【8】狙击windows的异常处理实验

    现在进入0day2的第六章内容 其中第六章的书本内容我都拍成了图片格式放在了QQ空间中(博客园一张一传,太慢了)http://user.qzone.qq.com/252738331/photo/V10 ...

  6. (五)js数组方法二

    一:数组方法 var arr = []; 1.arr.push()//给数组末尾<添加>元素 2.arr.unshift()//给数组头部<添加>元素 3.arr.shift( ...

  7. android 进制转换方法

    import android.util.Log; public class CommandHelper { public static String intToHexString(int value) ...

  8. 使用IntelliJ IDEA开发SpringMVC网站的学习

    最近开始了“使用IntelliJ IDEA开发SpringMVC网站”的学习,有幸看到一份非常完善的学习资料,笔者非常用心的详细注释了一份关于博客的开发过程和细节,并且在评论中回复大家提出的问题,非常 ...

  9. 【转载】对一致性Hash算法,Java代码实现的深入研究

    原文地址:http://www.cnblogs.com/xrq730/p/5186728.html 一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细 ...

  10. BZOJ3040 最短路(road)

    题意 N个点,M条边的有向图,求点1到点N的最短路(保证存在). \(1 \leq N \leq 1000000,1 \leq M \leq 10000000\) 前T条边采用如下方式生成: 初始化x ...