-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapHelper.java
More file actions
23 lines (19 loc) · 754 Bytes
/
MapHelper.java
File metadata and controls
23 lines (19 loc) · 754 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class MapHelper {
public static int countReachableCells(Map map) {
if (map == null)
throw new IllegalArgumentException("Arguments cannot be null");
boolean[][] visited = new boolean[map.height][map.width];
// subtract one to exclude starting point
return bfs(map.getStartCell(), visited) - 1;
}
private static int bfs(Cell currentCell, boolean[][] visited) {
visited[currentCell.y][currentCell.x] = true;
int touchedCells = 0;
for (Cell adjCell : currentCell.getAdjCells()) {
if (!adjCell.isWall && !visited[adjCell.y][adjCell.x]) {
touchedCells += dfs(adjCell, visited);
}
}
return ++touchedCells;
}
}