leetcode733(画像レンダリング:画像検索)

2次元整数配列で表される画像があり、各整数は画像のピクセル値を表し、値は0〜65535です。

画像がレンダリングされるピクセル値(行、列)を表す座標(sr、sc)と、画像の色を変更できる新しいカラー値newColorを指定します。

着色作業を完了するには、初期座標から始めて、初期座標と同じピクセル値を持つ接続ピクセルを初期座標の4方向に記録し、次に、これらの4方向の条件を満たす4つのピクセルとそれに対応する4つのピクセルを記録します。方向の初期座標と同じピクセル値を持つ接続されたピクセル、...、プロセスを繰り返します。記録されたすべてのピクセルのカラー値を新しいカラー値に変更します。

最後に、カラーリング後、レンダリングされたイメージに戻ります。

例:
入力:
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で表される値に変更し、このポイントの周りの他のポイントを検索します。満足できない場合は、何もせずに直接戻ります。このポイントで他の方向を検索しないでください。

ソリューション(1):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);
    }
}

問題解決(2):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