def build_connection_matrix(routes):
all_stations = set()
for route in routes:
all_stations.update(route)
sorted_stations = sorted(all_stations)
station_index = {station: idx for idx, station in enumerate(sorted_stations)}
n = len(sorted_stations)
matrix = [[0] * n for _ in range(n)]
for route in routes:
for i in range(len(route)):
for j in range(i + 1, len(route)):
start = route[i]
end = route[j]
row = station_index[start]
col = station_index[end]
matrix[row][col] += 1
return matrix, sorted_stations
if __name__ == "__main__":
routes = [
['A', 'B', 'C'],
['B', 'A', 'D']
]
matrix, stations = build_connection_matrix(routes)
print("站点顺序:", stations)
print("连接矩阵:")
for row in matrix:
print(row)
