0广度遍历优先/深度遍历优先中等 LeetCode1034. 边界着色

分析

bfs解法

广度遍历优先
需要定义一个相同大小的二维数组,标记一个节点有没有被遍历过,同时作为最后结果返回。
队列存储未被遍历的连通量结点,通过队列来向外扩展遍历连通量。
定义一个二维数组方便遍历一个结点的四周,也就是结点的上下左右。
从一个结点入手,把这个节点加入队列,弹出结点,遍历他周围的四个结点,若满足联通量,cnt加一,并加入队列。不停弹出结点,知道队列为空。
一个结点的四周遍历结束,若cnt不为4,则该点变为color;等于4,则该点保持原值不变。

class Solution {
    
    
    public int[][] colorBorder(int[][] grid, int row, int col, int color) {
    
    
        int[][] dir = new int[][]{
    
    {
    
    1,0},{
    
    -1,0},{
    
    0,1},{
    
    0,-1}};
        int m = grid.length, n = grid[0].length;
        int[][] ans = new int[m][n];
        Deque<int[]> deque = new ArrayDeque<>();
        deque.addLast(new int[]{
    
    row,col});
        while(!deque.isEmpty()){
    
    
            int[] tmp = deque.pollFirst();
            int cnt = 0;
            for(int[] a : dir){
    
    
                int x = tmp[0] + a[0];
                int y = tmp[1] + a[1];
                if(x < 0 || x >= m || y < 0 || y >= n){
    
    
                    continue;
                }
                if(grid[tmp[0]][tmp[1]] != grid[x][y] ){
    
    
                    continue;
                }
                cnt++;
                if(ans[x][y] == 0){
    
    
                    deque.offerLast(new int[]{
    
    x,y});
                }
            }
            if(cnt != 4){
    
    
                ans[tmp[0]][tmp[1]] = color;
            }else{
    
    
                ans[tmp[0]][tmp[1]] = grid[tmp[0]][tmp[1]];
            }
        }
        for(int i = 0; i < m; i++){
    
    
            for(int j = 0; j < n; j++){
    
    
                if(ans[i][j] == 0){
    
    
                    ans[i][j] = grid[i][j];
                }
            }
        }
        return ans;
    }
}

dfs解法

深度优先遍历
需要定义一个相同大小的二维数组,标记一个节点有没有被遍历过,同时作为最后结果返回。
定义一个二维数组方便遍历一个结点的四周,也就是结点的上下左右。
通过递归一直向下遍历,直到满足结束递归条件。通过递归向外拓展。
从一个结点出发,判断他的四周节点是否满足连通,若满足,递归这个结点(递归前要判断是不是已经遍历过了)。
当该节点的四周节点都遍历了一遍,判断若cnt,若不为4,则该点变为color;等于4,则该点保持原值不变。

class Solution {
    
    
        int[][] dir = new int[][]{
    
    {
    
    1,0},{
    
    -1,0},{
    
    0,1},{
    
    0,-1}};
    int[][] ans;
    public int[][] colorBorder(int[][] grid, int row, int col, int color) {
    
    
        int m = grid.length, n = grid[0].length;
        ans = new int[m][n];
        dfs(grid,row,col,color);
        for(int i = 0; i < m; i++){
    
    
            for(int j = 0; j < n; j++){
    
    
                if(ans[i][j] == 0){
    
    
                    ans[i][j] = grid[i][j];
                }
            }
        }
        return ans;
    }

    public void dfs(int[][] grid,int x, int y,int color){
    
    
        int m = grid.length, n = grid[0].length;
        int cnt = 0;
        for(int[] a : dir){
    
    
            int nx = x + a[0];
            int ny = y + a[1];
            if(nx < 0 || nx >= m || ny < 0 || ny >= n){
    
    
                continue;
            }
            if(grid[nx][ny] != grid[x][y] ){
    
    
                continue;
            }
            cnt++;
            if(ans[nx][ny] == -1){
    
    
                continue;
            }
            ans[nx][ny] = -1;
            dfs(grid,nx,ny,color);
        }
        if(cnt != 4){
    
    
            ans[x][y] = color;
        }else{
    
    
            ans[x][y] = grid[x][y];
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43260719/article/details/121770204