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. TCP/IP 中文译名为传输控制协议/因特网互联协议,又叫网络通讯协议

    原文地址:http://hi.baidu.com/albyuyrgqgbbhoq/item/65006d2d002ab33195f62ba1 TCP/IP(Transmission Control P ...

  2. poj1006 ( hdu1370 ):中国剩余定理裸题

    裸题,没什么好说的 第一个中国剩余定理 写暴力都过了..可见这题有多水 代码: #include<iostream> #include<stdio.h> #include< ...

  3. cocos2dx lua 加密

    cocos2dx-lua项目发布时,为了保护lua源码,需要对lua进行加密.通常分为两种方式:加密文件和编译为字节码. 1.加密文件 前提是你不用luajit,而使用lua.这样这种方法是真正加密, ...

  4. oracle10.2 dblink impd 同库不同用户复制数据

    同库不同用户复制数据 1.授权用户导入表权限; SQL> grant exp_full_database to system; SQL> commit; 2.创建dblink; SQL&g ...

  5. inline-block及解决空白间距

    參考:http://www.jb51.net/css/76707.html http://www.webhek.com/remove-whitespace-inline-block/ inline-b ...

  6. oracle 同样数据删除(仅仅留一条)

    DELETE FROM reg_user t1 WHERE user_name='9527008' and rowid > ( SELECT min(rowid) FROM location t ...

  7. C# 初识Ref和Out

    首先:两者都是按地址传递的,使用后都将改变原来参数的数值. 其次:ref可以把参数的数值传递进函数,但是out是要把参数清空,就是说你无法把一个数值从out传递进去的,out进去后,参数的数值为空,所 ...

  8. ARM体系结构_DAY2

    程序状态寄存器(CPSR) Mode位[4:0]:处理器模式为 USER模式不能直接切换到特权模式,在特权模式下可以直接修改mode位[4:0]为10000,切换到USER模式. T bit位[5]: ...

  9. WebApi2官网学习记录---Html Form Data

    HTML Forms概述 <form action="api/values" method="post"> 默认的method是GET,如果使用GE ...

  10. Oracle查找重复数据

    Select * From 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having Count(*)>1)