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. iframe框架

    在一个页面里做一个区域引入另一个页面的方法: <a href="http://www.baidu.com" target="in">百度</a ...

  2. winform 中 MessageBox 用法大全

    (转自:http://blog.csdn.net/xuenzhen123/article/details/4808005) MessageBox.Show()共有21中重载方法.现将其常见用法总结如下 ...

  3. 使用ettercap构建arp欺骗构建

    0.准备: 安装:ettercap apt-get install cmake libncurses5-dev libssl-dev libpcap-dev libnet1-dev git clone ...

  4. jQuery的width()、innerWidth()、outerWidth()方法

    width(): 不包括元素的外边距(margin).内边距(padding).边框(border)等部分的宽度. innerWidth(): 包括元素的内边距(padding),但不包括外边距(ma ...

  5. vsftp中的local_umask和anon_umask

    umask是unix操作系统的概念,umask决定目录和文件被创建时得到的初始权限umask = 022 时,新建的目录 权限是755,文件的权限是 644umask = 077 时,新建的目录 权限 ...

  6. Android 进阶8:进程通信之 Binder 机制浅析

    读完本文你将了解: IBinder Binder Binder 通信机制 Binder 驱动 Service Manager Binder 机制跨进程通信流程 Binder 机制的优点 总结 Than ...

  7. Asyphre for Delphi Xe4 Demo Running on iPad!

    Asyphre 4.0 . customize bitmap with asvf file.  compiled done!  check the video!

  8. 作为一名Java开发工程师需要掌握的专业技能

    在学习Java编程完之后,学员们面临的就是就业问题.作为一名Java开发工程师,企业在招聘的时候,也是有一定的标准的. 为了帮助大家更好的找到适合自己的工作,在这里分享了作为一名Java开发工程师需要 ...

  9. 类里边的构造函数可以被private修饰,在类的内部创建对象。利用这种特性创建单类模式

  10. cocos2d-x3.16 default模式项目 Android Studio C++文件编译失败问题

    gradle sync正常,但是在编译的时候几乎自己写的Classes里全部c++文件的最后一行都在报错,原来是3.16 模板cpp-template-default内的Android.mk文件内这一 ...