当前位置:网站首页>1020. 飞地的数量
1020. 飞地的数量
2022-06-30 11:41:00 【蓝莓侠】
这题很简单,也是普通的遍历。在四个边上把所有的1遍历一遍,就知道了所有能到边上的点,和grid比较一下就能算出有多少点能走出这个矩阵。再统计一下一共有多少个点,做个减法就OK了。
#include<stdc++.h>
using namespace std;
int countt = 0;
void dfs(std::vector<vector<int>>&grid, int x, int y)
{
if(x<0 || x>=(int)grid.size() || y<0 || y>=(int)grid[0].size())
return;
if(grid[x][y]==1)
{
countt++;
grid[x][y]=2;
dfs(grid, x+1, y);
dfs(grid, x, y+1);
dfs(grid, x-1, y);
dfs(grid, x, y-1);
}
}
int numEnclaves(std::vector<vector<int>>& grid) {
if(grid.empty())
return 0;
int onecount = 0;
for(int i=0; i<(int)grid.size(); i++)
for(int j=0; j<(int)grid[0].size(); j++)
if(grid[i][j]==1)
onecount++;
for(int i=0; i<(int)grid.size(); i++)
{
if(grid[i][0]==1)
dfs(grid,i,0);
if(grid[i][grid[0].size()-1]==1)
dfs(grid,i,grid[0].size()-1);
}
for(int j=0; j<(int)grid[0].size(); j++)
{
if(grid[0][j]==1)
dfs(grid,0,j);
if(grid[grid.size()-1][j]==1)
dfs(grid,grid.size()-1,j);
}
return onecount - countt;
}
int main()
{
int x[4][4] = {
{0,1,1,0},{0,0,1,0},{0,0,1,0},{0,0,0,0}};
vector<vector<int>> a;
for(int i=0; i<4; i++)
{
vector<int>b;
for(int j=0; j<4; j++)
{
b.push_back(x[i][j]);
}
a.push_back(b);
}
cout<<numEnclaves(a)<<endl;
return 0;
}
边栏推荐
- “\“id\“ contains an invalid value“
- Review the writing software with characteristics
- 数据库连接池 druid
- R语言ggplot2可视化:使用ggplot2可视化散点图、aes函数中的colour参数指定不同分组的数据点使用不同的颜色显示
- Constructor, class member, destructor call order
- R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用labs函数为动画图添加动态时间标题(抽取frame_time信息)
- koa - 洋葱模型浅析
- 自定义一个注解来获取数据库的链接
- Speech signal processing - Fundamentals (V): Fourier transform
- Alibaba cloud database represented by polardb ranks first in the world
猜你喜欢

谁还记得「张同学」?

Another miserable day by kotlin grammar

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

Database connection pool Druid

EMC surge

R language de duplication operation unique duplicate filter

Uncover the whole link communication process of customer service im

OpenMLDB Meetup No.4 会议纪要

Le talent scientifique 丨 dessins animés qu'est - ce qu'erdma?

The first batch in China! Alibaba cloud native data Lake products have passed the evaluation and certification of the ICT Institute
随机推荐
安装onnx很慢,使用清华镜像
YOLOv5导出onnx遇到的坑
c# 怎样能写个sql的解析器
自定义一个注解来获取数据库的链接
NoSQL——Redis的配置与优化
The sci-fi ideas in these movies have been realized by AI
Goto statement jump uninitialized variable: c2362
goto语句跳转未初始化变量:C2362
R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_color_viridis_d函数指定数据点的配色方案
论文解读(AGC)《Attributed Graph Clustering via Adaptive Graph Convolution》
他是上海两大产业的第一功臣,却在遗憾中默默离世
Is the golden cycle of domestic databases coming?
治数如治水,数据治理和数据创新难在哪?
基于视觉的机器人抓取:从物体定位、物体姿态估计到平行抓取器抓取估计
wallys/IPQ8074a/2x(4×4 or 8×8) 11AX MU-MIMO DUAL CONCURRENT EMBEDDEDBOARD
学习redis实现分布式锁—–自己的一个理解
Le talent scientifique 丨 dessins animés qu'est - ce qu'erdma?
Cache avalanche and cache penetration solutions
科普达人丨漫画图解什么是eRDMA?
MySQL 表的内连和外连