8 퍼즐 게임에 대한 폭 - 우선 알고리즘을 구현하려하고 있습니다. 나는 그것이 새로운 사례가 아니며 웹상에서 해결책을 찾는다는 것을 알고 있지만, 나는 사고 방식으로 그것을 만들고 싶다.폭스 우선 검색 알고리즘 JAVA 8- 퍼즐
이 코드는 이미
123
456
780
인 노드 결과를 발견하지만 그것을 할 수있는 35 단계를 수행!
의견을 보내 주시면 감사하겠습니다. Wikipedia 따르면 =)는
//This method receives a Collection of `Nodo` objects, each one will be checked and compare with the finalGoal
public void percorreNodos(Collection<Nodo> nodosBase)
{
//In this class a have an array that has all the IDs os nodes that already has been checked
//The ID of a Node, is the representation: 123456780, 354126870 , etc..
System.out.println("idsPercorrido.size() = " + idsPercorridos.size());
//Check if the collection nodosBase contains the finalGoal
Iterator<Nodo> iterator = nodosBase.iterator();
while(iterator.hasNext())
{
Nodo nodoBase = (Nodo) iterator.next();
//If this current node has already been checked, we dont have to check it again
idsPercorridos.add(nodoBase.getId());
//Just print the node (sysout)
nodoBase.print();
contPassos++;
System.out.println("\n" + contPassos + " STEPS(number of nodes checked)...");
//Check if this node is the final goal
if(nodoBase.isObjetivoFinal())
{
//set the variable indicating that the result has been found
encontrouObjetivo = true;
System.out.println("Resultado alcancado EM " + contPassos + " PASSOS...");
nodoBase.print();
break;
}
}
// Now that we know that no one Node of nosoBase collection is the final goal, we are going to generate the next children to be checked, and call this function recursively
//Just confirm that we didnt find the goal
if(encontrouObjetivo == false)
{
//Creates the next frontier
Collection<Nodo> novaFronteira = new HashSet<Nodo>();
for(Nodo nodoPai : nodosBase)
{
//Generate each Node its childrens and add to a collection called "novaFronteira"
Collection<Nodo> filhos = nodoPai.gerarFilhos();
for(Nodo filho : filhos)
{
//idsPercorridos is a collection<String> which contains all the nodes ids that we checked, we dont want to check a node more than one time !
if(idsPercorridos.contains(filho.getId()) == false)
{
novaFronteira.add(filho);
}
}
}
this.percorreNodos(novaFronteira);
}
}
변수 이름을 영어로 변경할 수 있습니다. 아마 당신이 더 많은 반응을 얻는 데 도움이 될 것입니다. – Khaelid
또한이 질문은 Code Review http://codereview.stackexchange.com – Khaelid
에 더 적합 할 것입니다. 8- 퍼즐에는 패리티로 인해 BFS에서 도달 할 수있는 고유 상태가 9 개/2 = 181,440 개 있습니다. 폭 넓은 우선 검색은 일반적으로 위에서 작성한 초기 상태에서 시작하여 바깥쪽으로 검색합니다. 적은 작업을 원할 경우, IDA *와 같은 경험적 방법과 알고리즘을 사용해야한다고 생각해야합니다. –