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. 图解 ASP.NET Core开发环境准备

    2016年6月28日微软宣布发布 .NET Core 1.0.ASP.NET Core 1.0 和 Entity Framework Core 1.0. .NET Core是微软在两年前发起的开源跨平 ...

  2. 再论typedef

    typedef 定义(或者叫重命名)类型而不是变量 1.类型是一个数据模板,变量是一个实在的数据.类型是不占内存的,而变量是占内存的. 2.面向对象的语言中:类型的类class,变量就是对象. #in ...

  3. json结构更改的方法 把date有数据的分类

    data=[ { "content": "如何走进智障儿童的内心", "title": "如何走进智障儿童的内心", & ...

  4. Struts12---文件的下载

    01.创建一个下载的页面  (我们的下载是把文件的路径写成固定的) <body> <form action="user/download" method=&quo ...

  5. 条款1:将c++视作一个语言联邦

    c++是一个多重泛型编程语言,其所支持的泛型有: 面向过程编程(procedual) 面向对象编程(object-oriented) 面向函数编程(functional) 泛型编程(generic) ...

  6. react table dropdown

    <DropdownButton type="primary" trigger={['click']} onClick={() => this.detail(item)} ...

  7. Could not find a valid gem 'rails' (>= 0), here is why

    很长一段时间之前 Ruby Rails入门--windows下搭建Ruby Rails Web开发环境 ,由于后来将Ruby的安装文件从 C 盘移动到了 D 盘,也修改了 Path 环境变量,ruby ...

  8. Ganymed实现基本的自动化部署API

    Ganymed SSH-2 for Java是一个纯Java实现的SHH2库,官网为http://www.ganymed.ethz.ch/ssh2/,最新的更新时间为2006年10月,在用之前,请仔细 ...

  9. Asphyre Sphinx is a cross-platform framework for developing 2D/3D video games and interactive business applications

    Introduction Introduction Asphyre Sphinx is a cross-platform framework for developing 2D/3D video ga ...

  10. 洛谷 P2205 [USACO13JAN]画栅栏Painting the Fence

    传送门 题目大意: 开始站在原点,给出一系列操作 x L/R,表示向左或向右走几步. 最多会移动到离原点1,000,000,000单位远的地方. n次操作,n<=100000 问走过k次的地方有 ...