当前位置:网站首页>[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;
}
};
边栏推荐
- The next key of win generates the timestamp file of the current day
- 2021-10-29
- Optimization scheme of win10 virtual machine cluster
- Sixth note
- Solon 框架如何方便获取每个请求的响应时间?
- The difference between heap and stack
- [turn to] MySQL operation practice (I): Keywords & functions
- Redis has four methods for checking big keys, which are necessary for optimization
- Django reports an error when connecting to the database. What is the reason
- Page countdown
猜你喜欢
Collapse of adjacent vertical outer margins
Download and use of font icons
《动手学深度学习》学习笔记
LeetCode之單詞搜索(回溯法求解)
Optimization scheme of win10 virtual machine cluster
3dsmax snaps to frozen objects
Simple modal box
Research on the value of background repeat of background tiling
Shell Sort
Research on the value of background repeat of background tiling
随机推荐
Three dimensional dice realize 3D cool rotation effect (with complete source code) (with animation code)
[turn]: Apache Felix framework configuration properties
Lua determines whether the current time is the time of the day
小程序直播+电商,想做新零售电商就用它吧!
[allocation problem] 455 Distribute cookies
使用命令符关闭笔记本自带键盘命令
2022 / 7 / 1 Résumé de l'étude
Es module and commonjs learning notes
Unity enables mobile phone vibration
Development error notes
Insert sort
C iterator
A complete attack chain
C # perspective following
Kali 2018 full image download
PMP考生,请查收7月PMP考试注意事项
To the distance we have been looking for -- film review of "flying house journey"
Lua GBK and UTF8 turn to each other
[leetcode] integer inversion [7]
Panel panel of UI