
import java.util.*;
public class CampusSearch {
String links[][] = {
{"office", "debuggingRoom"},
{"office", "CHI343"},
{"CHI343", "informaticsSchoolOffice"},
{"informaticsSchoolOffice", "ITS"},
{"ITS", "PEV11A6"},
{"PEV11A6", "PEV12A11"},
{"PEV11A6", "library"},
{"CHI343", "PEV11A6"},
{"PEV12A11", "library"},
{"library", "bridgeTeaBar"},
{"library", "meetingHouse"},
{"meetingHouse", "bridgeTeaBar"},
{"bridgeTeaBar", "debuggingRoom"}};
String goal = "meetingHouse";
ArrayList<String> getSuccessors(String location) {
ArrayList<String> successors = new ArrayList<String>();
for (int i = 0; i < links.length; i++) {
if (links[i][0].equals(location)) {
successors.add(links[i][1]); }
if (links[i][1].equals(location)) {
successors.add(links[i][0]); }
}
return(successors);
}
void run() {
ArrayList<ArrayList<String>> open = new ArrayList<ArrayList<String>>();
ArrayList<String> path = new ArrayList<String>();
path.add("office");
open.add(path);
while (open.size() > 0) {
path = open.remove(0);
String parent = path.get(path.size()-1);
if (parent.equals(goal)) {
System.out.println("ROUTE: " + path); }
ArrayList<String> successors = getSuccessors(parent);
for (int i = 0; i < successors.size(); i++) {
String child = successors.get(i);
if (!path.contains(child)) {
ArrayList<String> newPath = new ArrayList<String>(path);
newPath.add(child);
open.add(newPath); }
}
}
}
public static void main(String args[]) { // do the search
new CampusSearch().run();
}
}
javac CampusSearch.java java CampusSearch
You could also explicitly call the `main' method; i.e., right-click on the CampusSearch box, choose `void main(args)'. Then press `Ok'.
ROUTE: [office, debuggingRoom, bridgeTeaBar, meetingHouse] ROUTE: [office, debuggingRoom, bridgeTeaBar, library, meetingHouse] ROUTE: [office, CHI343, PEV11A6, library, meetingHouse] ROUTE: [office, CHI343, PEV11A6, PEV12A11, library, meetingHouse] ROUTE: [office, CHI343, PEV11A6, library, bridgeTeaBar, meetingHouse] ROUTE: [office, CHI343, informaticsSchoolOffice, ITS, PEV11A6, library, meetingHouse] ROUTE: [office, CHI343, PEV11A6, PEV12A11, library, bridgeTeaBar, meetingHouse] ROUTE: [office, CHI343, informaticsSchoolOffice, ITS, PEV11A6, PEV12A11, library, meetingHouse] ROUTE: [office, CHI343, informaticsSchoolOffice, ITS, PEV11A6, library, bridgeTeaBar, meetingHouse] ROUTE: [office, CHI343, informaticsSchoolOffice, ITS, PEV11A6, PEV12A11, library, bridgeTeaBar, meetingHouse]