当前位置:网站首页>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;
}
边栏推荐
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram and use scale_ x_ The log10 function configures the value range of the X axis to be logarithmic coordinates
- Re understand oauth2.0 protocol for joint login
- R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)
- 基于视觉的机器人抓取:从物体定位、物体姿态估计到平行抓取器抓取估计
- led背光板的作用是什麼呢?
- R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用labs函数为动画图添加动态时间标题(抽取frame_time信息)
- If it is not listed again, Kuangshi technology will not be able to endure
- Database cascading operation
- A High-Precision Positioning Approach for Catenary Support Components With Multiscale Difference阅读笔记
- 695.最大岛屿面积
猜你喜欢
wallys/3×3 MIMO 802.11ac Mini PCIe Wi-Fi Module, QCA9880, 2,4GHz / 5GHzDesigned for Enterprise
led背光板的作用是什么呢?
限时预约|6 月 Apache Pulsar 中文开发者与用户组会议
Object mapping - mapping Mapster
“\“id\“ contains an invalid value“
Let's talk about how to do hardware compatibility testing and quickly migrate to openeuler?
The first batch in China! Alibaba cloud native data Lake products have passed the evaluation and certification of the ICT Institute
wallys/IPQ8074a/2x(4×4 or 8×8) 11AX MU-MIMO DUAL CONCURRENT EMBEDDEDBOARD
Yolov5 export the pit encountered by onnx
It is said that with this, the boss opened the test overnight
随机推荐
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
Redis - ziplist compressed list
【云原生 | Kubernetes篇】深入了解Deployment(八)
Shutter 007 input field from zero
R语言ggplot2可视化:使用ggplot2可视化散点图、aes函数中的colour参数指定不同分组的数据点使用不同的颜色显示
A High-Precision Positioning Approach for Catenary Support Components With Multiscale Difference阅读笔记
wallys/IPQ8074a/2x(4×4 or 8×8) 11AX MU-MIMO DUAL CONCURRENT EMBEDDEDBOARD
MySQL 表的内连和外连
用于生成学习任务的量子神经网络2022最新综述
What is the function of LED backlight?
缓存雪崩和缓存穿透解决方案
ModelAtrs声音检测,基于声学特征的异响检测
Multiparty cardinality testing for threshold private set-2021: Interpretation
R language ggplot2 visualization: use ggplot2 visualization scatter diagram and the color parameter in AES function to specify that data points in different groups are displayed in different colors
21、wpf之绑定使用小记
Parallel interface 8255A
STM32F407ZGT6使用SDIO方式驱动SD卡
深入解析 Apache BookKeeper 系列:第四篇—背压
Flutter 从零开始 005 图片及Icon
「运维有小邓」用户个人资料管理