项目介绍
根据提供的地铁线路图,计算指定站点间的最短距离。输入地铁文本信息,输出指定地铁线路的所有站点。
文本存储格式
地铁线路图示
主要功能
将输入的站点信息进行储存处理,根据用户的输入信息进行处理,将最后处理完成的信息输出。
实现语言
Java
主要实现算法
BFS(广度优先遍历)算法
代码展示
BeanLine类
import java.util.ArrayList;
import java.util.List;
public class BeanLine {
private String lineName;//线路名
private List lineStation=new ArrayList(); //线路站点
public BeanLine() {
super();
// TODO Auto-generated constructor stub
}
public BeanLine(String lineName, List lineStation) {
super();
this.lineName = lineName;
this.lineStation = lineStation;
}
public String getLineName() {
return lineName;
}
public void setLineName(String lineName) {
this.lineName = lineName;
}
public List getLineStation() {
return lineStation;
}
public void setLineStation(List lineStation) {
this.lineStation = lineStation;
}
public void addLineStation(String station) {
lineStation.add(station);
}
}
BeanStation类
import java.util.ArrayList;
public class BeanStation {
private String stationName;
private ArrayList stationLine = new ArrayList(); //存放站点所在的线路
private ArrayList nearbystation = new ArrayList(); //存放相邻的站点
private int dist;
private BeanStation front; //存放最短路径的前驱节点
public int getDist() {
return dist;
}
public void setDist(int dist) {
this.dist = dist;
}
public BeanStation getFront() {
return front;
}
public void setFront(BeanStation front) {
this.front = front;
}
public String getStationName() {
return stationName;
}
public void setStationName(String stationName) {
this.stationName = stationName;
}
public ArrayList getStationLine() {
return stationLine;
}
public void setStationLine(ArrayList stationLine) {
this.stationLine = stationLine;
}
public void addStationLine(String station) {
stationLine.add(station);
}
public ArrayList getLinkstation() {
return nearbystation;
}
public void setLinkstation(ArrayList linkstation) {
nearbystation = linkstation;
}
public void addLineStation(BeanStation station) {
nearbystation.add(station);
}
}
主函数mainClass
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class mainClass {
static final int MaxValue=65535;
static final int MaxNum=400;
public static void main(String[] args) {
ArrayList stations= new ArrayList<>();
ArrayList lines = new ArrayList();
try{
InputStreamReader isr = new InputStreamReader(new FileInputStream("C:\\Users\\17309\\Desktop\\软件工程\\地铁线路最短路径\\地铁线路信息.txt"), "utf-8");
BufferedReader br = new BufferedReader(isr);
String s = null;
int j;
BeanStation last_station= new BeanStation(); //连接相邻的两个车站
while ((s = br.readLine()) != null) {
String tokens[]=s.split(" ");
BeanLine line = new BeanLine();
line.setLineName(tokens[0]);
System.out.println(line.getLineName() + ":");//输出线路名
for(int i=1;i
line.addLineStation(tokens[i]);
for(j=0;j
if(stations.get(j).getStationName().equals(tokens[i]))
{
stations.get(j).addStationLine(tokens[0]);
if(i!=1) {
stations.get(j).addLineStation(last_station);
last_station.addLineStation(stations.get(j));
}
last_station=stations.get(j);
break;
}
}
if(j==stations.size()) {
BeanStation station = new BeanStation();
station.addStationLine(tokens[0]);
station.setStationName(tokens[i]);
station.setDist(MaxValue);
if(i!=1) {
station.addLineStation(last_station);
last_station.addLineStation(station);
}
stations.add(station);
last_station=station;
}
}
lines.add(line);
System.out.println(line.getLineStation());//输出一条线路的全部站点
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("#################################################################################################");
Scanner input=new Scanner(System.in);
System.out.println("请输入起始站:");
String begin_station_name=input.nextLine();
System.out.println("请输入终点站:");
String end_station_name=input.next();
System.out.println("#################################################################################################");
System.out.println("输出路线信息:");
if(begin_station_name.equals(end_station_name)) {
System.out.println("起始站与终点站相同");
}
else {
BeanStation end_station = new BeanStation();
BeanStation begin_station = new BeanStation();
for(BeanStation station:stations) {
if(station.getStationName().equals(begin_station_name)) {
begin_station=station;
shortestPath(station);
}
if(station.getStationName().equals(end_station_name))
end_station=station;
}
ArrayList shortestPath=new ArrayList();
showPath(end_station,shortestPath);
String changeLine=getSameLine(shortestPath.get(0),shortestPath.get(1));
for(int i=0;i
if(i>=2) {
if(!getSameLine(shortestPath.get(i),shortestPath.get(i-1)).equals(changeLine)) {
changeLine=getSameLine(shortestPath.get(i),shortestPath.get(i-1));
System.out.println("------->换乘"+changeLine);
}
}
System.out.println(shortestPath.get(i).getStationName());
}
}
}
public static void shortestPath(BeanStation station){
Queue queue = new LinkedList<>();
station.setDist(0);
queue.offer(station);
while(!queue.isEmpty()) {
BeanStation vertex=queue.poll();
for(BeanStation linkstation:vertex.getLinkstation()) {
if(linkstation.getDist()==MaxValue) {
linkstation.setDist(vertex.getDist()+1);
queue.offer(linkstation);
linkstation.setFront(vertex);
}
}
}
}
public static void showPath(BeanStation end_station,ArrayList result) {
if(end_station.getFront()!=null)
showPath(end_station.getFront(),result);
result.add(end_station);
}
private static String getSameLine(BeanStation station1,BeanStation station2) {
for(String line1:station1.getStationLine()) {
for(String line2:station2.getStationLine()) {
if(line1.equals(line2))
return line1;
}
}
return null;
}
}
输出样例
输入两个站点
输入相同的站点
实验总结
本次实验我完成的有艰难,上网找了一些资料,本来想用Dijkstra算法,但最后发现BFS算法更简便,但是只能求出一条最短路径,希望我在下一次的实验中可以有所进步。