当前位置:网站首页>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;
}
边栏推荐
- Yolov5 export the pit encountered by onnx
- R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_color_viridis_d函数指定数据点的配色方案
- A Generic Deep-Learning-Based Approach for Automated Surface Inspection-論文閱讀筆記
- 【云原生 | Kubernetes篇】深入了解Deployment(八)
- Boost study: boost log
- The first batch in China! Alibaba cloud native data Lake products have passed the evaluation and certification of the ICT Institute
- 对象映射 - Mapping.Mapster
- “\“id\“ contains an invalid value“
- 一个悄然崛起的国产软件,低调又强大!
- time 函数和 clock_gettime()函数的区别
猜你喜欢
随机推荐
Serial communication interface 8250
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)
Limited time appointment | Apache pulsar Chinese developer and user group meeting in June
The sci-fi ideas in these movies have been realized by AI
wallys/IPQ8074a/2x(4 × 4 or 8 × 8) 11AX MU-MIMO DUAL CONCURRENT EMBEDDEDBOARD
The first batch in China! Alibaba cloud native data Lake products have passed the evaluation and certification of the ICT Institute
Evaluation of IP location query interface Ⅲ
构造函数、类成员、析构函数调用顺序
Flutter 从零开始 006 单选开关和复选框
Go 语言入门很简单:Go 处理 XML 文件
A High-Precision Positioning Approach for Catenary Support Components With Multiscale Difference阅读笔记
R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram, and_ Set show in the point parameter_ The legend parameter is false, and the legend information is not displayed
如何使用插件化机制优雅的封装你的请求hook
Flutter 从零开始 005 图片及Icon
Speech recognition - Fundamentals (I): introduction [speech to text]
1175. prime permutation
There are so many kinds of coupons. First distinguish them clearly and then collect the wool!
695.最大岛屿面积
Webview,ScrollView滑动冲突咋整
Shutter 007 input field from zero







