当前位置:网站首页>Statistics on the number of closed Islands
Statistics on the number of closed Islands
2022-06-30 12:05:00 【Blueberry man】
This problem didn't work out , I read the solution for a long time , First of all, there are not two ferrules , And four 1 not .
I really didn't think out how to do it . What is closed , It means that there is no 1. With this sentence came out . In this case , Just go through all 0, towards 0 Up, down, left and right traversal of , If you go to the edge, it is not considered closed . The nodes traversed at the same time should be marked , Prevent multiple iterations , Here is the solution to the problem of learning 1, If I want to, it must be 2, Avoid repetition .
#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;
}
边栏推荐
- 8253 counter introduction
- Speech signal processing - Fundamentals (V): Fourier transform
- A high precision positioning approach for category support components with multiscale difference reading notes
- AutoCAD - len command
- R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_x_log10函数配置X轴的数值范围为对数坐标
- ModelAtrs声音检测,基于声学特征的异响检测
- 学习redis实现分布式锁—–自己的一个理解
- R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_size函数配置数据点的大小的(size)度量调整的范围
- 用宝塔建第2个网站时网站总是报错:No input file specified.
- Evaluation of IP location query interface Ⅲ
猜你喜欢

深入解析 Apache BookKeeper 系列:第四篇—背压

来聊聊怎么做硬件兼容性检测,快速迁移到openEuler?

服务器常用的一些硬件信息(不断更新)

60 divine vs Code plug-ins!!

Another miserable day by kotlin grammar

治数如治水,数据治理和数据创新难在哪?

YOLOv5导出onnx遇到的坑

A high precision positioning approach for category support components with multiscale difference reading notes

网络营销之四大误解

OpenMLDB Meetup No.4 会议纪要
随机推荐
Stm32f407zgt6 uses SDIO mode to drive SD card
R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)
wallys/3 × 3 MIMO 802.11ac Mini PCIe Wi-Fi Module, QCA9880, 2,4GHz / 5GHzDesigned for Enterprise
Embedded SIG | 多 OS 混合部署框架
谁还记得「张同学」?
1175. 质数排列
STM32F407ZGT6使用SDIO方式驱动SD卡
ClipboardJS——开发学习总结1
Go 语言入门很简单:Go 处理 XML 文件
基于视觉的机器人抓取:从物体定位、物体姿态估计到平行抓取器抓取估计
【重温经典C语言】~c语言中%x、%c、%d、%x等等等、c语言取地址符&的作用、C语言中的 联合体
使用深度学习进行生物网络分析
Learn how to implement distributed locks in redis - my own understanding
Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)
R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram, and_ Set the alpha parameter in the point parameter to specify the transparency level of data points (points transparent
STM32 移植 RT-Thread 标准版的 FinSH 组件
Webview,ScrollView滑动冲突咋整
缓存雪崩和缓存穿透解决方案
Re understand oauth2.0 protocol for joint login
深入解析 Apache BookKeeper 系列:第四篇—背压