当前位置:网站首页>1020. number of enclaves
1020. number of enclaves
2022-06-30 12:05:00 【Blueberry man】
The question is very simple , It is also an ordinary traversal . Put all the... On the four sides 1 Go through it , You know all the points that can reach the edge , and grid By comparison, we can figure out how many points can get out of the matrix . Count the total number of points , Just subtract 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;
}
边栏推荐
- 21、wpf之绑定使用小记
- HMS core audio editing service 3D audio technology helps create an immersive auditory feast
- Global Capital Market 101:国内高净值人群最好的投资标的之一BREIT
- 基于视觉的机器人抓取:从物体定位、物体姿态估计到平行抓取器抓取估计
- [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
- MySQL 复合查询
- Speech signal processing - Fundamentals (V): Fourier transform
- 对象映射 - Mapping.Mapster
- If it is not listed again, Kuangshi technology will not be able to endure
- R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_size函数配置数据点的大小的(size)度量调整的范围
猜你喜欢

A High-Precision Positioning Approach for Catenary Support Components With Multiscale Difference阅读笔记

HMS core audio editing service 3D audio technology helps create an immersive auditory feast

Boost study: boost log

MySQL 表的内连和外连

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

MySQL 内置函数
![[pattern recognition]](/img/b1/dcb444cbf40a43eeb7f7b233d7741a.png)
[pattern recognition]

Openmldb meetup No.4 meeting minutes

A quietly rising domestic software, low-key and powerful!

这些电影中的科幻构想,已经用AI实现了
随机推荐
【模式识别大作业】
Review the writing software with characteristics
光谱共焦位移传感器的原理是什么?能应用那些领域?
3D线光谱共焦传感器在半导体如何检测
ClipboardJS——开发学习总结1
1175. 质数排列
Serial communication interface 8250
Quel est le rôle du rétroéclairage LED?
How can c write an SQL parser
用宝塔建第2个网站时网站总是报错:No input file specified.
来聊聊怎么做硬件兼容性检测,快速迁移到openEuler?
The sci-fi ideas in these movies have been realized by AI
Cache avalanche and cache penetration solutions
【重温经典C语言】~c语言中%x、%c、%d、%x等等等、c语言取地址符&的作用、C语言中的 联合体
Openmldb meetup No.4 meeting minutes
OpenMLDB Meetup No.4 会议纪要
goto语句跳转未初始化变量:C2362
爱可可AI前沿推介(6.30)
这些电影中的科幻构想,已经用AI实现了
学习redis实现分布式锁—–自己的一个理解