Professional Documents
Culture Documents
NPM: 18312215
package com.hendro;
import java.awt.Point;
import java.util.LinkedList;
int maps[][] = {
{1, 3, 2, 3, 3},
{2, 3, -1, -1, -1},
{2, 4, -1, 1, -1},
{5, 2, -1, 3, -1},
{2, 3, 5, 1, 4}
};
Point direction[] = {
new Point(1, 0), new Point(1, 1),
new Point(0, 1), new Point(-1, 1),
new Point(-1, 0), new Point(-1, -1),
new Point(0, -1), new Point(1, -1),};
public Astar() {
Point start = new Point(3, 4);
Point target = new Point(3, 0);
System.out.println("Hasil Pencarian A*");
LinkedList<MapNode> path1 = findPath(start, target);
if (path1 == null) {
System.out.println("Alamat Tidak Ditemukan");
} else {
int cost = 0;
for (MapNode node : path1) {
Point loc = node.getLocation();
cost += maps[loc.y][loc.x];
if (!node.getLocation().equals(target)) {
System.out.println(node.getId() + " -> ");
} else {
System.out.println("(" + node.getId() + ")");
}
}
System.out.println("\nTotal cost : " + cost);
}
openList.add(new MapNode(src));
MapNode leaves = null;
while (openList.size() > 0) {
MapNode cNode = openList.removeFirst();
closedList.add(cNode);
if (cNode.equals(dst)) {
leaves = cNode;
break;
} else {
cNode.addChild(child);
addToList(openList, dst, child);
}
}
}
}
if (leaves != null) {
return getPath(leaves);
}
return null;
}
private void addToList(LinkedList<MapNode> openList, Point dst, MapNode node) {
int i = 0;
double hValue = getSLDHeuristic(node.getLocation(), dst);
for (i = 0; i < openList.size(); i++) {
double tmpH = getSLDHeuristic(openList.get(i).getLocation(), dst);
if (hValue < tmpH) {
break;
}
}
openList.add(i, node);
neighbors.add(ttg);
}
}
return neighbors;
}