标签:stream sem src 模块名 file 实现 adl final stp
本项目已上传Github:
主要模块介绍:
序号 | 模块名 | 功能 | 对应java类 |
1 | 主模块(subway) | 对输入参数的控制 | Subway.java |
2 | 文件输入输出模块 | txt文件的读写 | DataBuilder.java |
3 | 求最短路径(dij算法) | 实现两个类的相互连接 | Station.java |
public static List<String> getFileContent(String path) {
List<String> strList = new ArrayList<String>();
File file = new File(path);
InputStreamReader read = null;
BufferedReader reader = null;
try {
read = new InputStreamReader(new FileInputStream(file));
reader = new BufferedReader(read);
String line;
while ((line = reader.readLine()) != null) {
strList.add(line);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (read != null) {
try {
read.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return strList;
}
public class Subway {
private List<Station> outList = new ArrayList<Station>();//记录已经分析过的站点
private void search(Station string) {
// TODO Auto-generated method stub
if(outList.size() == DataBuilder.totalStaion){
String a = string.getLinename();
}
return;
}
public void calculate(Station s1,Station s2){
if(outList.size() == DataBuilder.totalStaion){
System.out.println("找到目标站点:"+s2.getName()+",共经过"+(s1.getAllPassedStations(s2).size()-1)+"站");
for(Station station : s1.getAllPassedStations(s2)){
System.out.println(station.getName()+" (" +station.getLinename()+")");
}
return;
}
if(!outList.contains(s1)){
outList.add(s1);
}
if(s1.getOrderSetMap().isEmpty()){
List<Station> Linkedstations = getAllLinkedStations(s1);
for(Station s : Linkedstations){
s1.getAllPassedStations(s).add(s);
}
}
Station parent = getShortestPath(s1);
if(parent == s2){
System.out.println("找到目标站点:"+s2+",共经过"+(s1.getAllPassedStations(s2).size()-1)+"站");
for(Station station : s1.getAllPassedStations(s2)){
System.out.print(station.getName()+" (" +station.getLinename()+")");
}
return;
}
for(Station child : getAllLinkedStations(parent)){
if(outList.contains(child)){
continue;
}
int shortestPath = (s1.getAllPassedStations(parent).size()-1) + 1;
if(s1.getAllPassedStations(child).contains(child)){
if((s1.getAllPassedStations(child).size()-1) > shortestPath){
//重置S1到周围各站的最小路径
s1.getAllPassedStations(child).clear();
s1.getAllPassedStations(child).addAll(s1.getAllPassedStations(parent));
s1.getAllPassedStations(child).add(child);
}
} else {
//如果s1还没有计算过到此child的经过距离
s1.getAllPassedStations(child).addAll(s1.getAllPassedStations(parent));
s1.getAllPassedStations(child).add(child);
}
}
outList.add(parent);
calculate(s1,s2);
}
private Station getShortestPath(Station station){
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);
if(set.size() < minPatn){
minPatn = set.size();
rets = s;
}
}
return rets;
}
private List<Station> getAllLinkedStations(Station station){
List<Station> linkedStaions = new ArrayList<Station>();
for(List<Station> line : DataBuilder.lineSet){
if(line.contains(station)){
Station s = line.get(line.indexOf(station));
if(s.prev != null){
linkedStaions.add(s.prev);
}
if(s.next != null){
linkedStaions.add(s.next);
}
}
}
return linkedStaions;
}
总结
标签:stream sem src 模块名 file 实现 adl final stp
原文地址:https://www.cnblogs.com/xiaoxiao54/p/11674718.html