305. 岛屿数量 II
题目:
假设你设计一个游戏,用一个 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的更多相关文章
- Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands)
Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands) 深度优先搜索的解题详细介绍,点击 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计 ...
- LeetCode 200:岛屿数量 Number of Islands
题目: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. Given ...
- Leetcode题目200.岛屿数量(BFS+DFS+并查集-中等)
题目描述: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...
- Java实现 LeetCode 200 岛屿数量
200. 岛屿数量 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. ...
- 力扣Leetcode 200. 岛屿数量
岛屿数量 给你一个由 '1'(陆地)和 '0'(水)组成的的二维网格,请你计算网格中岛屿的数量. 岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成. 此外,你可以假设该网 ...
- [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)
695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...
- [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 ...
- DFS或BFS(深度优先搜索或广度优先搜索遍历无向图)-04-无向图-岛屿数量
给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: ...
- LeetCode 200. 岛屿数量
习题地址 https://leetcode-cn.com/problems/number-of-islands/ 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水 ...
随机推荐
- linux 统计文件夹下文件,文件夹,所有个数
统计某文件夹下文件的个数 ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数 ls -l |grep "^d"|wc -l 统计文件夹下文件 ...
- windows服务器 IIS FTP服务
一.安装ftp,如果服务器没有,去windows组件里面装一下. 安装IIS,安装FTP(版本不同,选项不相同,这两项必选) 二.装完之后在IIS管理中心创建FTP站点 创建类型 ftp站点: ...
- Apache 的多站点配置
1.修改httpd.conf 文件 Apache的主配置文件路径: D:\phpTools\Apache24\conf 用编辑器打开 httpd.conf 文件,查找 #Include conf/ex ...
- mybatis缓存,包含一级缓存与二级缓存,包括ehcache二级缓存
一,引言 首先我们要明白一点,缓存所做的一切都是为了提高性能.明白了这一点下面我们开始进入正题. 二,mybatis缓存概要 ①.mybatis的缓存有两种,分别是一级缓存和二级缓存.两者都属于查询缓 ...
- Pandas常用功能
在使用Pandas之前,需要导入pandas库 import pandas as pd #pd作为pandas的别名 常用功能如下: 代码 功能1 .DataFrame() 创建一个DataFr ...
- console控制台的用法
参考链接:https://developer.mozilla.org/zh-CN/docs/Web/API/Console 1,console.log('a'); 2,console.dir(xx); ...
- Linux运维--11.手动部署Galera Cluster
1.搭建galera集群 yum install epel-release yum install centos-release-openstack-stein #1.1 安装mariadb yum ...
- Spark存储介绍
目录 整体架构 存储相关类 应用启动时 增删改后更新元数据 获取数据存放位置 数据块的删除 RDD存储调用 数据读取 数据写入 cache & checkpoint Reference 记录一 ...
- .net代码实现上千次ping的实现
先上代码: 多线程实现ping校验: public void PingCameraNew(List<CameraMongoDto> assetlist) { ThreadPool.SetM ...
- centos6.8 安装.net core2.1 sdk 或 .net core2.1 runtime
前段时间看.net core 更更更新了,大家反应都挺好,想有机会也学习一下,正好这两天要写一个简单的服务在centos上面跑,于是决定放弃使用java,直接.net core走起来,事情进行的非常顺 ...