733. 图像渲染(leetcode)

733. 图像渲染

class Solution {

    int[] dx={0,1,0,-1};
    int[] dy={1,0,-1,0};
    int n;
    int m;
    int[][] image;
    boolean[][] vis;
    int color;
    int sourceColor;
    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        m=image.length;
        n=image[0].length;
        vis=new boolean[m][n];
        this.image=image;
        this.color=color;
        sourceColor=image[sr][sc];
        dfs(sr,sc);
        return image;
    }

    void dfs(int x,int y)
    {
        vis[x][y]=true;
        image[x][y]=color;
        for(int i=0;i<4;i++)
        {
            int a=dx[i]+x,b=dy[i]+y;
            if(a>=0 && b>=0 && a<m && b<n && vis[a][b]==false && sourceColor==image[a][b])
            {
                dfs(a,b);
            }
        }
    }
}

 

posted @ 2024-10-08 03:45  风乐  阅读(2)  评论(0编辑  收藏  举报