通过递归查找所有可能的路由

时间:2015-03-02 10:14:17

标签: java recursion arraylist

所以我一直在努力解决这个问题,似乎无法解决这个问题。我有一个包含startCity和goalCity的“道路”列表。通过递归,我必须找到所有可能的路线。我目前的代码只找到最长的路线。 我该怎么做才能解决这个问题?

道路:

Colorado Springs,Denver
Denver,Loveland
Loveland,Fort Collins
Loveland,Greeley
Greeley,Fort Collins
Fort Collins,Cheyenne
Fort Collins,Laramie
Laramie,Cheyenne

当前输出:

    All paths from Colorado Springs to Fort Collins are:
 1. [Colorado Springs, Denver, Loveland, Fort Collins]
 2. [Colorado Springs, Denver, Loveland, Greeley, Fort Collins]
 3. [Colorado Springs, Denver, Loveland, Greeley, Fort Collins, Loveland, Greeley, Fort Collins]

编辑更新的代码: 现在我得到额外的输出......

    public ArrayList<String> findPath2(String startCity, String goalCity, String oStartCity, ArrayList<String> route){

    //see if goal city possible
    if(tester(goalCity) != true){
        return null;
    }

    ///Base Case////
    if(startCity.equals(goalCity)){
        route.add(goalCity);
        String derp = route.toString();
        paths.add(derp);
        System.out.println(derp);
        //return route;
    }else{
        for (int i = 0; i < theRoads.size(); i ++){
            if(theRoads.get(i).isFrom(startCity)){


                route.add(startCity);
                findPath2(theRoads.get(i).endsAt(), goalCity, oStartCity, route);

                //System.out.println(route);
                //course = startCity + " -> " + course;

                for (int l = i+1; l < theRoads.size(); l ++){
                    if(theRoads.get(l).isFrom(startCity) && !(theRoads.get(l).startsAt().equals(goalCity))){
                        System.out.println("SPLIT");

                        route.remove(goalCity);
                        findPath2(theRoads.get(l).endsAt(), goalCity, oStartCity, route);
                        System.out.println("SPLIT RETURNING");

                    }
                }

                //return route;
            }
        }
    }
    return route;
}

如果有人有兴趣,我的所有代码也是: http://pastebin.com/3yeBU2fn

2ND编辑:@Vandale

仍然无法让它工作,但这样的事情?

public ArrayList<String> findPath2(String startCity, String goalCity, ArrayList<String> route){

        ArrayList<String> course = new ArrayList<>();

        if(startCity.equals(goalCity)){
            course.add(startCity);
        }else{
            route.add(startCity);
            for (int i = 0; i < theRoads.size(); i ++){
                if(theRoads.get(i).isFrom(startCity)){

                    for (int l = 0; l < route.size(); l ++){
                        //check if city has already been visited && if its possible to get to goal city (not sure if this works)
                        if(!(route.get(l).equals(startCity)) && (findPath2(theRoads.get(l).endsAt(), goalCity, route).equals(goalCity))){

                            course.add(startCity + "->" + findPath2(theRoads.get(l).endsAt(), goalCity, route));

                        }
                    }
                }
            }
            route.remove(startCity);
        }
        System.out.println(course);
        return course;
    }

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

oStartCity实际上从未在您的方法体中使用,因此您可以摆脱它。

实现目的的一种方法如下

Create empty list of paths
if at target city
    add current city to paths
else
    add current city to list of visited cities
    for each city connected to this
        if not already visited city and city has path to the target city
            add current city + each one of connected cities paths to paths
    remove current city from list of visited cities
return paths

为了跟踪你已访问过的城市,你应该使用类似HashSet的东西,它应作为函数的参数传入。

有一点需要注意的是,如果目的地城市没有路径,那么该函数将返回一个空列表,您可以使用该列表检查是否有路径。如果您使用此功能,则不应该对测试人员进行调用。

答案 1 :(得分:0)

所以我想通了,希望这有助于将来的某个人!

public void findAllPaths(String startCity, String goalCity){
        clearPaths();
        findPathHelps(startCity, goalCity, "");
    }

    private void findPathHelps(String startCity, String goalCity, String path){
        //base
        if(startCity.equals(goalCity))
            paths.add(path + goalCity);
        //cycle through
        for(Road road : this.roads)
            if(road.isFrom(startCity))
                //recursion and stuff
            findPathHelps(road.endsAt(), goalCity, path+startCity+"->");
    }