Ben java genişliğinde ilk arama uygulamak için bir okul excersice var. Sorun benim arama çalışmıyor ve beni tavsiye ve bana nihai sorun olabilir nereye bazı guidlines vermek isteyen sorunu :(Yani Im bulamıyorum ki ama neredeyse her şeyi uyguladı.Genişlik İlk Arama - Java
public ArrayList<SearchNode> search(Problem p) {
// The frontier is a queue of expanded SearchNodes not processed yet
frontier = new NodeQueue();
/// The explored set is a set of nodes that have been processed
explored = new HashSet<SearchNode>();
// The start state is given
GridPos startState = (GridPos) p.getInitialState();
// Initialize the frontier with the start state
frontier.addNodeToFront(new SearchNode(startState));
// Path will be empty until we find the goal.
path = new ArrayList<SearchNode>();
// The start NODE
SearchNode node = new SearchNode(startState);
// Check if startState = GoalState??
if(p.isGoalState(startState)){
path.add(new SearchNode(startState));
return path;
}
do {
node = frontier.removeFirst();
explored.add(node);
ArrayList reachable = new ArrayList<GridPos>();
reachable = p.getReachableStatesFrom(node.getState());
SearchNode child;
for(int i = 0; i< reachable.size(); i++){
child = new SearchNode((GridPos)reachable.get(i));
if(!(explored.contains(child) || frontier.contains(child))){
if(p.isGoalState(child.getState())){
path = child.getPathFromRoot() ;
return path;
}
frontier.addNodeToFront(child);
}
}
}while(!frontier.isEmpty());
return path;
}
Eğer
Nasıl çalışmıyor? Kesin ol. – Borgleader
"Yanlış" düğümleri ve yolu araştırıyor gibi görünüyor. – mrjasmin
Bize göstermediğiniz birçok yönteminiz var. İki farklı diziden/listeden düğümler çıkarmakta ve ulaşılabilir düğümleri yalnızca birine yerleştiriyor gibi görünüyorsunuz. Durumunuz da garip, klasik bir uygulamada sadece 'keşfedilen' listesini kontrol etmelisiniz. Temel fikir şudur: İlk düğümü bir listenin başlangıcından çıkarın, tüm komşularını aynı listenin sonuna ekleyin. Liste boş olduğunda veya hedef düğümü bu listeye eklediğinizde durun. – IVlad