-
Notifications
You must be signed in to change notification settings - Fork 4
/
WaveAlgorithm.java
40 lines (39 loc) · 1.42 KB
/
WaveAlgorithm.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
import java.util.LinkedList;
import java.util.Queue;
public class WaveAlgorithm {
public static void colorize(int[][] map) {
boolean broke = false;
Queue<int[]> queue = new LinkedList<>();
for (int row = 0; row < map.length; row++) {
for (int col = 0; col < map[row].length; col++) {
if (map[row][col] == 1) {
queue.add(new int[]{row, col});
broke = true;
break;
}
}
if (broke) {
break;
}
}
while (queue.size() > 0) {
int[] tile = queue.remove();
if (map[tile[0] + 1][tile[1]] == 0) {
map[tile[0] + 1][tile[1]] = map[tile[0]][tile[1]] + 1;
queue.add(new int[]{tile[0] + 1, tile[1]});
}
if (map[tile[0]][tile[1] + 1] == 0) {
map[tile[0]][tile[1] + 1] = map[tile[0]][tile[1]] + 1;
queue.add(new int[]{tile[0], tile[1] + 1});
}
if (map[tile[0] - 1][tile[1]] == 0) {
map[tile[0] - 1][tile[1]] = map[tile[0]][tile[1]] + 1;
queue.add(new int[]{tile[0] - 1, tile[1]});
}
if (map[tile[0]][tile[1] - 1] == 0) {
map[tile[0]][tile[1] - 1] = map[tile[0]][tile[1]] + 1;
queue.add(new int[]{tile[0], tile[1] - 1});
}
}
}
}