package com.example.underground.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
/**
* <p>
*
* </p>
*
* @author xu
* @since 2024-04-04
*/
@Getter
@Setter
@Accessors(chain = true)
@TableName("line")
@ApiModel(value = "Line对象", description = "")
public class Line extends Model<Line> {
@TableId(value = "LineNumber", type = IdType.AUTO)
private Integer lineNumber;
@TableField("LineName")
private String lineName;
@Override
public Serializable pkVal() {
return this.lineNumber;
}
}
package com.example.underground.entity;
import lombok.Data;
/**
* 功能:
* 作者:
* 日期: 2024/4/9 15:49
*/
@Data
public class PreandNextstation {
private String lineName;
private String preStation;
private String thisStation;
private String nextStation;
}
package com.example.underground.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
/**
* <p>
*
* </p>
*
* @author xu
* @since 2024-04-04
*/
@Getter
@Setter
@Accessors(chain = true)
@TableName("station")
@ApiModel(value = "Station对象", description = "")
public class Station extends Model<Station> {
@TableId(value = "StationID", type = IdType.AUTO)
private Integer stationID;
@TableField("StationName")
private String stationName;
@TableField("LineNumber")
private Integer lineNumber;
@TableField("IsExchange")
private Integer isExchange;
@Override
public Serializable pkVal() {
return this.stationID;
}
}
package com.example.underground.entity;
import java.util.*;
public class Subway {
public List<String> getStationsBetween(String startStation, String endStation, List<List<String>> allSubwayStations) {
List<String> stationsBetween = new ArrayList<>();
Map<String, List<String>> graph = buildGraph(allSubwayStations);
// 检查起始站和终点站是否存在于地铁网络中
if (!graph.containsKey(startStation) || !graph.containsKey(endStation)) {
System.out.println("起始站或终点站不在地铁网络中");
return stationsBetween;
}
// 使用BFS搜索起始站到终点站之间的所有站点
Queue<String> queue = new LinkedList<>();
Set<String> visited = new HashSet<>();
Map<String, String> parentMap = new HashMap<>();
queue.offer(startStation);
visited.add(startStation);
while (!queue.isEmpty()) {
String currentStation = queue.poll();
if (currentStation.equals(endStation)) {
// 找到终点站,构建站点路径并返回
String station = endStation;
while (!station.equals(startStation)) {
stationsBetween.add(station);
station = parentMap.get(station);
}
stationsBetween.add(startStation);
Collections.reverse(stationsBetween);
return stationsBetween;
}
List<String> neighbors = graph.get(currentStation);
if (neighbors != null) {
for (String neighbor : neighbors) {
if (!visited.contains(neighbor)) {
queue.offer(neighbor);
visited.add(neighbor);
parentMap.put(neighbor, currentStation);
}
}
}
}
// 如果没有找到路径,返回空列表
System.out.println("无法找到起始站到终点站之间的路径");
return stationsBetween;
}
// 构建地铁站点图
private Map<String, List<String>> buildGraph(List<List<String>> allSubwayStations) {
Map<String, List<String>> graph = new HashMap<>();
for (List<String> lineStations : allSubwayStations) {
for (int i = 0; i < lineStations.size(); i++) {
String station = lineStations.get(i);
if (!graph.containsKey(station)) {
graph.put(station, new ArrayList<>());
}
if (i > 0) {
String prevStation = lineStations.get(i - 1);
graph.get(station).add(prevStation);
graph.get(prevStation).add(station);
}
}
}
return graph;
}
}