안녕하세요 저는 맨해튼 거리를 비용으로 사용하는 a * 경로 찾기를 구현하고 있습니다. 여기에 검색 코드가 있습니다.
public LinkedList<MapNode> search(MapNode startNode, MapNode goalNode) {
LinkedList<MapNode> closedList = new LinkedList<MapNode>();
LinkedList<MapNode> openList = new LinkedList<MapNode>();
openList.add(startNode);
startNode.setPathParent(null);
while (!openList.isEmpty()) {
MapNode node = (MapNode) openList.removeFirst();
if (node == goalNode)
return constructPath(goalNode);
else {
closedList.add(node);
// add neighbors to the open list
Iterator<MapNode> i = getNeighbours(node, goalNode).iterator();
while (i.hasNext()) {
MapNode neighborNode = (MapNode) i.next();
if (!closedList.contains(neighborNode) && !openList.contains(neighborNode)) {
neighborNode.setPathParent(node);
openList.add(neighborNode);
}
}
}
}
// no path found
return null;
}
내 getneighbours()
방법은 다음 과 같습니다.
public LinkedList<MapNode> getNeighbours(MapNode node, MapNode goalNode) {
Position positionCheck = new Position(0, 0);
for (int x = -1; x < 2; x++){
int newX = node.getPosition().getPositionX() + x;
if(newX<0 || newX >= Map.DIMENSION){continue;}
for (int y = -1; y < 2; y++){
int newY = node.getPosition().getPositionY() + y;
if(newY<0 || newY >= Map.DIMENSION){continue;}
positionCheck.setPositionX(newX);
positionCheck.setPositionY(newY);
if(!node.getPosition().equals(positionCheck)){
calculateCost(map.elementAt(positionCheck), goalNode);
neighbours.add(map.elementAt(positionCheck));
}
}
}
return neighbours;
}
작동하지만 불행히도 맨해튼 원칙을 사용하지 않는 출력은 다음과 같습니다.
Shortest Delivery Point is: 6 miles! - 8,7 // using manhattan distance
S = Start, D=Destination, "."= path taken.
[3,4] ->
[4,3] ->
[5,4] ->
[6,5] ->
[ D ] ->
[0,0] [0,1] [0,2] [0,3] [0,4] [0,5] [0,6] [0,7]
[1,0] [1,1] [1,2] [1,3] [1,4] [1,5] [1,6] [1,7]
[2,0] [2,1] [2,2] [2,3] [2,4] [ S ] [2,6] [2,7]
[3,0] [3,1] [3,2] [3,3] [ . ] [3,5] [3,6] [3,7]
[4,0] [4,1] [4,2] [ . ] [4,4] [4,5] [4,6] [4,7]
[5,0] [5,1] [5,2] [5,3] [ . ] [5,5] [5,6] [5,7]
[6,0] [6,1] [6,2] [6,3] [6,4] [ . ] [6,6] [6,7]
[7,0] [7,1] [7,2] [7,3] [7,4] [7,5] [ D ] [7,7]
나는 누군가가 내가 할 수없는 것을 발견 할 수 있는지 궁금해했습니다. 처음으로 길 찾기를 엉망으로 만들었으므로 조금 나이가 들었습니다. 도와 주셔서 감사합니다..
공개 목록에서 제거하는 노드는 F 비용이 가장 작은 노드 여야합니다. 따라서 연결 목록을 사용하는 것은 잘못된 선택입니다. 노드를 추출하려면 완전히 검색해야하거나 노드를 삽입하려면 완전히 검색해야합니다. 하지만, 나쁜 선택이든 아니든, 당신은 둘 다하지 않기 때문에 이렇게 옳지 않습니다.
또한 이웃이 이미 열려있는 목록에있는 경우 G 점수를 비교하고 새 경로가 더 나은 경우 다시 부모로 지정해야합니다.
닫힌 집합에 연결 목록을 사용하는 것도 나쁜 선택입니다. "추가"와 "포함"만 필요하며 연결 목록에 포함은 끔찍합니다. 그래도 정확성에는 영향을 미치지 않습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다