当前位置:网站首页>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;
}
边栏推荐
- Typescript readonlyarray (read only array type) details
- A Generic Deep-Learning-Based Approach for Automated Surface Inspection-論文閱讀筆記
- Learn how to implement distributed locks in redis - my own understanding
- 使用深度学习进行生物网络分析
- [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
- HMS core audio editing service 3D audio technology helps create an immersive auditory feast
- 1254. 统计封闭岛屿的数目
- Installing onnx is very slow. Use Tsinghua image
- TypeScript ReadonlyArray(只读数组类型) 详细介绍
- Automatic database growth
猜你喜欢
随机推荐
Object mapping - mapping Mapster
如何使用插件化机制优雅的封装你的请求hook
Let's talk about how to do hardware compatibility testing and quickly migrate to openeuler?
R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_x_log10函数配置X轴的数值范围为对数坐标
Learn how to implement distributed locks in redis - my own understanding
NoSQL——Redis的配置与优化
R language ggplot2 visualization: use ggplot2 visualization scatter diagram and the size parameter in AES function to specify the size of data points (point size)
Typescript readonlyarray (read only array type) details
"War" caused by a bottle of water
Constructor, class member, destructor call order
OpenMLDB Meetup No.4 会议纪要
The first batch in China! Alibaba cloud native data Lake products have passed the evaluation and certification of the ICT Institute
Lucene full text search toolkit learning notes summary
The sci-fi ideas in these movies have been realized by AI
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
限时预约|6 月 Apache Pulsar 中文开发者与用户组会议
3D线光谱共焦传感器在半导体如何检测
STM32F407ZGT6使用SDIO方式驱动SD卡
DMA controller 8237a
一个悄然崛起的国产软件,低调又强大!









