-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloodFill.java
More file actions
44 lines (29 loc) · 1.02 KB
/
FloodFill.java
File metadata and controls
44 lines (29 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package Leetcode;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;
public class FloodFill
{
public static void main( String[] args )
{
Scanner s = new Scanner( System.in );
int[][] image = {{0,0,0},{0,1,1}};
floodFill( image, 1, 1, 1 );
}
public static int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
if(image==null || image.length==0)
return null;
dsf(image, sr,sc, newColor, image[sr][sc]);
return image;
}
private static void dsf(int[][] image, int i, int j, int newColor, int oldColor)
{
if(i<0 || i>=image.length || j<0 || j>=image[0].length || image[i][j]!=oldColor )
return;
image[i][j] = newColor;
dsf(image, i+1, j, newColor, oldColor);
dsf(image, i-1, j, newColor, oldColor);
dsf(image, i, j+1, newColor, oldColor);
dsf(image, i, j-1, newColor, oldColor);
}
}