[C language brush LeetCode] 1034. Boundary coloring (M)

[

Give you an integer matrix grid of size m x n, representing a grid. I'll give you three more integers   row, col, and color. Each value in the grid represents the color of the grid block at that location.

Two grid blocks belong to the same connected component, and all the following conditions shall be met:

The two grid blocks are the same color
Adjacent in any direction of up, down, left and right
The boundary of the connected component refers to all grid blocks in the connected component that meet one of the following conditions:

It is adjacent to grid blocks that do not belong to the same connected component in the up, down, left and right directions
On the boundary of the grid (first row / column or last row / column)
Please use the specified color   Color contains grid blocks for all   The boundary of the connected component of grid[row][col] is colored and the final mesh is returned   grid .

Example 1:

Input: grid = [[1,1],[1,2]], row = 0, col = 0, color = 3
Output: [[3,3], [3,2]]
Example 2:

Input: grid = [[1,2,2],[2,3,2]], row = 0, col = 1, color = 3
Output: [[1,3,3], [2,3,3]]
Example 3:

Input: grid = [[1,1,1],[1,1,1],[1,1,1]], row = 1, col = 1, color = 2
Output: [[2,2,2], [2,1,2], [2,2,2]]
 

Tips:

m == grid.length
n == grid[i].length
1 <= m, n <= 50
1 <= grid[i][j], color <= 1000
0 <= row < m
0 <= col < n

Source: LeetCode
Link: https://leetcode-cn.com/problems/coloring-a-border
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

]

Matrix problem, think of DFS and direction array. But this question is a little special, that is, coloring the boundary, so you can't use the direction array, because you need the return value of DFS to judge whether the current point is a boundary.

The key point of this problem is to judge the boundary.

In addition, it is too troublesome to apply for a two-dimensional array of tags. Use global variables directly. Remember that all global variables must be initialized at the function entry, otherwise the following use cases will be useless.

In addition, in C language, if the row and column return value does not change, you can directly use the passed in value:

This question:

*returnColumnSizes = gridColSize;

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int m;
int n;
int oricolor;

int flag[51][51];

bool Dfs(int** grid, int row, int col, int color) {
    
    if ((row >= m) || (col >= n) || (row < 0) || (col < 0)) {
        return false; // The last element reaches the edge of the matrix, which is the boundary
    }
    
    if (flag[row][col] == 1) {
        return true; // Already visited, not boundary
    }
    
    if (grid[row][col] != oricolor) {
        return false; // The current element is not the same as the original color, that is, the previous element is a boundary
    }
    flag[row][col] = 1;

    int a = Dfs(grid, row + 1, col, color);
    int b = Dfs(grid, row - 1, col, color);
    int c = Dfs(grid, row, col + 1, color);
    int d = Dfs(grid, row, col - 1, color);
    
    if ((a & b & c & d) == false) { //One of the four positions is out of bounds, that is, reaching the boundary position
        grid[row][col] = color;
    }
    
    return true;
}

int** colorBorder(int** grid, int gridSize, int* gridColSize, int row, int col, int color, int* returnSize, int** returnColumnSizes){
    int i;
    
    m = gridSize;
    n = *gridColSize;
    oricolor = grid[row][col];
    memset(flag, 0, sizeof(flag));
    
    Dfs(grid, row, col, color);
    
    *returnSize = m;
    *returnColumnSizes = gridColSize;
    
    return grid;
}

Keywords: C Algorithm leetcode

Added by l053r on Wed, 08 Dec 2021 20:02:36 +0200