-
-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathflood_fill.java
More file actions
96 lines (92 loc) · 3.77 KB
/
flood_fill.java
File metadata and controls
96 lines (92 loc) · 3.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
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
85
86
87
88
89
90
91
92
93
94
package code.java;
import java.util.*;
public class flood_fill {
static class Point{
int x;
int y;
public Point(int x,int y){
this.x = x;
this.y = y;
}
}
public static boolean inbound(int[][] grid, Point p){
int row = grid.length, col = grid[0].length;
return (p.x < 0 || p.x >= row || p.y < 0 || p.y >= col);
}
public static List<Point> find_neighbors(int[][] grid, Point p, int old_val, int new_val){
//north, south, east, west neighbors
List<Point> possible_neighbors = new ArrayList<>();
List<Point> neighbors = new ArrayList<>();
possible_neighbors.add(new Point(p.x,p.y + 1));
possible_neighbors.add(new Point(p.x,p.y - 1));
possible_neighbors.add(new Point(p.x - 1, p.y));
possible_neighbors.add(new Point(p.x + 1, p.y));
//exclude the neighbors that go out of bounds and should not be colored
for (Point possible_neighbor : possible_neighbors){
if(inbound(grid,possible_neighbor) && grid[possible_neighbor.x][possible_neighbor.y] == old_val){
neighbors.add(possible_neighbor);
}
}
return neighbors;
}
public static void stack_fill(int[][] grid, Point p, int old_val, int new_val){
if (old_val == new_val) return;
Stack<Point> stack = new Stack<>();
stack.add(p);
while (!stack.empty()){
Point cur = stack.pop();
grid[cur.x][cur.y] = new_val;
for (Point neighbor : find_neighbors(grid, cur, old_val, new_val)){
stack.add(neighbor);
}
}
}
public static void recursive_fill(int[][] grid, Point p, int old_val, int new_val){
if (old_val == new_val) return;
grid[p.x][p.y] = new_val;
for (Point neighbor : find_neighbors(grid, p, old_val, new_val)){
recursive_fill(grid, neighbor, old_val, new_val);
}
}
public static void queue_fill(int[][] grid, Point p, int old_val, int new_val){
if (old_val == new_val) return;
Queue<Point> queue = new ArrayDeque<>();
queue.add(p);
while (!queue.isEmpty()){
Point cur = queue.remove();
for (Point neighbor : find_neighbors(grid, cur, old_val, new_val)){
grid[neighbor.x][neighbor.y] = new_val;
queue.add(neighbor);
}
}
}
public static boolean isEqual(int[][] grid, int[][] solution){
for(int i = 0; i < grid.length; i++){
for(int j = 0; j < grid[0].length; j++) {
if(grid[i][j] != solution[i][j]) return false;
}
}
return true;
}
public static void main(String[] args) {
Point start = new Point(0,0);
int[][] grid = {
{0, 0, 1, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 1, 0, 0}};
int[][] solution = {
{1, 1, 1, 0, 0},
{1, 1, 1, 0, 0},
{1, 1, 1, 0, 0},
{1, 1, 1, 0, 0},
{1, 1, 1, 0, 0}};
queue_fill(grid, start, 0, 1);
if(isEqual(grid, solution)) System.out.println("test pass");
stack_fill(grid, start, 0, 1);
if(isEqual(grid, solution)) System.out.println("test pass");
recursive_fill(grid, start, 0, 1);
if(isEqual(grid, solution)) System.out.println("test pass");
}
}