当前位置:网站首页>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语言ggplot2可视化:使用ggplot2可视化散点图、在geom_point参数中设置alpha参数指定数据点的透明度级别(points transparent、从0到1)
- 科普达人丨漫画图解什么是eRDMA?
- 数据库 自动增长
- R language view version R package view version
- What is erdma as illustrated by Coptic cartoon?
- 缓存雪崩和缓存穿透解决方案
- go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
- MySQL 内置函数
- Boost研究:Boost Log
- Quel est le rôle du rétroéclairage LED?
猜你喜欢

What is erdma as illustrated by Coptic cartoon?

Pointdistiller: structured knowledge distillation for efficient and compact 3D detection

服务器常用的一些硬件信息(不断更新)
Redis - ziplist compressed list

AUTOCAD——LEN命令

【模式识别大作业】
![[pattern recognition]](/img/b1/dcb444cbf40a43eeb7f7b233d7741a.png)
[pattern recognition]

R language view version R package view version

60 个神级 VS Code 插件!!

60 divine vs Code plug-ins!!
随机推荐
The first batch in China! Alibaba cloud native data Lake products have passed the evaluation and certification of the ICT Institute
Global Capital Market 101:国内高净值人群最好的投资标的之一BREIT
Learn how to implement distributed locks in redis - my own understanding
The operation and maintenance security gateway (Fortress machine) of Qiming star group once again won the first place!
网络营销之四大误解
Redis - ziplist compressed list
Speech recognition - Fundamentals (I): introduction [speech to text]
Typescript readonlyarray (read only array type) details
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
这些电影中的科幻构想,已经用AI实现了
shell第一个命令结果传入第二个命令删除
再不上市,旷视科技就熬不住了
STM32F407ZGT6使用SDIO方式驱动SD卡
R language view version R package view version
Boost研究:Boost Log
Review the writing software with characteristics
Dameng data rushes to the scientific innovation board, or becomes the "first share of domestic database" in the A-share market
据说用了这个,老板连夜把测试开了
MySQL 内置函数
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