当前位置:网站首页>Rebate 200, the number of islands
Rebate 200, the number of islands
2022-08-07 22:40:00 【Yingtai Night Snow】
力扣200,岛屿数量
题目描述
给你一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,请你计算网格中岛屿的数量.
岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成.
此外,你可以假设该网格的四条边均被水包围.
输入输出样例
输入:grid = [
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
]
输出:1
输入:grid = [
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
]
输出:3
Tips
m == grid.lengthn == grid[i].length1 <= m, n <= 300grid[i][j]的值为'0'或'1'
解法一,使用DFS
//encounter island problem,The main method used is graph theory,深度优先搜索DFS 和广度优先搜索 BFS
//使用深度优先dfs ,时复 O(nm)空复 O(nm)
int numIslands(vector<vector<char>>& grid) {
//初始化值
int islandsRow=grid.size();
if(!islandsRow)
{
return 0;
}
int islandCow=grid[0].size();
int islandNum=0;
//Use a depth-first search to iterate over each one1的快
for(int r=0;r<islandsRow;r++)
{
for(int c=0;c<islandCow;c++)
{
if(grid[r][c]=='1')
{
islandNum++;
dfs(grid,r,c);
}
}
}
return islandNum;
}
void dfs(vector<vector<char>>&grid,int r,int c)
{
int islandRow=grid.size();
int islandCow=grid[0].size();
//Set the traversed elements to 0
grid[r][c]='0';
//Spread around this point,until all four sides are set to1
if(r-1>=0&&grid[r-1][c]=='1')
dfs(grid,r-1,c);
if(r+1<islandRow&&grid[r+1][c]=='1')
dfs(grid,r+1,c);
if(c-1>=0&&grid[r][c-1]=='1')
dfs(grid,r,c-1);
if(c+1<islandCow&&grid[r][c+1]=='1')
dfs(grid,r,c+1);
}
解法二,使用BFS
//Solution 2 uses breadth-first search
//时复 O(nm), 空复 O(min(m,n))
int numIslands2(vector<vector<char>>& grid) {
//初始化值
int islandsRow=grid.size();
if(!islandsRow)
{
return 0;
}
int islandCow=grid[0].size();
int islandNum=0;
//遍历每一个结点
for(int r=0;r<islandsRow;r++)
{
for(int c=0;c<islandCow;c++)
{
if(grid[r][c]=='1')
{
islandNum++;
grid[r][c]='0';
//Create a queue to save
queue<pair<int,int>>neighbors;
neighbors.push({
r,c});
//Use a queue to mark all sides of the node
while(!neighbors.empty())
{
auto topValue=neighbors.front();
neighbors.pop();
int row=topValue.first;
int col=topValue.second;
if(row-1>=0&&grid[row-1][col]=='1')
{
neighbors.push({
row-1,col});
grid[row-1][col]='0';
}
if(row+1<islandsRow&&grid[row+1][col]=='1')
{
neighbors.push({
row+1,col});
grid[row+1][col]='0';
}
if(col-1>=0&&grid[row][col-1]=='1')
{
neighbors.push({
row,col-1});
grid[row][col-1]='0';
}
if(col+1<islandCow&&grid[row][col+1]=='1')
{
neighbors.push({
row,col+1});
grid[row][col+1]='0';
}
}
}
}
}
return islandNum;
}
边栏推荐
- Unity编辑器拓展--Project拓展
- 论文翻译:2021_LACOPE: Latency-Constrained Pitch Estimation for Speech Enhancement
- The basics of database learning
- golang函数和方法的区别
- 《论文解读》THE CURIOUS CASE OF NEURAL TEXT DeGENERATION
- SQL教程之 10 个终极 SQL JOIN 问题和答案
- 第一次小实习记录
- The Unity editor development, Project development
- 站在数字经济浪尖:360视觉云探路中小微企业数智转型
- Add basic animation effects to UE4 Sequence (03-Use of main sequence)
猜你喜欢

golang方法的使用细节:参数默认是值拷贝,不仅仅是struct自定义数据类型也可以绑定方法、方法名称首字母大写为public权限、String()方法的使用

Unity editor extension - preview window extension

UE4 Sequence adds basic animation effects (02-switch action)

Leecode-SQL 1407. 排名靠前的旅行者

黑盒子问题

The 2019 ICPC Asia Nanjing Regional Contest(A、C、K)

2022年制冷与空调设备运行操作操作证考试题库模拟考试平台操作

The Unity editor development, Project development

Leecode-SQL 1393. Capital Gains and Losses on Stocks

第一次小实习记录
随机推荐
买股票用通达信安全吗?资金会不会被转走?
[kali-privilege escalation] (4.2.3) Social Engineering Toolkit: QR Code Combination Attack
Expansion of the Unity editor - Scene view custom operations
测试也应该具备的项目管理能力
《最新开源 随插即用》SAM 自增强注意力深度解读与实践(附代码及分析)
JDBC简介
《论文解读》THE CURIOUS CASE OF NEURAL TEXT DeGENERATION
systemd 管理工具详解
如何使用 ABAP 创建包含不同字体大小的 Word 文档
Towards a Unified View of Parameter-Efficient Transfer Learning
Is it safe to open an account with Big Wisdom?
tensorflow/serving部署keras的h5模型服务
Unity editor extension--Scene view extension
流式计算中的 Window 计算
论文翻译:2021_LACOPE: Latency-Constrained Pitch Estimation for Speech Enhancement
Redis分布式锁
【Proteus仿真】Arduino UNO+OLED12864 I2C接口跑图形库
The 2019 ICPC Asia Nanjing Regional Contest(A、C、K)
The Unity editor development, Project development
Introduction to JDBC