当前位置:网站首页>The number of enclaves
The number of enclaves
2022-07-05 05:27:00 【low dog】
The number of enclaves
stay m*n The size of gird in , among 0 On behalf of the ocean ,1 Representing land , The title is to find the number of lands wrapped by the ocean , Finally, return the number of lands surrounded by the ocean .
Their thinking
First, the enclave problem is a simple matrix traversal problem , A little method is added to the basic matrix traversal , The enclave problem is solved .
First, we create a method ,DFS Method ( Depth first search method )
void DFS(int** grid,int i,int j,int n,int* m){
if(i<0||j<0||j=>m||i=>n) return;
if(grid[i][j]==0) return;
grid[i][j]=0;
DFS(grid,i,j+1,n,m);
DFS(grid,i,j-1,n,m);
DFS(grid,i+1,j,n,m);
DFS(grid,i-1,j,n,m);
}The idea of recursion is also used here, which will be discussed in Chapter i+1 That's ok , The first j+1 Check the upper, lower, left and right nodes of the elements of the column , If it is land, change it into ocean , Because in C Language cannot be used grid.length,grid[0].length. So at the end of this function, we add the length and width of the whole matrix .( If a boss finds out how to put the long broadband of the matrix into this method, he hopes to leave your footprints in the comment area , Xiaobai really needs your help )
Second, let's first make sure that nothing at the boundary of the matrix counts , Then we will first eliminate the land around the boundary , The elimination method is to call the above method to find the land near the boundary , And change them into oceans ( For convenience, we use n,m)
for(int i=0;i<n;i++){
DFS(grid,i,0,n,m);
DFS(grid,i,m-1,n,m);
}
for(int j=0;j<m;j++){
DFS(grid,0,j,n,m);
DFS(grid,n-1,j,n,m);
}Finally, we traverse the matrix directly 1 The node of , Count with one parameter .( You can check the number of islands in )
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
if(grid[i][j]==1) sum++;
}for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
if(grid[i][j]==1){
sum++;
DFS(grid,i,j,n,m); // Add this if you want to query the number of islands
}
}Add the return value at the end to complete .( Here's the complete code )
Complete code
void Dfs(int** grid,int i,int j,int n,int* m){
if(i<0||j<0||i>=n||j>=*m) return;
if(grid[i][j]==0) return;
grid[i][j]=0;
Dfs(grid,i,j+1,n,m);
Dfs(grid,i,j-1,n,m);
Dfs(grid,i+1,j,n,m);
Dfs(grid,i-1,j,n,m);
}
int numEnclaves(int** grid, int n, int* m){
int sum=0;
for(int i=0;i<n;i++){
Dfs(grid,i,0,n,m);
Dfs(grid,i,*m-1,n,m);
}
for(int j=0;j<*m;j++){
Dfs(grid,0,j,n,m);
Dfs(grid,n-1,j,n,m);
}
for(int i=0;i<=m-1;i++)
for(int j=0;j<*m;j++){
if(grid[i][j]==1){
sum++;
}
}
return sum;
}边栏推荐
- Programmers' experience of delivering takeout
- [binary search] 34 Find the first and last positions of elements in a sorted array
- 对象的序列化
- Using HashMap to realize simple cache
- Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
- SSH password free login settings and use scripts to SSH login and execute instructions
- 剑指 Offer 05. 替换空格
- Count sort
- Haut OJ 1218: maximum continuous sub segment sum
- PMP考试敏捷占比有多少?解疑
猜你喜欢

Research on the value of background repeat of background tiling

支持多模多态 GBase 8c数据库持续创新重磅升级

To the distance we have been looking for -- film review of "flying house journey"

Yolov5 ajouter un mécanisme d'attention

JVM call not used once in ten years
![[to be continued] [UE4 notes] L2 interface introduction](/img/0f/268c852b691bd7459785537f201a41.jpg)
[to be continued] [UE4 notes] L2 interface introduction

远程升级怕截胡?详解FOTA安全升级

YOLOv5-Shufflenetv2

Gbase database helps the development of digital finance in the Bay Area

On-off and on-off of quality system construction
随机推荐
质量体系建设之路的分分合合
Zheng Qing 21 ACM is fun. (3) part of the problem solution and summary
Add level control and logger level control of Solon logging plug-in
Haut OJ 1352: string of choice
SDEI初探-透过事务看本质
[转]MySQL操作实战(三):表联结
Solon Logging 插件的添加器级别控制和日志器的级别控制
2022上半年全国教师资格证下
GBase数据库助力湾区数字金融发展
Heap sort summary
National teacher qualification examination in the first half of 2022
Yolov5 adds attention mechanism
Insert sort
Haut OJ 1241: League activities of class XXX
记录QT内存泄漏的一种问题和解决方案
YOLOv5添加注意力機制
Service fusing hystrix
PMP考试敏捷占比有多少?解疑
object serialization
A new micro ORM open source framework