第一次结对作业
package com.Dao;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import com.Bean.Bean;
public class Subway {
private List<Station> outList = new ArrayList<Station>();//记录已经分析过的站点
//计算从s1站到s2站的最短经过路径
public List<Bean> calculate(Station s1,Station s2){
List<Bean> list = new ArrayList<Bean>();//用于返回路径信息
if(outList.size() == DataBuilder.totalStaion){
System.out.println("找到目标站点:"+s2.getName()+",共经过"+(s1.getAllPassedStations(s2).size()-1)+"站");
for(Station station : s1.getAllPassedStations(s2)){
System.out.print(station.getName()+"->");
Bean bean = new Bean();
bean.setStationname(station.getName());
list.add(bean);
}
return list;
}
if(!outList.contains(s1)){
outList.add(s1);
}
//如果起点站的OrderSetMap为空,则第一次用起点站的前后站点初始化之
if(s1.getOrderSetMap().isEmpty()){
List<Station> Linkedstations = getAllLinkedStations(s1);
for(Station s : Linkedstations){
s1.getAllPassedStations(s).add(s);
}
}
Station parent = getShortestPath(s1);//获取距离起点站s1最近的一个站(有多个的话,随意取一个)
if(parent == s2){
System.out.println("找到目标站点:"+s2+",共经过"+(s1.getAllPassedStations(s2).size()-1)+"站");
for(Station station : s1.getAllPassedStations(s2)){
System.out.print(station.getName()+"->");
Bean bean = new Bean();
bean.setStationname(station.getName());
list.add(bean);
}
return list;
}
for(Station child : getAllLinkedStations(parent)){
if(outList.contains(child)){
continue;
}
int shortestPath = (s1.getAllPassedStations(parent).size()-1) + 1;//前面这个1表示计算路径需要去除自身站点,后面这个1表示增加了1站距离
if(s1.getAllPassedStations(child).contains(child)){
//如果s1已经计算过到此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);
return calculate(s1,s2);
}
//取参数station到各个站的最短距离,相隔1站,距离为1,依次类推
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);//参数station到s所经过的所有站点的集合
if(set.size() < minPatn){
minPatn = set.size();
rets = s;
}
}
return rets;
}
//获取参数station直接相连的所有站,包括交叉线上面的站
private List<Station> getAllLinkedStations(Station station){
List<Station> linkedStaions = new ArrayList<Station>();
for(List<Station> line : DataBuilder.lineSet){
if(line.contains(station)){//如果某一条线包含了此站,注意由于重写了hashcode方法,只有name相同,即认为是同一个对象
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;
}
}
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import com.Bean.Bean;
public class Subway {
private List<Station> outList = new ArrayList<Station>();//记录已经分析过的站点
//计算从s1站到s2站的最短经过路径
public List<Bean> calculate(Station s1,Station s2){
List<Bean> list = new ArrayList<Bean>();//用于返回路径信息
if(outList.size() == DataBuilder.totalStaion){
System.out.println("找到目标站点:"+s2.getName()+",共经过"+(s1.getAllPassedStations(s2).size()-1)+"站");
for(Station station : s1.getAllPassedStations(s2)){
System.out.print(station.getName()+"->");
Bean bean = new Bean();
bean.setStationname(station.getName());
list.add(bean);
}
return list;
}
if(!outList.contains(s1)){
outList.add(s1);
}
//如果起点站的OrderSetMap为空,则第一次用起点站的前后站点初始化之
if(s1.getOrderSetMap().isEmpty()){
List<Station> Linkedstations = getAllLinkedStations(s1);
for(Station s : Linkedstations){
s1.getAllPassedStations(s).add(s);
}
}
Station parent = getShortestPath(s1);//获取距离起点站s1最近的一个站(有多个的话,随意取一个)
if(parent == s2){
System.out.println("找到目标站点:"+s2+",共经过"+(s1.getAllPassedStations(s2).size()-1)+"站");
for(Station station : s1.getAllPassedStations(s2)){
System.out.print(station.getName()+"->");
Bean bean = new Bean();
bean.setStationname(station.getName());
list.add(bean);
}
return list;
}
for(Station child : getAllLinkedStations(parent)){
if(outList.contains(child)){
continue;
}
int shortestPath = (s1.getAllPassedStations(parent).size()-1) + 1;//前面这个1表示计算路径需要去除自身站点,后面这个1表示增加了1站距离
if(s1.getAllPassedStations(child).contains(child)){
//如果s1已经计算过到此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);
return calculate(s1,s2);
}
//取参数station到各个站的最短距离,相隔1站,距离为1,依次类推
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);//参数station到s所经过的所有站点的集合
if(set.size() < minPatn){
minPatn = set.size();
rets = s;
}
}
return rets;
}
//获取参数station直接相连的所有站,包括交叉线上面的站
private List<Station> getAllLinkedStations(Station station){
List<Station> linkedStaions = new ArrayList<Station>();
for(List<Station> line : DataBuilder.lineSet){
if(line.contains(station)){//如果某一条线包含了此站,注意由于重写了hashcode方法,只有name相同,即认为是同一个对象
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;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现