leetcode733(图像渲染:图的搜索)

有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间。

给你一个坐标 (sr, sc) 表示图像渲染开始的像素值(行 ,列)和一个新的颜色值 newColor,让你重新上色这幅图像。

为了完成上色工作,从初始坐标开始,记录初始坐标的上下左右四个方向上像素值与初始坐标相同的相连像素点,接着再记录这四个方向上符合条件的像素点与他们对应四个方向上像素值与初始坐标相同的相连像素点,……,重复该过程。将所有有记录的像素点的颜色值改为新的颜色值。

最后返回经过上色渲染后的图像。

例:
输入:
image = [[1,1,1],[1,1,0],[1,0,1]]
sr = 1, sc = 1, newColor = 2
输出: [[2,2,2],[2,2,0],[2,0,1]]

这道题是一道典型的图的搜索问题,在搜索到图中的某一个点时,我们只需要判断该点的数值是否满足题目要求,若满足,则改变成newColor所代表的数值,并在该点往周围其他点进行搜索,若不满足,则直接返回,不进行任何操作,不在该点往其他方向搜索

题解(一):DFS

class Solution {
    
    
    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
    
    
            if(image.length==0||image[0].length==0)
                return image;
            boolean[][]search=new boolean[image.length][image[0].length];
            move(sr,sc,image,newColor,image[sr][sc],search);
            return image;
    }
    private void move(int x,int y,int[][]image,int newColor,int target,boolean[][] search){
    
    
        if(x<0||x>= image.length||y<0||y>=image[0].length)
            return;
        if(image[x][y]!=target||search[x][y])
            return;
        image[x][y]=newColor;
        search[x][y]=true;
        move(x+1,y,image,newColor,target,search);
        move(x-1,y,image,newColor,target,search);
        move(x,y+1,image,newColor,target,search);
        move(x,y-1,image,newColor,target,search);
    }
}

题解(二):BFS

class Solution {
    
    
    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
    
    
            if(image.length==0||image[0].length==0)
                return image;
            boolean[][]search=new boolean[image.length][image[0].length];
            Queue<int[]>order=new LinkedList<>();
            int target=image[sr][sc];
            image[sr][sc]=newColor;
            order.add(new int[]{
    
    sr,sc});
            search[sr][sc]=true;
            while(!order.isEmpty()){
    
    
                int[]temp=order.poll();
                if(temp[0]+1< image.length&&!search[temp[0]+1][temp[1]]&&image[temp[0]+1][temp[1]]==target) {
    
    
                    order.add(new int[]{
    
    temp[0] + 1, temp[1]});
                    search[temp[0]+1][temp[1]]=true;
                    image[temp[0]+1][temp[1]]=newColor;
                }
                if(temp[0]-1>=0&&!search[temp[0]-1][temp[1]]&&image[temp[0]-1][temp[1]]==target) {
    
    
                    order.add(new int[]{
    
    temp[0] - 1, temp[1]});
                    search[temp[0]-1][temp[1]]=true;
                    image[temp[0]-1][temp[1]]=newColor;
                }
                if(temp[1]+1< image[1].length&&!search[temp[0]][temp[1]+1]&&image[temp[0]][temp[1]+1]==target) {
    
    
                    order.add(new int[]{
    
    temp[0], temp[1]+1});
                    search[temp[0]][temp[1]+1]=true;
                    image[temp[0]][temp[1]+1]=newColor;
                }
                if(temp[1]-1>=0&&!search[temp[0]][temp[1]-1]&&image[temp[0]][temp[1]-1]==target) {
    
    
                    order.add(new int[]{
    
    temp[0] , temp[1]-1});
                    search[temp[0]][temp[1]-1]=true;
                    image[temp[0]][temp[1]-1]=newColor;
                }
            }
            return image;
    }

}

猜你喜欢

转载自blog.csdn.net/CY2333333/article/details/108034024