-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWaterSpillage.java
More file actions
53 lines (44 loc) · 1.11 KB
/
WaterSpillage.java
File metadata and controls
53 lines (44 loc) · 1.11 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
/**
* Implements the algorithm to spread water in a labyrinth.
*/
package matrix;
class WaterSpillage {
/**
* Sets the map and spills water.
* @param args Program arguments, not used.
*/
public static void main(String[] args) {
int[][] map = {
{1, 1, 1, 1, 1, 1},
{1, 0, 1, 0, 1, 1},
{1, 0, 1, 1, 1, 1},
{1, 0, 0, 1, 0, 1},
{1, 0, 1, 0, 0, 1},
{1, 1, 1, 1, 1, 1},
};
MatrixMap.print(map);
spillWater(map, new Point(1,1));
MatrixMap.print(map);
}
/**
* Spills the water in the labyrinth.
* @param lab The labyrinth where it happens.
* @param p The point where the flood starts.
*/
public static void spillWater(int[][] lab, Point p) {
if (p.x < 0 || p.y < 0 ||
p.x >= lab.length || p.y >= lab.length ||
lab[p.x][p.y] != 0) {
return;
}
lab[p.x][p.y] = 2;
for (int x = p.x - 1; x <= p.x + 1; x++) {
for (int y = p.y -1; y <= p.y + 1; y++) {
if (p.x != x && p.y != y) { // remove diagonals
continue;
}
spillWater(lab, new Point(x, y));
}
}
}
}