-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS.java
More file actions
53 lines (42 loc) · 1.53 KB
/
DFS.java
File metadata and controls
53 lines (42 loc) · 1.53 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
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class DFS extends SearchTool{
private List<Site> path;
public DFS(Dungeon dungeon){
super(dungeon, dungeon.size());
path = new ArrayList<>();
}
public boolean dfs(Site current, Site target, boolean[][] visited, String pathType) {
if (current.equals(target)) return true;
if (visited[current.i()][current.j()]) return false;
visited[current.i()][current.j()] = true;
List<Site> neighbors;
if(pathType.equals(Dungeon.ROOM)){
neighbors = dungeon.getNeighborsInRoom(current);
}
else{
neighbors = dungeon.getNeighborsInCorridor(current);
}
for (Site neighbor : neighbors) {
if (isValidMove(current, neighbor, visited) &&
((dungeon.isCorridor(neighbor)&&pathType.equals(Dungeon.CORRIDOR)) || (dungeon.isRoom(neighbor)&&pathType.equals(Dungeon.ROOM))))
{
if (dfs(neighbor, target, visited, pathType)) {
path.add(neighbor);
return true;
}
}
}
return false;
}
public boolean isPathBetweenPoints(Site start, Site end, String pathType) {
boolean[][] visited = new boolean[N][N]; // 记录已访问的位置
path = new ArrayList<>();
path.add(start);
return dfs(start, end, visited, pathType);
}
public List<Site> getPath(){
return path;
}
}