用于查找目标是否可访问的程序

时间:2018-01-22 15:16:13

标签: java

我在接受采访时被问到以下问题

根据航班列表和起点和目的地打印出所有可能用于从起点到达目的地的航班组合。

示例: 给出像

这样的航班列表
A -> B
A -> D
B -> C
B -> D
B -> H
D -> A
C -> E
E -> F
E -> G
G -> H

将起点指定为A,将端点指定为H,程序应输出

A - > B - > ħ

A - > B - > C - > E - > G - > ħ

我写的程序如下

package flight;

import java.util.List;

public class Flights {

    private String start;
    private List<String> destinations;

    public Flights(String start, List<String> destinations) {
        this.start = start;
        this.destinations = destinations;
    }

    public String getStart() {
        return start;
    }

    public void setStart(String start) {
        this.start = start;
    }

    public List<String> getDestinations() {
        return destinations;
    }

    public void setDestinations(List<String> destinations) {
        this.destinations = destinations;
    }

    public boolean containsDestination(String city){
        return getDestinations().contains(city);
    }
}

package flight;

import java.util.ArrayList;
import java.util.List;

public class Test {

    public static void main(String[] args) {
        List<String> fList = new ArrayList<>();
        fList.add("B");
        fList.add("D");
        Flights f1 = new Flights("A",fList);

        fList = new ArrayList<>();
        fList.add("C");
        fList.add("D");
        fList.add("H");
        Flights f2 = new Flights("B",fList);

        fList = new ArrayList<>();
        fList.add("A");
        Flights f3 = new Flights("D",fList);


        fList = new ArrayList<>();
        fList.add("E");
        Flights f4 = new Flights("C",fList);

        fList = new ArrayList<>();
        fList.add("F");
        fList.add("G");
        Flights f5 = new Flights("E",fList);

        fList = new ArrayList<>();
        fList.add("H");
        Flights f6 = new Flights("G",fList);

        List<Flights> flights = new ArrayList<>();
        flights.add(f1);
        flights.add(f2);
        flights.add(f3);
        flights.add(f4);
        flights.add(f5);
        flights.add(f6);

        String start = "A";
        String end = "H";

        getFlights(flights,start,end,end);

    }


    private static void getFlights(List<Flights> flights,String start,String destination,String flightResult){
        if (start.equals(destination)){
             flightResult = flightResult+"-"+start;
        }
        List<String> ls = new ArrayList<>();
        for (int i=0;i<flights.size();i++){
            Flights f = flights.get(i);
            if(f.containsDestination(destination)){
                ls.add(flightResult+"-"+f.getStart());
            }
        }
        for (int i=0;i<ls.size();i++){
            String as = ls.get(i);
            String[] s = as.split("-");
            String lastCity = s[s.length-1];
            if (!lastCity.equals(start)){
                getFlights(flights,start,lastCity,as);
            }

        }

        System.out.println(ls);
    }
}

程序正在输出

[H-B-A]
[H-G-E-C-B-A]
[H-G-E-C-B]
[H-G-E-C]
[H-G-E]
[H-B, H-G]

我只想要前两个有效的,我该怎么做? 还有一种优化方式吗?

1 个答案:

答案 0 :(得分:0)

您的代码存在问题

if (!lastCity.equals(start)){
     getFlights(flights,start,lastCity,as);
}

从递归调用返回后,它仍然在System.out.println(ls)下面,所以它打印了你在列表中保存的所有内容,你可以通过将结果存储在另一个列表中来克服这个问题,我检查它是否正常工作,这里是代码和我提到的代码中的更改与侧面注释,您只需要更改您的测试类

package flight;
import java.util.*;

public class Test {

    public static void main(String[] args) {
        List<String> fList = new ArrayList<>();
        fList.add("B");
        fList.add("D");
        Flights f1 = new Flights("A",fList);

        fList = new ArrayList<>();
        fList.add("C");
        fList.add("D");
        fList.add("H");
        Flights f2 = new Flights("B",fList);

        fList = new ArrayList<>();
        fList.add("A");
        Flights f3 = new Flights("D",fList);


        fList = new ArrayList<>();
        fList.add("E");
        Flights f4 = new Flights("C",fList);

        fList = new ArrayList<>();
        fList.add("F");
        fList.add("G");
        Flights f5 = new Flights("E",fList);

        fList = new ArrayList<>();
        fList.add("H");
        Flights f6 = new Flights("G",fList);

        List<Flights> flights = new ArrayList<>();
        flights.add(f1);
        flights.add(f2);
        flights.add(f3);
        flights.add(f4);
        flights.add(f5);
        flights.add(f6);

        String start = "A";
        String end = "H";

        getFlights(flights,start,end,end);

        for(int i=0;i<answerlist.size();i++){   // print here
            String s = answerlist.get(i);
            StringBuilder x = new StringBuilder(); // stringbuilder to reverse the result
            x.append(s);
            x = x.reverse();
            System.out.println(x);
        }
    }
    public static List<String> answerlist = new ArrayList<>();   //list to store result

    private static void getFlights(List<Flights> flights,String start,String destination,String flightResult){
        if (start.equals(destination)){
             flightResult = flightResult+"-"+start;
        }
        List<String> ls = new ArrayList<>();
        for (int i=0;i<flights.size();i++){
            Flights f = flights.get(i);
            if(f.containsDestination(destination)){
                ls.add(flightResult+"-"+f.getStart());
            }
        }
        for (int i=0;i<ls.size();i++){
            String as = ls.get(i);
            String[] s = as.split("-");
            String lastCity = s[s.length-1];
            if (!lastCity.equals(start)){
                getFlights(flights,start,lastCity,as);
            }
            else{
                answerlist.add(as);        //store in a newlist
            }       
        }
    }
}