Question

1 1 1 1 1 0
1 0 1 0 0 1
1 0 1 0 0 1
1 1 0 1 1 1

1 is earth, 0 is water.

i) count the number of 'islands' that the matrix has.
ii) count the number of 'lakes' that the matrix has i.e. connected clump of zeros that is entirely surrounded by a single island

Solution

这是Number of Islands的升级版。关键在于lake的定义,必须被同一个岛包围。

所以我们在dfs遍历岛的时候,给相同的岛同样的标号。然后在遍历水的时候,检查包围的岛是否是相同标号。

 import java.util.*;
import java.io.*; public class Islands {
private static final int[][] directions = {{0,1},{0,-1},{1,0},{-1,0}}; public static void main(String[] args) {
int[][] island = {
{1, 1, 1, 1, 1, 0},
{1, 0, 1, 0, 0, 1},
{1, 0, 1, 0, 0, 1},
{1, 1, 0, 1, 1, 1}
};
int m = island.length, n = island[0].length;
// Calculate island number
int color = 2;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (island[i][j] == 1) {
dfs(island, color, i, j);
color++;
}
}
}
int islandNum = color - 2;
int lakeNum = 0;
int surround = -2;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (island[i][j] == 0) {
if (dfs2(island, surround, i, j)) {
lakeNum++;
}
}
}
}
System.out.println(islandNum);
System.out.println(lakeNum); } private static void dfs(int[][] island, int color, int x, int y) {
int m = island.length, n = island[0].length;
island[x][y] = color;
int newX, newY;
for (int i = 0; i < 4; i++) {
newX = x + directions[i][0];
newY = y + directions[i][1];
if (newX < 0 || newX >= m || newY < 0 || newY >= n)
continue;
if (island[newX][newY] != 1)
continue;
dfs(island, color, newX, newY);
}
} private static boolean dfs2(int[][] island, int surround, int x, int y) {
int m = island.length, n = island[0].length;
island[x][y] = -1;
int newX, newY;
for (int i = 0; i < 4; i++) {
newX = x + directions[i][0];
newY = y + directions[i][1];
if (newX < 0 || newX >= m || newY < 0 || newY >= n)
continue;
int color = island[newX][newY];
if (color == -1)
continue;
if (color != 0) {
// This point is earth
if (surround == -2) {
surround = color;
} else if (surround != color) {
return false;
}
} else {
if (!dfs2(island, surround, newX, newY))
return false;
} }
return true;
}
}

Calculate Number Of Islands And Lakes 解答的更多相关文章

  1. [LeetCode] Number of Islands II 岛屿的数量之二

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  2. [LeetCode] Number of Islands 岛屿的数量

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  3. Leetcode 200. number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  4. 【leetcode】Number of Islands(middle)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  5. [LintCode] Number of Islands 岛屿的数量

    Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...

  6. [LeetCode] Number of Islands II

    Problem Description: A 2d grid map of m rows and n columns is initially filled with water. We may pe ...

  7. [leetcode] Number of Islands

    Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...

  8. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  9. Leetcode: Number of Islands II && Summary of Union Find

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

随机推荐

  1. hdu 5685 Problem A

    Problem Description 度熊手上有一本字典存储了大量的单词,有一次,他把所有单词组成了一个很长很长的字符串.现在麻烦来了,他忘记了原来的字符串都是什么,神奇的是他竟然记得原来那些字符串 ...

  2. java如何从方法返回多个值

    本文介绍三个方法,使java方法返回多个值. 方法1:使用集合类 方法2:使用封装对象 方法3:使用引用传递 示例代码如下: import java.util.HashMap; import java ...

  3. Android静态变量使用陷阱

    静态变量大家再熟悉不过了,本来没什么好重复的.事情起因是这样的,最近测试那边反应正在做的一个产品总是莫名其妙的显示不出某些数据,甚至闪退崩溃,仔细查了几遍发现没什么问题,最后百般周折发现在那部测试机上 ...

  4. linux上网络配置不生效的怪异现象处理

    1.在Linux上.在ifcfg-eth0上设置IP地址等信息 具体配置信息例如以下已 [root@rac01 Desktop]#more/etc/sysconfig/network-scripts/ ...

  5. Eclipse UML插件AmaterasUML的配置及使用

    AmaterasUML是个人觉得最好用的Eclipse UML插件,可以通过拖拽Java源文件,轻松生成类图结构,同时支持活动图.时序图和用例图.它的官方下载地址是:http://sourceforg ...

  6. .NET基础拾遗(4)委托为何而生?

    生活中的例子: 你早上要吃包子作为早饭,那么你可能让你爸爸或者妈妈帮你做,那你就会调用 爸爸.要包子() 或妈妈.要包子() 返回包子对象. 但是如果你爸妈不在家的时候,你只能去街上买,问题是你根本不 ...

  7. 表格table常见的边框设置和初步的立体效果

    做网页时经常会遇到表格,常见的表格如下: <style type="text/css"> .tbtest0 { width:500px; height:200px; b ...

  8. 实现VS2010整合NUnit进行单元测试

    1.下载安装NUnit(最新win版本为NUnit.3.2.1.msi) http://www.nunit.org/index.php?p=download 2.下载并安装VS的Visual Nuni ...

  9. 阐述Lambada表达式

    在C#2.0引入匿名方法之前,声明委托的唯一方法就是使用命名方法,C#2.0之后的C#3.0中开始引入了Lambda表达式取代了匿名方法. 匿名方法 要说Lambda必然离不开匿名方法,实际上,Lam ...

  10. 基于注释的Spring Security实战

    一.准备工作 预准备的工具及软件有: 1. Eclipse IDE:我使用Eclipse JEE 3.7版,即eclipse-jee-indigo-SR2-win32-x86_64.zip 2. JD ...