题目:

假设你设计一个游戏,用一个 m 行 n 列的 2D 网格来存储你的游戏地图。

起始的时候,每个格子的地形都被默认标记为「水」。我们可以通过使用 addLand 进行操作,将位置 (row, col) 的「水」变成「陆地」。

你将会被给定一个列表,来记录所有需要被操作的位置,然后你需要返回计算出来 每次 addLand 操作后岛屿的数量。

注意:一个岛的定义是被「水」包围的「陆地」,通过水平方向或者垂直方向上相邻的陆地连接而成。你可以假设地图网格的四边均被无边无际的「水」所包围。

请仔细阅读下方示例与解析,更加深入了解岛屿的判定。

示例:

输入: m = 3, n = 3, positions = [[0,0], [0,1], [1,2], [2,1]]
输出: [1,1,2,3]
解析:

起初,二维网格 grid 被全部注入「水」。(0 代表「水」,1 代表「陆地」)

0 0 0
0 0 0
0 0 0
操作 #1:addLand(0, 0) 将 grid[0][0] 的水变为陆地。

1 0 0
0 0 0 Number of islands = 1
0 0 0
操作 #2:addLand(0, 1) 将 grid[0][1] 的水变为陆地。

1 1 0
0 0 0 岛屿的数量为 1
0 0 0
操作 #3:addLand(1, 2) 将 grid[1][2] 的水变为陆地。

1 1 0
0 0 1 岛屿的数量为 2
0 0 0
操作 #4:addLand(2, 1) 将 grid[2][1] 的水变为陆地。

1 1 0
0 0 1 岛屿的数量为 3
0 1 0
拓展:

你是否能在 O(k log mn) 的时间复杂度程度内完成每次的计算?(k 表示 positions 的长度)

解答:

方法1:

首先我想到的就是dfs,每次加入陆地的时候考察一共有多少岛屿。但不能每次加入陆地都遍历全部矩阵,这样复杂度太高了,需要剪枝。

我的思路是这样:

每个独立的岛屿有独特的编号。每次addland的时候,遍历周边四个位置。

1.如果有一个相邻岛屿,岛屿数--,保存这个相邻岛屿的编号。

2.如果有第二个、甚至第三、四个相邻岛屿,就把之后的相邻的岛屿都用dfs遍历,全部更新为第一个相邻岛屿的编号。每次仍然记得总岛屿数--。

3.如果没有相邻岛屿,直接赋岛屿编号,全局的岛屿编号加一。

代码:

 class Solution {
public:
vector<vector<int>> d={{,-},{,},{,},{-,}};
int m,n;
bool check(int x,int y){
if(x< or x>=m or y< or y>=n){
return false;
}
return true;
}
vector<int> numIslands2(int m, int n, vector<vector<int>>& positions) {
this->m=m;
this->n=n;
vector<vector<int>> matrix(m,vector<int>(n,));
int val=;//下一个有效的岛屿编号
vector<int>res(positions.size(),);
res[]=;
matrix[positions[][]][positions[][]]=val++;
for(int i=;i<positions.size();++i){
if(matrix[positions[i][]][positions[i][]]!=){//本身就是岛屿
res[i]=res[i-];
continue;
}
res[i]=res[i-]+;
int x=positions[i][],y=positions[i][],cur_val=;
// cout<<"addland "<<x<<" "<<y<<endl;
for(int j=;j<;++j){
int new_x=x+d[j][],new_y=y+d[j][];
if(check(new_x,new_y) and matrix[new_x][new_y]!=cur_val){
if(cur_val==){//相邻的第一片岛屿
cur_val=matrix[new_x][new_y];
res[i]--;
}
else if(matrix[new_x][new_y]!=){//相邻的第二/三/四片岛屿
dfs(matrix,new_x,new_y,matrix[new_x][new_y],cur_val);
res[i]--;
}
}
}
matrix[x][y]=cur_val==?val++:cur_val;
// for(auto& vec:matrix){
// for(int x:vec){
// cout<<x<<" ";
// }cout<<endl;
// }
}
return res;
} void dfs(vector<vector<int>>& matrix,int x,int y,int ori_val,int new_val){
// cout<<"dfs: "<<x<<" "<<y<<" "<<ori_val<<" "<<new_val<<endl;
//将ori_val改为new_val
matrix[x][y]=new_val;
int new_x,new_y;
for(int i=;i<;++i){
new_x=x+d[i][],new_y=y+d[i][];
if(check(new_x,new_y) and matrix[new_x][new_y]==ori_val){
dfs(matrix,new_x,new_y,ori_val,new_val);
}
}
}
};

方法2:

使用并差集。

代码:

 class Solution {
public:
vector<vector<int>> dif={{,},{,-},{,},{-,}};
//并差集
vector<int> parents;
int islands = ;
int getParent(int child){
if(child!=parents[child]){
parents[child]=getParent(parents[child]);
}
return parents[child];
}
void merge(int x, int y){
int px = getParent(x);
int py = getParent(y);
if(px != py){
parents[px] = py;
islands--;
}
}
vector<int> numIslands2(int m, int n, vector<vector<int>>& positions) {
vector<int> res;
parents.assign(m*n,-);
for(auto& coordinate:positions){
int row=coordinate[],col=coordinate[],index=row*n+col;
if(parents[index]==-){//水
++islands;
parents[index]=index;
for(const auto& d:dif){
int new_index=(row+d[])*n+col+d[];
if(row+d[]<m and row+d[]>= and col+d[]<n and col+d[]>= and parents[new_index]!=-){
merge(new_index,index);
}
}
}
res.push_back(islands);
}
return res;
}
};


305. 岛屿数量 II的更多相关文章

  1. Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands)

    Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands) 深度优先搜索的解题详细介绍,点击 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计 ...

  2. LeetCode 200:岛屿数量 Number of Islands

    题目: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. Given ...

  3. Leetcode题目200.岛屿数量(BFS+DFS+并查集-中等)

    题目描述: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...

  4. Java实现 LeetCode 200 岛屿数量

    200. 岛屿数量 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. ...

  5. 力扣Leetcode 200. 岛屿数量

    岛屿数量 给你一个由 '1'(陆地)和 '0'(水)组成的的二维网格,请你计算网格中岛屿的数量. 岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成. 此外,你可以假设该网 ...

  6. [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)

    695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...

  7. [LeetCode] 305. Number of Islands II 岛屿的数量 II

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

  8. DFS或BFS(深度优先搜索或广度优先搜索遍历无向图)-04-无向图-岛屿数量

    给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: ...

  9. LeetCode 200. 岛屿数量

    习题地址 https://leetcode-cn.com/problems/number-of-islands/ 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水 ...

随机推荐

  1. 简单ts文件结构

    一.ts文件结构 DEMO{ 1. .vscode:特有文件夹,调试的配置文件,启动浏览器 2. Js:放ts编译后的文件,不管 3. Ts:放ts文件,敲代码 4. tsconfig.json:ts ...

  2. 标准 I/O 和管道

    1.标准输入和输出1>程序:指令+数据(指令服务于数据) 读入数据:input 输出数据:output 2>三种 I/O 设备 Linux 给程序提供三种 I/O 设备 标准输入(STDI ...

  3. 2000_narrowband to wideband conversion of speech using GMM based transformation

    论文地址:基于GMM的语音窄带到宽带转换 博客作者:凌逆战 博客地址:https://www.cnblogs.com/LXP-Never/p/12151027.html 摘要 在不改变现有通信网络的情 ...

  4. C# 根据天、周、月汇总统计生成统计报表

    先看核心代码: public List<DataEntity> SearchShopSalesReport(DateTimeOffset? dateFrom, DateTimeOffset ...

  5. vue 路由过渡动效

    <router-view> 是基本的动态组件,所以我们可以用 <transition> 组件给它添加一些过渡效果: <transition name="slid ...

  6. Maven 多模块开发

    多模块开发在大项目中用得比较多,把一个项目拆分为多个模块,一个小组开发一个模块. 比如微服务,一个服务一个模块:比如ssm,持久层(dao)一个模块,业务层一个模块(service).视图层(mvc. ...

  7. 批量unzip一大堆压缩文件进行文件查询的办法.

    1. 公司里面开发提交的补丁存在问题. 需要找出来 哪些文件有问题 最简单的办法, 想将一对文件 转移到一个目录里面去 然后创建一个 shell 脚本执行解压缩的操作 for i in `ls *.g ...

  8. ubuntu 安装mysql数据库

    apt方式安装 官网参考: https://dev.mysql.com/doc/mysql-apt-repo-quick-guide/en/ 执行命令: sudo wget https://dev.m ...

  9. Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set…

    php打印小票错误提示:Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activate ...

  10. 【React Native】集成声网Agora语音通讯

    前言: 公司的产品是一款基于社交的内容聊天软件,需要集成语音通讯功能,在写iOS原生项目时,用到的就是Agora SDK,现在写React Native也直接采用了Agora的库. 集成iOS.And ...