733. 图像渲染(leetcode)
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);
}
}
}
}