This repository was archived by the owner on Feb 19, 2020. It is now read-only.
forked from aswinkumarrk/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloodFill_733.java
More file actions
52 lines (44 loc) · 1.77 KB
/
FloodFill_733.java
File metadata and controls
52 lines (44 loc) · 1.77 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
45
46
47
48
49
50
51
52
package com.leetcode.problems.medium;
import com.geeksforgeeks.array.Rotate2DMatrix;
/**
* @author neeraj on 01/09/19
* Copyright (c) 2019, data-structures.
* All rights reserved.
*/
public class FloodFill_733 {
public static void main(String[] args) {
floodFill(new int[][]{
{1,1,1}, {1,1,0}, {1,0,1}
}, 1,1, 2);
floodFill(new int[][]{
{0, 0, 0}, {0, 1, 1}
}, 1, 1, 1);
}
public static int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
int initialColor = image[sr][sc];
boolean[][] visited = new boolean[image.length][image[0].length];
dfs(image, visited, sr, sc, newColor, initialColor);
Rotate2DMatrix.print2DArray(image);
return image;
}
public static void dfs(int[][] image, boolean[][] visited, int sr, int sc, int newColor, int initialColor) {
image[sr][sc] = newColor;
visited[sr][sc] = true;
if (isSafe(image, visited, sr - 1, sc, initialColor)) {
dfs(image, visited, sr - 1, sc, newColor, initialColor);
}
if (isSafe(image, visited, sr, sc + 1, initialColor)) {
dfs(image, visited, sr, sc + 1, newColor, initialColor);
}
if (isSafe(image, visited, sr + 1, sc, initialColor)) {
dfs(image, visited, sr + 1, sc, newColor, initialColor);
}
if (isSafe(image, visited, sr, sc - 1, initialColor)) {
dfs(image, visited, sr, sc - 1, newColor, initialColor);
}
}
public static boolean isSafe(int[][] image, boolean[][] visited, int sr, int sc, int initialColor) {
return sr >= 0 && sc >= 0 && sr < image.length && sc < image[0].length
&& image[sr][sc] == initialColor && visited[sr][sc] == false;
}
}