当前位置:网站首页>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;
}
边栏推荐
- Global Capital Market 101:国内高净值人群最好的投资标的之一BREIT
- 21、wpf之绑定使用小记
- Set up your own website (13)
- 来聊聊怎么做硬件兼容性检测,快速迁移到openEuler?
- 学习redis实现分布式锁—–自己的一个理解
- 这些电影中的科幻构想,已经用AI实现了
- Object mapping - mapping Mapster
- Stm32f407zgt6 uses SDIO mode to drive SD card
- 重新理解oauth2.0协议进行联合登录
- R语言ggplot2可视化:使用ggplot2可视化散点图、aes函数中的colour参数指定不同分组的数据点使用不同的颜色显示
猜你喜欢

深入解析 Apache BookKeeper 系列:第四篇—背压

Who still remembers "classmate Zhang"?

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

Our company has used this set of general solutions for 7 years, and has opened up dozens of systems, a stable batch!

“\“id\“ contains an invalid value“

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

21、wpf之绑定使用小记

Boost study: boost log

MySQL 表的内连和外连

【云原生 | Kubernetes篇】深入了解Deployment(八)
随机推荐
A quietly rising domestic software, low-key and powerful!
数据库 级联操作
Redis - SDS simple dynamic string
HMS Core音频编辑服务3D音频技术,助力打造沉浸式听觉盛宴
Database transactions
使用深度学习进行生物网络分析
盘点那些具有特色的写作软件
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
论文解读(AGC)《Attributed Graph Clustering via Adaptive Graph Convolution》
"War" caused by a bottle of water
[cf] 803 div2 A. XOR Mixup
R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_size函数配置数据点的大小的(size)度量调整的范围
60 divine vs Code plug-ins!!
Redis - ziplist compressed list
自定义一个注解来获取数据库的链接
[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
线下门店为什么要做新零售?
又被 Kotlin 语法糖坑惨的一天
Analysis of KOA - onion model
Global Capital Market 101:国内高净值人群最好的投资标的之一BREIT