[detailed solution of algorithm problem] DFS solution force buckle the maximum area of 695 Islands

subject

Give you a binary matrix grid of size m x n.

An island is a combination of some adjacent 1s (representing land). The "adjacent" here requires that two 1s must be adjacent in four horizontal or vertical directions. You can assume that the four edges of the grid are surrounded by 0 (representing water).

The area of an island is the number of cells on the island with a value of 1.

Calculate and return the largest island area in the grid. If there are no islands, 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
 Explanation: the answer should not be 11, because the island can only contain 1 in the horizontal or vertical directions.

Example 2:

Input: grid = [[0,0,0,0,0,0,0,0]]
Output: 0

Tips

 1. m == grid.length
 2. n == grid[i].length
 3. 1 <= m, n <= 50
 4. grid[i][j] Is 0 or 1

thinking

Firstly, this is a binary matrix grid with the size of m x n, which shows that there are only two values of 01 in the matrix. 1 represents land and 0 represents water. This problem is to solve the maximum number of connected 1, and the connection direction also gives four directions, horizontal or vertical. This problem is the same as yesterday's problem Perimeter of the island Similarly, we also need to traverse the matrix once. If the traversed point is 1 and has not been accessed, we enter dfs, and then access the points in the upper, lower, left and right directions of the current point after accessing the current point. Recursive search and judgment. Next, we enter the code.

code

The first is the traversal of the matrix in the main function

class Solution {
	int m, n;
	int maxAreaOfIsland(vector<vector<int>>& grid) {
			m = int(grid.size());
	        n = int(grid[0].size());
	        for(int i = 0; i < m; i++) {
	            for(int j = 0; j < n; j++) {
	                dfs(grid, i, j);
	            }
	        }
	}
}

Then we need to record the land size of each dfs search and calculate a larger value with the maximum size of the current record (the current size is recorded with res, and the initial value is - 1 to minimize it). Because the number of records needs to appear in the main function and dfs function, it is defined in the class like m and n

class Solution {
	int m, n;
	int count = 0;
	int maxAreaOfIsland(vector<vector<int>>& grid) {
			m = int(grid.size());
	        n = int(grid[0].size());
	        for(int i = 0; i < m; i++) {
	            for(int j = 0; j < n; j++) {
	                count = 0;
	                dfs(grid, i, j);
	                res = max(res, count);
	            }
	        }
	}
}

Then, the dfs function marks each visited point after entering the dfs. The exit condition is still that the coordinates are out of bounds or the current point value is 0 or 2 (representing water area or visited)

void dfs(vector<vector<int>>& grid, int x, int y) {
        if(x < 0 || x >= m || y < 0 || y >= n || grid[x][y] != 1 || grid[x][y] == 2) {
            return;
        }
        grid[x][y] = 2;
        dfs(grid, x + 1, y);
        dfs(grid, x - 1, y);
        dfs(grid, x, y + 1);
        dfs(grid, x, y - 1);
}

It should also be noted that after accessing and marking the current point, we need to add the count value + 1 to represent the current land area value + 1. The following is the complete code
Full code:

class Solution {
public:
    int m, n;
    int count = 0;
    void dfs(vector<vector<int>>& grid, int x, int y) {
        if(x < 0 || x >= m || y < 0 || y >= n || grid[x][y] != 1 || grid[x][y] == 2) {
            return;
        }
        grid[x][y] = 2;
        ++count;
        dfs(grid, x + 1, y);
        dfs(grid, x - 1, y);
        dfs(grid, x, y + 1);
        dfs(grid, x, y - 1);
    }
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int res = -1;
        m = int(grid.size());
        n = int(grid[0].size());
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                count = 0;
                dfs(grid, i, j);
                res = max(res, count);
            }
        }
        return res;
    }
};

Tags: Algorithm leetcode dfs

Posted on Sun, 31 Oct 2021 19:31:05 -0400 by barrylee