Professional Documents
Culture Documents
Source Code Algoritma BFS
Source Code Algoritma BFS
NPM: 18312215
package com.hendro;
import java.awt.Point;
import java.util.LinkedList;
/*
{1, 1, 2, 1, 1},
{1, 1, -1, -1, -1},
{1, 1, -1, 1, -1},
{1, 1, -1, 1, -1},
{1, 1, 1, 1, 1}
*/
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 BFS() {
Point start = new Point(3, 4);
Point target = new Point(3, 0);
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{
for(Point nb: getNeighbors(cNode.getLocation())){
MapNode child = new MapNode (nb);
if(!openList.contains(child)&& !closedList.contains(child)){
openList.add(child);
cNode.addChild(child);
}
}
}
}
if(leaves != null) {
return getPath(leaves);
}
return null;
}