Maximum area DFS of front end algorithm Island (depth first search)

Time：2020-11-15 Given a non empty two-dimensional array grid containing some 0 and 1.

An island is a combination of adjacent ones (representing land). The “adjacency” here requires that two ones must be adjacent horizontally or vertically. You can assume that all four edges of the grid are surrounded by zeros (representing water).

Find the largest island area in a given two-dimensional array. (if there are no islands, the return area is 0. )

Example 1:

[[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]]

For the given matrix above, 6 should be returned. Note that the answer should not be 11, because islands can only contain 1 in four directions, horizontal or vertical.

Example 2:

[[0,0,0,0,0,0,0,0]]

For the given matrix above, return 0.

Note: the length and width of a given matrix grid is no more than 50.

analysis:

We want to know the area of each connected shape in the grid, and then take the maximum value.

If we explore each land connected with it (and the land connected to it) in four directions on a land, the total number of land explored will be the area of the connected shape.

To ensure that each land is not visited more than once, we set the value of a piece of land to 0 each time we pass it. So we don’t visit the same land many times.

The maximum area of each island is returnedmaxArea
2. Search function recursive implementation of DFS function
3. Judge the boundary. If it is not within the boundary, return 0; otherwise, it is 1. Recursively calculate whether the upper, lower, left and right is 1, and area calculates the island area;
4. After judging each position, set it to 0 (gridi = 0)

Implementation solution:

var maxAreaOfIsland = function (grid) {
Var maxarea = 0; // record holder
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid.length; j++) {
if (grid[i][j] === 1) {
maxArea =  Math.max (maxarea, DFS (grid, I, J)); // update records
}
}
}
function dfs(grid, i, j) {
if (i < 0 || i >= grid.length || j < 0 || j >= grid.length || grid[i][j] === 0) {
Return 0; // recursive exit boundary
}
Grid [i] [J] = 0; // avoid double calculation, sink Island strategy
var area = 1;
Area + = DFS (grid, I + 1, J); // 1
Area + = DFS (grid, I - 1, J); // 1
Area + = DFS (grid, I, j + 1); // 1 on the left
Area + = DFS (grid, I, J - 1); // 1 on the right
return area
}
Return maxarea // returns the maximum area
};

A brief introduction to file import in HTML5

Template, shadow Dom and custom elements make it easier for you to create UI components than before. However, resources such as HTML, CSS and JavaScript still need to be loaded one by one, which is very inefficient. Removing duplicate dependencies is not easy. For example, now loading the jQuery UI or bootstrap requires adding separate […]