-
Notifications
You must be signed in to change notification settings - Fork 1
/
Search.java
59 lines (44 loc) · 1.3 KB
/
Search.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.*;
public class Search {
List<Node> BFS(Node root,int [] gol){
List<Node> PathToSolve = new ArrayList<Node>();
List<Node> OpenList = new ArrayList<Node>();
List<Node> CloseList = new ArrayList<Node>();
OpenList.add(root);
boolean goalFound = false ;
while(!OpenList.isEmpty() && !goalFound) {
Node currrentNode = OpenList.get(0);
CloseList.add(currrentNode);
OpenList.remove(0);
currrentNode.expandMove();
for(int i=0 ; i< currrentNode.chlidern.size();i++) {
Node currrentChi = currrentNode.chlidern.get(i);
if(currrentChi.isGaol(gol)) {
// System.out.println("Goal is found ");
goalFound =true ;
Pathtest(PathToSolve,currrentChi);
}
if(!contains(OpenList,currrentChi)||!contains(CloseList,currrentChi)) {
OpenList.add(currrentChi);
}
}
}
return PathToSolve;
}
public boolean contains(List<Node> lis ,Node co) {
boolean cout = false;
for(int i=0; i<lis.size();i++) {
if(lis.get(i).isSamePuz(co.puzzle)) {
cout = true ;}
}
return cout;
}
void Pathtest(List<Node> path ,Node n) {
Node current = n;
path.add(current);
while(current.pirent != null) {
current = current.pirent;
path.add(current);
}
}
}