-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDFS.java
More file actions
70 lines (67 loc) · 1.95 KB
/
DFS.java
File metadata and controls
70 lines (67 loc) · 1.95 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
69
70
import java.io.IOException;
import java.util.Stack;
public class DFS extends Search{
Stack<Node> stack = new Stack<Node>();
public static void main(String[] args) throws IOException {
DFS dfs= new DFS();
dfs.readInput();
dfs.stack.push(dfs.startNode);
Node end = dfs.search();
dfs.findPath(end);
System.out.println("Path Cost: "+dfs.pathCost);
System.out.println("Nodes Expanded: "+dfs.nodesExpanded);
System.out.println("Max Depth: "+dfs.maxDepth);
System.out.println("Maximum size of the frontier: "+dfs.maxFrontierSize);
dfs.printMaze();
//System.out.println(end.xPos+":"+end.yPos);
}
private Node search() {
while(!stack.isEmpty()){
if(stack.size()>maxFrontierSize)
maxFrontierSize = stack.size();
Node node = stack.pop();
nodesExpanded++;
int xPos = node.xPos, yPos = node.yPos;
//System.out.println(xPos+";"+yPos);
if(maze.get(yPos).charAt(xPos)=='.')
return node;
if(maze.get(yPos).charAt(xPos+1)!='%'){
Node child = new Node(xPos+1,yPos,node,node.depth+1);
if(node.depth+1>maxDepth)
maxDepth = node.depth+1;
if(visited[xPos+1][yPos]!=1){
visited[xPos+1][yPos]=1;
stack.push(child);
}
}
if(maze.get(yPos-1).charAt(xPos)!='%'){
Node child = new Node(xPos,yPos-1,node,node.depth+1);
if(node.depth+1>maxDepth)
maxDepth = node.depth+1;
if(visited[xPos][yPos-1]!=1){
visited[xPos][yPos-1]=1;
stack.push(child);
}
}
if(maze.get(yPos+1).charAt(xPos)!='%'){
Node child = new Node(xPos,yPos+1,node,node.depth+1);
if(node.depth+1>maxDepth)
maxDepth = node.depth+1;
if(visited[xPos][yPos+1]!=1){
visited[xPos][yPos+1]=1;
stack.push(child);
}
}
if(maze.get(yPos).charAt(xPos-1)!='%'){
Node child = new Node(xPos-1,yPos,node,node.depth+1);
if(node.depth+1>maxDepth)
maxDepth = node.depth+1;
if(visited[xPos-1][yPos]!=1){
visited[xPos-1][yPos]=1;
stack.push(child);
}
}
}
return null;
}
}