当前位置:网站首页>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;
}
边栏推荐
- 线下门店为什么要做新零售?
- What is the function of LED backlight?
- EMC-浪涌
- Redis - ziplist compressed list
- Set up your own website (13)
- 网络营销之四大误解
- R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用labs函数为动画图添加动态时间标题(抽取frame_time信息)
- Alibaba cloud lifeifei: China's cloud database has taken the lead in many mainstream technological innovations abroad
- STM32F407ZGT6使用SDIO方式驱动SD卡
- AutoCAD - len command
猜你喜欢
一瓶水引发的“战争”
What is erdma as illustrated by Coptic cartoon?
EMC surge
NoSQL——Redis的配置与优化
[pattern recognition]
21、wpf之绑定使用小记
如何使用插件化机制优雅的封装你的请求hook
Oracle netsuite helps TCM bio understand data changes and make business development more flexible
It is said that with this, the boss opened the test overnight
R language view version R package view version
随机推荐
There are so many kinds of coupons. First distinguish them clearly and then collect the wool!
“\“id\“ contains an invalid value“
R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram and use scale_ The size function configures the measurement adjustment range of the size of the data point
R语言ggplot2可视化:使用ggplot2可视化散点图、aes函数中的size参数指定数据点的大小(point size)
[revisiting the classic C language] ~x,%c,%d,%x, etc. in C language, the role of the address character in C language, and the consortium in C language
Uncover the whole link communication process of customer service im
对象映射 - Mapping.Mapster
wallys/3 × 3 MIMO 802.11ac Mini PCIe Wi-Fi Module, QCA9880, 2,4GHz / 5GHzDesigned for Enterprise
goto语句跳转未初始化变量:C2362
爱可可AI前沿推介(6.30)
科普达人丨漫画图解什么是eRDMA?
Shell first command result is transferred to the second command delete
R language de duplication operation unique duplicate filter
60 divine vs Code plug-ins!!
缓存雪崩和缓存穿透解决方案
构造函数、类成员、析构函数调用顺序
zabbix监控TCP连接个数
"War" caused by a bottle of water
21、wpf之绑定使用小记
数据库 级联操作