Subway travel route planning
github: 31701022
Review Questions
implement a program to calculate the shortest subway correctly handle the command-line path
First, according to the design needs of the document read
the first line is the line, the second line of each line is represented by the line between the corresponding sites, and these sites and sites lines, partition
1号线、2号线、4号线、5号线...
苹果园、古城、八角游乐园、八宝山...
西直门、积水潭、鼓楼大街、安定门...
安河桥北、北宫门、西苑、圆明园、北京大学东门...
宋家庄、刘家窑、蒲黄榆、天坛东门、磁器口...
......
......
Create a storage site for station class object names and storage site corresponding to the site and the adjacent lines are omitted get / set methods
public class station {
private String name;
public station prev;
public station next;
private Map<station,LinkedHashSet<station>> orderSetMap = new HashMap<station,LinkedHashSet<station>>();
private String line;
public station (String name){
this.name = name;
}
GetAllPassedStations obtained using the method of all paths starting from the collection station to the terminal
public LinkedHashSet<station> getAllPassedStations(station station) {
if(orderSetMap.get(station) == null){
LinkedHashSet<station> set = new LinkedHashSet<station>();
set.add(this);
orderSetMap.put(station, set);
}
return orderSetMap.get(station);
}
Then read the file, each line array corresponding to a station
private static List<station> line1 = new ArrayList<station>();//1号线
private static List<station> line2 = new ArrayList<station>();//2号线
...
After the data has been read using the Dijkstra algorithm computes the shortest path, each time a complete calculation of the closest site, and then analyze the next closest site, until the end.
The core algorithm
private station getShortestPath(station station){ //station站到各个站的最短距离,每相邻两个站距离为1
int minPatn = Integer.MAX_VALUE;
station rets = null;
for(station s :station.getOrderSetMap().keySet()){
if(outList.contains(s)){
continue;
}
LinkedHashSet<station> set = station.getAllPassedStations(s);//参数station到s站所经过的所有站点的集合
if(set.size() < minPatn){
minPatn = set.size();
rets = s;
}
}
return rets;
}
After the string array output using line information storage site and calculate the shortest path output
public List <String > linename= new ArrayList<>();//存储输出结果
-Map demand command line parameter read using a readable file corresponding to
Java subway -map subway.txt
args[arg].equals("-map")
-A specified using subway station and using the corresponding line output -o
subway -a 2号线 -map subway.txt -o station.txt
args[i].equals("-a")
args[w].equals("-o")
args[i+1].equals("1号线")
...
-B input using the start and end uses and outputs -o
subway -b 安定门 万寿路 -map subway.txt -o routine.txt
args[i].equals("-b")
summary:
The experiment uses a LinkedHashSet collection, LinkedHashSet is a combination of a hash table and linked list, and is a doubly linked list, elements are not repeated and orderly, so used to find the site very convenient site, while using Dijkstra's algorithm can be Yang more convenient find the shortest path.
Of course, some problems encountered during the experiment:
First, in the read and write I / O on the issue met with BufferedWriter did not write content, BufferedWriter to buffer data through an array of characters, when the buffer is full or the user calls flush () function, it will data write buffer to the output stream. Then the algorithm forget a little more, and finally the use of the command line is encountered two problems
javac find symbol error since written three papers each call separately compiled a file so the compiler can not be successful, the reason is because there is no environment variable configured, you can compile all the classes at the same time.
java.lang.NoClassDefFoundError runtime can not be loaded into the class
because of the presence package so there is no way to call between classes in the main function, so I used package.main way to successfully run the main function
Through this experiment there was still a lot to improve, but also step on a lot of mine, I hope next long point memory.