找出旅行路线 | 树的应用

给出几张车票,找出旅行路线。

关键:建立模型,找出特点。

//import javafx.util.Pair;

import java.util.*;

public class Travel {
    public static void main(String[] args) {
        HashMap Tickets = new HashMap<>();
        Tickets.put("南京","上海");
        Tickets.put("济南","南京");
        Tickets.put("广州","成都");
        Tickets.put("上海","广州");
        TravelPath(Tickets);
    }

/*    static void TravelPath2(Map Tickets){
        Queue> stk=null;
        for (Map.Entry Ticket : Tickets.entrySet()) {
            stk.add((Pair) Ticket);
        }
    }*/

    static void TravelPath(Map Tickets){
        String source = null;
        ArrayList values = new ArrayList<>();
        for (String value : Tickets.values()) {
            values.add(value);
        }
        for (String key : Tickets.keySet()) {
            if(!values.contains(key)){
                source=key;
            }
        }
        for(String place=source;Tickets.keySet().contains(place);place=Tickets.get(place)){
            System.out.println(place+"->"+Tickets.get(place));
        }
    }
}

class Node{
    Node pre;
    String city;
    Node next;
}

 

你可能感兴趣的:(#,JavaSE,————算法训练————)