当前位置:网站首页>[depth first search] 695 Maximum area of the island
[depth first search] 695 Maximum area of the island
2022-07-05 05:16:00 【lee2813】
One 、 subject
Give you a size of m x n The binary matrix of grid .
Islands It's made up of some neighboring 1 ( Representing land ) A combination of components , there 「 adjacent 」 Ask for two 1 Must be in In four horizontal or vertical directions adjacent . You can assume grid The four edges of are all 0( Representative water ) Surrounded by a .
The area of the island is the value of 1 Number of cells .
Calculate and return grid The largest island area in . If there were no Islands , Then the return area is 0 .
Example 1:
Input :grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]
Output :6
explain : The answer should not be 11 , Because the island can only contain horizontal or vertical 1 .
Example 2:
Input :grid = [[0,0,0,0,0,0,0,0]]
Output :0
Two 、 Answer key
This question is a typical search question , An island here is also equivalent to a ring , So we use depth first search .
The implementation of depth first search generally requires a main function and an auxiliary function , The main function is used to traverse all positions , Judge and serve as the starting point of the search ; Auxiliary functions are used for a depth first search . among , Depth first search can be realized by recursion or stack , Recursive implementation is relatively easy , It can be used to brush questions , There are many projects in the stack , It can prevent the recursive stack from being full .
in addition , Once recursion is used, we should consider the boundary conditions and what operations should be carried out under the boundary conditions . There are two ways to deal with recursive boundaries :
- Judge the boundary first , Re recursion
- Recursion first , Judge the boundary again
These two methods correspond to two ways of writing auxiliary functions , as follows .
Be careful : When using recursive operation, the searched position here is to prevent it from being searched again , The corresponding location should be marked as visited .
3、 ... and 、 Code
- Judge the boundary first , Re recursion
class Solution {
public:
vector<int> direction{
-1,0,1,0,-1};
int maxAreaOfIsland(vector<vector<int>>& grid) {
if(grid.empty()||grid[0].empty()) return 0;
int res = 0;
for(int i = 0;i<grid.size();i++){
for(int j = 0;j<grid[0].size();j++){
if(grid[i][j]== 1){
res = max(res,dfs(grid,i,j));
}
}
}
return res;
}
int dfs(vector<vector<int>>& grid,int i,int j){
if(grid[i][j]==0) return 0;
grid[i][j] = 0;
int r,c,count = 1;
for(int k=0;k<4;k++){
r = i + direction[k];
c = j+direction[k+1];
if(r < grid.size() && c < grid[0].size() && r >=0 && c>=0 && grid[r][c]!=0){
count += dfs(grid,r,c);
}
}
return count;
}
};
- Recursion first , Judge the boundary again
class Solution {
public:
vector<int> direction{
-1,0,1,0,-1};
int maxAreaOfIsland(vector<vector<int>>& grid) {
if(grid.empty()||grid[0].empty()) return 0;
int res = 0;
for(int i = 0;i<grid.size();i++){
for(int j = 0;j<grid[0].size();j++){
res = max(res,dfs(grid,i,j));
}
}
return res;
}
int dfs(vector<vector<int>>& grid,int i,int j){
if(i < grid.size() && j < grid[0].size() && i >=0 && j>=0 && grid[i][j]!=0){
grid[i][j] = 0;
return 1+dfs(grid,i-1,j)+dfs(grid,i,j-1)+dfs(grid,i+1,j)+dfs(grid,i,j+1);
}
else return 0;
}
};
边栏推荐
- GBase数据库助力湾区数字金融发展
- How to choose a panoramic camera that suits you?
- C4D simple cloth (version above R21)
- 远程升级怕截胡?详解FOTA安全升级
- C iterator
- Solon Logging 插件的添加器级别控制和日志器的级别控制
- Use of snippets in vscode (code template)
- [turn]: OSGi specification in simple terms
- Solon Auth 认证框架使用演示(更简单的认证框架)
- Embedded database development programming (VI) -- C API
猜你喜欢

Chinese notes of unit particle system particle effect

JVM call not used once in ten years

TF-A中的工具介绍

嵌入式数据库开发编程(五)——DQL
![[轉]: OSGI規範 深入淺出](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[轉]: OSGI規範 深入淺出

嵌入式数据库开发编程(零)

Collapse of adjacent vertical outer margins

Research on the value of background repeat of background tiling

小程序直播+電商,想做新零售電商就用它吧!

Embedded database development programming (VI) -- C API
随机推荐
Unity intelligent NPC production -- pre judgment walking (method 1)
Embedded database development programming (V) -- DQL
cocos_ Lua listview loads too much data
Time format conversion
远程升级怕截胡?详解FOTA安全升级
[转]:Apache Felix Framework配置属性
Research on the value of background repeat of background tiling
The present is a gift from heaven -- a film review of the journey of the soul
Unity connects to the database
Lua GBK and UTF8 turn to each other
Dotween usage records ----- appendinterval, appendcallback
【论文笔记】Multi-Goal Reinforcement Learning: Challenging Robotics Environments and Request for Research
PMP考生,请查收7月PMP考试注意事项
3dsmax snaps to frozen objects
Use of snippets in vscode (code template)
Sqlserver stored procedures pass array parameters
PR first time
Panel panel of UI
Common database statements in unity
cocos2dx_ Lua card flip