个人项目———北京地铁最短路径
一、程序
类:分别有Station、Path、Map、Subwany 四个类
- class Station :地铁站点类,包含地铁站点名称、地铁站点编号、所属地铁线路
- class Path:路径类,包含路径距离、路径中的最后一站以及路径中的所有站点
class Map(处理读入的文件以及计算最短路径)
public void loadSubwayFile(String subway_file)
读取文件中的每一行,即每一条地铁线路暂时存储起来。
`public void loadSubwayFile(String subway_file) {
BufferedReader reader=null;
File subway=new File(subway_file);
try {
reader=new BufferedReader(new FileReader(subway));
String t=null;
while((t=reader.readLine())!=null) {
subway_allstation.add(t);
}
}catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } }` `public void parseSubway()`处理读入的文件,将内容分割,分别得到地铁线路的id,地铁线路名称,以及每个地铁站点,并且给每个地铁站点赋上地铁站点id=线路id*1000+该站点所在该线路的第几个站点。 对应存储在map中 : HashMap<String,Station> name_to_station 站点名称对应地铁站 HashMap<Integer,Station> id_to_station 站点id对应地铁站 HashMap<String,List<Station>> line_to_station 地铁线名称对应该线路的所有地铁站点集合 HashMap<Integer,String> lineid_to_linename 地铁线路id对应地铁线路名称 HashMap<Integer,Set<Station>> Lineid_to_transferstation 地铁线路id对应该线路所有的中转站 HashMap<String,Integer> transferstationname_to_distanc 中转站的名称对应路径的距离 `public void parseSubway(){ for(int i=0;i<subway_allstation.size();i++) { String line=subway_allstation.get(i); String[] arrIdAndStations=line.split(","); if(arrIdAndStations.length!=2) { System.out.println("地铁数据错误" + line); return; } int lineid=i+1; if(lineid==-1) { System.out.println("地铁线路号数据错误"+lineid); } String[]arrLineAndStations =arrIdAndStations[1].split(":"); lineid_to_linename.put(lineid, arrLineAndStations[0]); String[]arrStationNames=arrLineAndStations[1].split(" "); for(int i1=0;i1<arrStationNames.length;i1++) { String Stationname=arrStationNames[i1]; Station station=new Station(); station.station_name=Stationname; int stationid=lineid*1000+i1+1; station.station_id.add(stationid); station.line_name=arrLineAndStations[0]; station.line_id=lineid; if(!line_to_station.containsKey(station.line_name)) { List <Station> stations=new ArrayList<Station>(); stations.add(station); line_to_station.put(station.line_name, stations); } else { List <Station> stations=line_to_station.get(station.line_name); stations.add(station); } id_to_station.put(stationid, station); //注意中转站 要先判断再存 if(!name_to_station.containsKey(arrStationNames[i1])) { name_to_station.put(Stationname, station); } else { Station stationExistedTransferStation =name_to_station.get(arrStationNames[i1]); stationExistedTransferStation.station_id.add(stationid); updateTransferStation(stationExistedTransferStation); } }` `Path shortedPath(String startname,String endname) `计算最短路径 如果起点终点都在同一地铁线上,那么只需遍历一次,返回的Path中存的是起点站和终点站。若要换成一次,则需要遍历两遍,返回的Path中存的是起点站,中转站和终点站。以此类推。如果需要中转则遍历同意线路上的所有中转站,找到最短路径。 `for(String stationname:transferstationname_to_distance.keySet()) { Station stationtransfer=name_to_station.get(stationname); int currenttotransferdis =getStationDistance(pathcurrent.path_laststation,stationtransfer); int finaldis=pathcurrent.distance+currenttotransferdis; if(finaldis>=transferstationname_to_distance.get(stationname)) continue; transferstationname_to_distance.put(stationname, finaldis); if(finaldis<1000&&finaldis<shorted.distance) { Path pathnew=new Path(); pathnew.distance=finaldis; pathnew.path_laststation=stationtransfer; pathnew.path_stationlist=new Vector<>(pathcurrent.path_stationlist); pathnew.path_stationlist.addElement(stationtransfer); stackAllPaths.push(pathnew); }` 
class Subway 进行输出。
printStationsOfLine(String Line, String strOutFile)
返回地铁线路的所有站点名称printPath(Path path)
返回路径上的所有站点名称printFile(String strContent, String strOutFile)
将String输出到文件void printStationsOfLine(String Line, String strOutFile) { StringBuffer strRst = new StringBuffer(); strRst.append(Line+"\r\n"); if (line_to_station.containsKey(Line)) { List<Station> stations=new ArrayList<Station>(); stations=line_to_station.get(Line); for(int i=0;i<stations.size();i++) { Station s=stations.get(i); strRst.append(s.station_name+ "\r\n"); } } printFile(strRst.toString(), strOutFile); }
测试结果
1.输出所有站点 java Subway -a 一号线 -map subway.txt -o station.txt
一号线 苹果园 古城 八角游乐园 八宝山 玉泉路 五棵松 万寿路 公主坟 军事博物馆 木樨地 南礼士路 复兴门 西单 天安门西 天安门东 王府井 东单 建国门 永安里 国贸 大望路 四惠 四惠东
2. 错误输入 java Subway -a 0号线 -map subway.txt -o station.txt
得到结果:0号线 地铁线不存在3.最短路径 无换乘 java Subway -b 金安桥 西黄村 -map subway.txt -o routine.txt
得到结果:六号线 金安桥 苹果园 杨庄 西黄村4.最短路径 换乘1次 java Subway -b 安定门 北新桥 -map subway.txt -o routine.txt
得到结果:二号线 安定门 雍和宫 五号线 北新桥5.最短路径 换乘两次 java Subway -b 东直门 安贞门 -map subway.txt -o routine.txt
得到结果:二号线 和平门 宣武门 四号线 菜市口 七号线 虎坊桥6.最短路径 错误输入 java Subway -b 和平门 虎坊 -map subway.txt -o routine.txt
得到结果:查询站点输入有误个人小结
这次个人作业学习了不少别人的代码,因为在写的过程中遇到不少问题,导致和一开始的想法有很大的不同,看了别人写的代码发现了很多需要学习的代码,有很多的知识点还不知道,有些知道了也不会使用,还需要大量的练习提高能力。