LeetCode 1034. Border coloring (BFS/DFS)

1. Title

Given a two-dimensional integer grid, each value in the grid represents the color of the grid block at that position.

Only when two grid blocks have the same color and are adjacent in any of the four directions, they belong to the same connected component.

The boundary of the connected component refers to all the squares in the connected component adjacent to the squares not in the component (in four directions), or all the squares on the boundary of the grid (the first row/column or the last row/column) square.

Give the grid block located at (r0, c0) and the color color, use the specified color color to color the boundary of the connected components of the given grid block , and return the final grid grid.

示例 1:
输入:grid = [[1,1],[1,2]], 
r0 = 0, c0 = 0, color = 3
输出:[[3, 3], [3, 2]]

示例 2:
输入:grid = [[1,2,2],[2,3,2]], 
r0 = 0, c0 = 1, color = 3
输出:[[1, 3, 3], [2, 3, 3]]

示例 3:
输入:grid = [[1,1,1],[1,1,1],[1,1,1]], 
r0 = 1, c0 = 1, color = 2
输出:[[2, 2, 2], [2, 1, 2], [2, 2, 2]]
 
提示:
1 <= grid.length <= 50
1 <= grid[0].length <= 50
1 <= grid[i][j] <= 1000
0 <= r0 < grid.length
0 <= c0 < grid[0].length
1 <= color <= 1000

Source: LeetCode Link: https://leetcode-cn.com/problems/coloring-a-border The
copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

2. Problem solving

  • Simple BFS/DFS is fine
  • Judgment condition: if the surrounding points are out of bounds, or the colors of surrounding points are different, the current point is marked as the boundary

2.1 BFS

class Solution {
    
    
public:
    vector<vector<int>> colorBorder(vector<vector<int>>& grid, int r0, int c0, int color) {
    
    
    	int m = grid.size(), n = grid[0].size(), i, j, k, x, y;
    	vector<vector<int>> dir = {
    
    {
    
    1,0},{
    
    0,1},{
    
    0,-1},{
    
    -1,0}};
    	int origin = grid[r0][c0];
    	vector<vector<bool>> vis(m, vector<bool>(n, false));
    	queue<pair<int,int>> q;
    	q.push(make_pair(r0, c0));
    	vis[r0][c0] = true;
    	while(!q.empty())
    	{
    
    
    		i = q.front().first;
    		j = q.front().second;
            q.pop();
    		for(k = 0; k < 4; ++k)
    		{
    
    
    			x = i + dir[k][0];
    			y = j + dir[k][1];
    			if(x >=0 && x<m && y>=0 && y<n)//在界内
    			{
    
    
                    if(vis[x][y]) continue;//访问过了,下一个
    				if(grid[x][y] != origin)//没有访问,颜色不同
    					grid[i][j] = color;// i, j 旁边的 x, y跟它不一样,边界
    				else//没有访问,颜色一样,正常入队
    				{
    
    
                        q.push({
    
    x,y});
                        vis[x][y] = true;
                    }
    			}
    			else//出界了
    				grid[i][j] = color;//i,j 是边界
    		}
    	}
    	return grid;
    }
};

40 ms 13 MB

2.2 DFS

class Solution {
    
    
    vector<vector<int>> dir = {
    
    {
    
    1,0},{
    
    0,1},{
    
    0,-1},{
    
    -1,0}};
    int m, n, origin, col;
public:
    vector<vector<int>> colorBorder(vector<vector<int>>& grid, int r0, int c0, int color) {
    
    
        m = grid.size(), n = grid[0].size();
        origin = grid[r0][c0], col = color;
        vector<vector<bool>> vis(m, vector<bool>(n, false));
        vis[r0][c0] = true;
        dfs(grid,r0,c0,vis);
        return grid;
    }
    void dfs(vector<vector<int>>& grid, int i, int j, vector<vector<bool>>& vis)
    {
    
    
        int x, y, k;
        for(k = 0; k < 4; ++k)
        {
    
    
            x = i + dir[k][0];
            y = j + dir[k][1];
            if(x >=0 && x<m && y>=0 && y<n)//在界内
            {
    
    
                if(vis[x][y]) continue;//访问过了,下一个
                if(grid[x][y] != origin)//没有访问,颜色不同
                    grid[i][j] = col;// i, j 旁边的 x, y跟它不一样,边界
                else//没有访问,颜色一样
                {
    
    
                    vis[x][y] = true;
                    dfs(grid, x, y, vis);
                }
            }
            else//出界了
                grid[i][j] = col;//i,j 是边界
        }
    }
};

36 ms 12.9 MB


My CSDN blog address https://michael.blog.csdn.net/

Long press or scan the QR code to follow my official account (Michael Amin), come on together, learn and make progress together!
Michael Amin

Guess you like

Origin blog.csdn.net/qq_21201267/article/details/108521257