当前位置:网站首页>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;
}边栏推荐
- Es module and commonjs learning notes
- 动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
- Bucket sort
- Shell Sort
- [binary search] 69 Square root of X
- JVM call not used once in ten years
- Haut OJ 2021 freshmen week II reflection summary
- [turn]: OSGi specification in simple terms
- Embedded database development programming (VI) -- C API
- Haut OJ 1357: lunch question (I) -- high precision multiplication
猜你喜欢

Heap sort summary

lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
![[转]: OSGI规范 深入浅出](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[转]: OSGI规范 深入浅出
![[轉]: OSGI規範 深入淺出](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[轉]: OSGI規範 深入淺出

Corridor and bridge distribution (csp-s-2021-t1) popular problem solution

On-off and on-off of quality system construction
![[turn to] MySQL operation practice (I): Keywords & functions](/img/b1/8b843014f365b786e310718f669043.png)
[turn to] MySQL operation practice (I): Keywords & functions

National teacher qualification examination in the first half of 2022

Web APIs DOM node

Reader writer model
随机推荐
[轉]: OSGI規範 深入淺出
Optimization scheme of win10 virtual machine cluster
Haut OJ 1243: simple mathematical problems
服务熔断 Hystrix
How can the Solon framework easily obtain the response time of each request?
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
Solon 框架如何方便获取每个请求的响应时间?
The next key of win generates the timestamp file of the current day
用STM32点个灯
kubeadm系列-00-overview
TF-A中的工具介绍
Gbase database helps the development of digital finance in the Bay Area
Applet Live + e - commerce, si vous voulez être un nouveau e - commerce de détail, utilisez - le!
2022年上半年国家教师资格证考试
Improvement of pointnet++
[allocation problem] 135 Distribute candy
剑指 Offer 53 - II. 0~n-1中缺失的数字
Magnifying glass effect
Embedded database development programming (VI) -- C API
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail