- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloodFill.java
More file actions
Latest commit
21 lines (19 loc) · 814 Bytes
/
Copy pathFloodFill.java
File metadata and controls
21 lines (19 loc) · 814 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
publicclassFloodFill {
publicint[][] floodFill(int[][] image, intsr, intsc, intnewColor) {
if(newColor == image[sr][sc]) returnimage;
introws = image.length;
intcols = image[0].length;
intsrc = image[sr][sc];
dfs(image, sr, sc, newColor, rows, cols, src);
returnimage;
}
publicvoiddfs(int[][] image, intsr, intsc, intnewColor, introws, intcols, intsrc) {
if(sr < 0 || sc < 0 || sr >= rows || sc >= cols) return;
elseif (image[sr][sc] != src) return;
image[sr][sc] = newColor;
dfs(image, sr+1,sc,newColor,rows,cols,src);
dfs(image, sr-1,sc,newColor,rows,cols,src);
dfs(image, sr,sc+1,newColor,rows,cols,src);
dfs(image, sr,sc-1,newColor,rows,cols,src);
}
}