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. poj1011 Sticks(dfs+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110416   Accepted: 25331 Descrip ...

  2. phpcms:五、网站首页(index.html)

    1.经典案例:图文列表:{pc:content  action="position" posid="2" order="listorder DESC& ...

  3. MySql按日期时间段进行统计(前一天、本周、某一天、某个时间段)

    在mysql数据库中,常常会遇到统计当天的内容.例如,在user表中,日期字段为:log_time 统计当天 sql语句为: select * from user where date(log_tim ...

  4. log4j 突然不打印记录,提示:No appenders could be found for logge,处理方法

    log4j 一直都在使用正常,log4j.xml配置.代码都没有修改,突然不打印记录,出现下面提示: log4j:WARN No appenders could be found for logger ...

  5. pyqt搜索指定信息 github处找到,谢谢这位朋友的帮助了

    def tabunqi(self,text):    #第一遍添加之后,不提示,当第二次添加相同的数据时,就提示下    text1=str(text)    items = self.downwid ...

  6. js练习【DOM操作】

    完成效果: 演示地址:http://codepen.io/anon/pen/jPbYWq HTML: <!DOCTYPE html> <html lang="en" ...

  7. iOS 消息推送原理

    一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图: 1. Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Prov ...

  8. NSLog用法,打印日志

    要输出的格式化占位:   %@ 对象 %d, %i 整数 %u   无符整形 %f 浮点/双字 %x, %X 二进制整数 %o 八进制整数 %zu size_t %p 指针 %e   浮点/双字 (科 ...

  9. c++ 11 多线程教学(1)

    本篇教学代码可在GitHub获得:https://github.com/sol-prog/threads. 在之前的教学中,我展示了一些最新进的C++11语言内容: 1. 正则表达式(http://s ...

  10. Oracle CheckPoint进程

    在实例经过分配内存结构,加载控制文件后,然后要打开数据库的时候,需要做到控制文件,数据文件,联机重做日志保持相互状态一致性,数据库才可以打开.当数据库发生实例不正常关闭时(比如系统掉电或者Shutdo ...