-
Notifications
You must be signed in to change notification settings - Fork 87
/
FloodFillAlgorithm.java
98 lines (72 loc) · 1.86 KB
/
FloodFillAlgorithm.java
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
95
96
97
import java.util.*;
import java.awt.Point;
public class FloodFillAlgorithm
{
static boolean isValid(int[][] screen, int m, int n, int x, int y, int prevC, int newC)
{
if(x < 0 || x >= m || y < 0 || y >= n || screen[x][y] != prevC
|| screen[x][y]== newC)
return false;
return true;
}
static void floodFill(int[][] screen, int m, int n, int x, int y, int prevC, int newC)
{
Vector<Point> queue = new Vector<Point>();
queue.add(new Point(x, y));
screen[x][y] = newC;
while(queue.size() > 0)
{
Point currPixel = queue.get(queue.size() - 1);
queue.remove(queue.size() - 1);
int posX = currPixel.x;
int posY = currPixel.y;
if(isValid(screen, m, n, posX + 1, posY, prevC, newC))
{
screen[posX + 1][posY] = newC;
queue.add(new Point(posX + 1, posY));
}
if(isValid(screen, m, n, posX-1, posY, prevC, newC))
{
screen[posX-1][posY]= newC;
queue.add(new Point(posX-1, posY));
}
if(isValid(screen, m, n, posX, posY + 1, prevC, newC))
{
screen[posX][posY + 1]= newC;
queue.add(new Point(posX, posY + 1));
}
if(isValid(screen, m, n, posX, posY-1, prevC, newC))
{
screen[posX][posY-1]= newC;
queue.add(new Point(posX, posY-1));
}
}
}
public static void main(String[] args) {
int[][] screen ={
{1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 0, 0},
{1, 0, 0, 1, 1, 0, 1, 1},
{1, 2, 2, 2, 2, 0, 1, 0},
{1, 1, 1, 2, 2, 0, 1, 0},
{1, 1, 1, 2, 2, 2, 2, 0},
{1, 1, 1, 1, 1, 2, 1, 1},
{1, 1, 1, 1, 1, 2, 2, 1}};
// Row of the display
int m = screen.length;
int n = screen.length;
int x = 4;
int y = 4;
int prevC = screen[x][y];
int newC = 3;
floodFill(screen, m, n, x, y, prevC, newC);
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
System.out.print(screen[i][j] + " ");
}
System.out.println();
}
}
}