-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS_Floodfill.java
More file actions
68 lines (54 loc) · 1.97 KB
/
BFS_Floodfill.java
File metadata and controls
68 lines (54 loc) · 1.97 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
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class BFS_Example {
public static boolean inBounds(int rows, int cols, int r, int c) {
return r >= 0 && r < rows && c >= 0 && c < cols;
}
public static void main(String[] args) {
char[][] grid = {
{'1', '1', '1', '1', '1'},
{'1', '2', '3', '3', '2'},
{'3', '3', '3', '3', '5'},
{'5', '2', '4', '3', '4'},
{'4', '3', '1', '3', '4'},
{'5', '3', '5', '3', '2'},
};
// This is an example of floodfill (used in MS Paint,etc) that replaces
// squares of adjacent colors with another color
// This example replaces the color 3 at square (2,2) and all its neighbors with the color '6'
int rows = grid.length, cols = grid[0].length;
Queue<Pair> q = new LinkedList<>();
q.add(new Pair(2, 2));
int[] xs = {-1, 0, 1, 0};
int[] ys = {0, 1, 0, -1};
boolean[][] visited = new boolean[rows][cols];
while (!q.isEmpty()) {
Pair top = q.poll();
if (visited[top.row][top.column]) continue;
visited[top.row][top.column] = true;
grid[top.row][top.column] = '6';
for (int d = 0; d < 4; d++) {
int newRow = top.row + xs[d];
int newCol = top.column + ys[d];
if (inBounds(rows, cols, newRow, newCol) && grid[newRow][newCol] == '3')
q.add(new Pair(newRow, newCol));
}
}
for(char [] a: grid) System.out.println(Arrays.toString(a));
}
public static class Pair {
int row, column;
public Pair(int i, int j) {
row = i;
column = j;
}
public Pair(Pair p) {
row = p.row;
column = p.column;
}
public String toString() {
return row + "," + column;
}
}
}