forked from kant003/JavaPracticeHacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloodFill.java
More file actions
84 lines (64 loc) · 1.52 KB
/
Copy pathFloodFill.java
File metadata and controls
84 lines (64 loc) · 1.52 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.awt.*;
import java.util.List;
import java.util.ArrayList;
/**
* Created by gustavvalentin on 31/10/2016
*/
public class FloodFill {
private enum Direction {
NORTH(0, 1), SOUTH(0, -1), EAST(1, 0), WEST(-1, 0);
private final int x, y;
Direction(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Point derive(Point point) {
return new Point(point.x + getX(), point.y + getY());
}
}
private List<Point> flood() {
final List<Point> points = new ArrayList<Point>();
flood(points, new Point(5, 5), new Condition() {
@Override
public boolean accept() {
return points.size() > 100;
}
}, new Filter<Point>() {
@Override
public boolean accept(Point point) {
return true;
}
});
return points;
}
private void flood(List<Point> points, Point start, Condition stopCondition, Filter<Point> filter) {
if (points.contains(start) || stopCondition.accept()) {
return;
}
points.add(start);
for (Direction direction : Direction.values()) {
Point derived = direction.derive(start);
if (points.contains(derived) || !filter.accept(derived)) {
continue;
}
flood(points, derived, stopCondition, filter);
}
}
private interface Condition {
boolean accept();
}
private interface Filter<T> {
boolean accept(T t);
}
public static void main(String[] args) {
FloodFill floodFill = new FloodFill();
List<Point> test = floodFill.flood();
System.out.println(test);
}
}