当前位置:网站首页>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;
}
边栏推荐
- nvm安装node后,在使用npm指令时候显示不是内部或外部指令
- Pointdistiller: structured knowledge distillation for efficient and compact 3D detection
- STM32F407ZGT6使用SDIO方式驱动SD卡
- Alibaba cloud database represented by polardb ranks first in the world
- QT embeds the sub QT program window into the current program
- MySQL 表的内连和外连
- 重新理解oauth2.0协议进行联合登录
- Lucene full text search toolkit learning notes summary
- 对象映射 - Mapping.Mapster
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram, and_ Set the alpha parameter in the point parameter to specify the transparency level of data points (points transparent
猜你喜欢

c# 怎样能写个sql的解析器

wallys/3 × 3 MIMO 802.11ac Mini PCIe Wi-Fi Module, QCA9880, 2,4GHz / 5GHzDesigned for Enterprise

使用深度学习进行生物网络分析

科普达人丨漫画图解什么是eRDMA?

Boost study: boost log

led背光板的作用是什么呢?

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

The sci-fi ideas in these movies have been realized by AI

CVPR 2022 | greatly reduce the manual annotation required for zero sample learning. Mapu and Beiyou proposed category semantic embedding rich in visual information

The operation and maintenance security gateway (Fortress machine) of Qiming star group once again won the first place!
随机推荐
R语言ggplot2可视化分面图(facet):gganimate包基于transition_time函数创建动态散点图动画(gif)、使用labs函数为动画图添加动态时间标题
MySQL 内置函数
STM32F407ZGT6使用SDIO方式驱动SD卡
Using cookie technology to realize historical browsing records and control the number of displays
He was the first hero of Shanghai's two major industries, but died silently in regret
Redis - SDS simple dynamic string
R语言ggplot2可视化:使用ggplot2可视化散点图、aes函数中的colour参数指定不同分组的数据点使用不同的颜色显示
Shell first command result is transferred to the second command delete
Multiparty cardinality testing for threshold private set-2021: Interpretation
Summer vacation study record
VScode选中多个单词
Yolov5 export the pit encountered by onnx
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
nvm安装node后,在使用npm指令时候显示不是内部或外部指令
AUTOCAD——LEN命令
Is the golden cycle of domestic databases coming?
AutoCAD - len command
缓存雪崩和缓存穿透解决方案
Limited time appointment | Apache pulsar Chinese developer and user group meeting in June
OpenMLDB Meetup No.4 会议纪要