当前位置:网站首页>[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;
}
};
边栏推荐
- Count sort
- Embedded database development programming (V) -- DQL
- win10虚拟机集群优化方案
- Common database statements in unity
- MD5 bypass
- Redis has four methods for checking big keys, which are necessary for optimization
- Panel panel of UI
- [转]MySQL操作实战(一):关键字 & 函数
- 3dsmax scanning function point connection drawing connection line
- 2021-10-29
猜你喜欢
Unity3d learning notes
Embedded database development programming (V) -- DQL
2022/7/2 question summary
Establish cloth effect in 10 seconds
3dsmax snaps to frozen objects
The present is a gift from heaven -- a film review of the journey of the soul
LeetCode之單詞搜索(回溯法求解)
[轉]: OSGI規範 深入淺出
Do a small pressure test with JMeter tool
Ue4/ue5 illusory engine, material part (III), material optimization at different distances
随机推荐
Solon Logging 插件的添加器级别控制和日志器的级别控制
用 Jmeter 工具做个小型压力测试
PMP考生,请查收7月PMP考试注意事项
Count sort
A three-dimensional button
Ue4/ue5 illusory engine, material part (III), material optimization at different distances
Unity synergy
Out and ref functions of unity
3dsmax common commands
Cocos2dx Lua registers the touch event and detects whether the click coordinates are within the specified area
GameObject class and transform class of unity
Unity and database
FVP和Juno平台的Memory Layout介绍
2022年上半年国家教师资格证考试
Kali 2018 full image download
stm32Cubemx(8):RTC和RTC唤醒中断
When will Wei Lai, who has been watched by public opinion, start to "build high-rise buildings" again?
Unity get component
Leetcode word search (backtracking method)
Learning notes of "hands on learning in depth"