当前位置:网站首页>1254. 统计封闭岛屿的数目
1254. 统计封闭岛屿的数目
2022-06-30 11:41:00 【蓝莓侠】
这道题没做出来,看题解还看了半天,首先套圈的情况是不算两个的,而且四个1不算。
做法确实没想出来。什么叫封闭,是指到了边上还没有1。有了这句话就出来了。既然这样,就遍历所有的0,向0的上下左右遍历,如果到边上就认为不是封闭。同时遍历过的节点要标记一下,防止多次遍历,这里是学的题解还用1,要是我自己想肯定是2,避免重复。
#include<stdc++.h>
using namespace std;
bool is_island;
void dfs(vector<vector<int>>&grid, int x, int y)
{
if(x<0 || x>=(int)grid.size() || y<0 || y>=(int)grid[0].size())
{
is_island = false;
return;
}
if(grid[x][y]==0)
{
grid[x][y] = 1;
dfs(grid, x+1, y);
dfs(grid, x, y+1);
dfs(grid, x-1, y);
dfs(grid, x, y-1);
}
}
int closedIsland(vector<vector<int>>& grid) {
int count = 0;
for(int i=1; i<(int)grid.size()-1;i++)
{
for(int j=1; j<(int)grid[0].size()-1; j++)
{
if(grid[i][j]==0)
{
is_island = true;
dfs(grid, i,j);
if(is_island)
count++;
}
}
}
return count;
}
int main()
{
int x[5][8] = {
{1,1,1,1,1,1,1,0},{1,0,0,0,0,1,1,0},{1,0,1,0,1,1,1,0},{1,0,0,0,0,1,0,1},{1,1,1,1,1,1,1,0}};
vector<vector<int>> a;
for(int i=0; i<5;i++)
{
vector<int>b;
for(int j=0; j<8;j++)
{
b.push_back(x[i][j]);
}
a.push_back(b);
}
cout<<closedIsland(a)<<endl;
return 0;
}
边栏推荐
- R language ggplot2 visualization: gganimate package is based on Transition_ The time function creates a dynamic scatter graph animation (GIF), and uses the labs function to add a dynamic time title to
- shell第一个命令结果传入第二个命令删除
- "War" caused by a bottle of water
- In depth analysis of Apache bookkeeper series: Part 4 - back pressure
- Typescript readonlyarray (read only array type) details
- Global Capital Market 101:国内高净值人群最好的投资标的之一BREIT
- 【云原生 | Kubernetes篇】深入了解Deployment(八)
- Multiparty cardinality testing for threshold private set-2021: Interpretation
- R language view version R package view version
- R语言ggplot2可视化分面图(facet):gganimate包基于transition_time函数创建动态散点图动画(gif)、使用labs函数为动画图添加动态时间标题
猜你喜欢

Let's talk about how to do hardware compatibility testing and quickly migrate to openeuler?
Redis - SDS simple dynamic string

wallys/3 × 3 MIMO 802.11ac Mini PCIe Wi-Fi Module, QCA9880, 2,4GHz / 5GHzDesigned for Enterprise

Review the writing software with characteristics

Paper interpretation (AGC) attributed graph clustering via adaptive graph revolution

NoSQL——Redis的配置与优化

Is the golden cycle of domestic databases coming?

建立自己的网站(13)

Stm32f407zgt6 uses SDIO mode to drive SD card

led背光板的作用是什么呢?
随机推荐
[cf] 803 div2 A. XOR Mixup
Re understand oauth2.0 protocol for joint login
对象映射 - Mapping.Mapster
Webview,ScrollView滑动冲突咋整
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
goto语句跳转未初始化变量:C2362
构造函数、类成员、析构函数调用顺序
What is erdma as illustrated by Coptic cartoon?
R语言ggplot2可视化:使用ggplot2可视化散点图、在geom_point参数中设置show_legend参数为FALSE配置不显示图例信息
Speech recognition - Fundamentals (I): introduction [speech to text]
Evaluation of IP location query interface Ⅲ
wallys/600VX – 2 × 2 MIMO 802.11ac Mini PCIe Wi-Fi Module, Dual Band, 2,4GHz / 5GHz QCA 9880
Installing onnx is very slow. Use Tsinghua image
Cache avalanche and cache penetration solutions
R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram and use scale_ color_ viridis_ D function specifies the color scheme of data points
基于视觉的机器人抓取:从物体定位、物体姿态估计到平行抓取器抓取估计
【重温经典C语言】~c语言中%x、%c、%d、%x等等等、c语言取地址符&的作用、C语言中的 联合体
Shell first command result is transferred to the second command delete
揭秘得物客服IM全链路通信过程
一瓶水引发的“战争”