2024.3.29(周五)进度

讨论结对作业1中最短路径的问题,需要再建一个表,然后将两个表链接使用迪杰斯特拉算法

package com.example.beijingsubway;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

public class FptActivity extends AppCompatActivity {

// 储存地铁站信息的哈希表

// 添加地铁站信息到哈希表中
private static Map<String, List<String>> subwayLines = new HashMap<>();
private static List<String> allStations = new ArrayList<>();

// 添加地铁站信息到哈希表中
private static void addStation(String station, String[] neighbors) {
allStations.add(station); // 添加站点到所有站点列表
List<String> neighborList = Arrays.asList(neighbors);
subwayLines.put(station, neighborList); // 添加站点及其相邻站点到地铁线路中

// 添加反向连接
for (String neighbor : neighbors) {
if (!subwayLines.containsKey(neighbor)) {
subwayLines.put(neighbor, new ArrayList<>());
}
subwayLines.get(neighbor).add(station);
}
}


// 初始化地铁站信息
private void initializeStationsFromTxt() {
InputStream inputStream = getResources().openRawResource(R.raw.bgstations);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(" ");
String station = parts[0];
String[] neighbors = Arrays.copyOfRange(parts, 1, parts.length);
addStation(station, neighbors);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

// 使用Dijkstra算法计算最短路径
public static List<String> findShortestPath(String start, String end) {
Map<String, Integer> distance = new HashMap<>();
Map<String, String> predecessor = new HashMap<>();
PriorityQueue<String> queue = new PriorityQueue<>(Comparator.comparingInt(distance::get));

// 初始化距离和前驱节点信息
for (String station : allStations) {
distance.put(station, Integer.MAX_VALUE);
predecessor.put(station, null);
}

distance.put(start, 0);
queue.add(start);

// Dijkstra算法主循环
while (!queue.isEmpty()) {
String current = queue.poll();
if (current.equals(end)) {
break;
}

List<String> neighbors = subwayLines.get(current);
if (neighbors != null) {
for (String neighbor : neighbors) {
int newDist = distance.get(current) + 1; // 假设每两个站之间的距离为1
if (newDist < distance.getOrDefault(neighbor, Integer.MAX_VALUE)) {
distance.put(neighbor, newDist);
predecessor.put(neighbor, current);
queue.add(neighbor);
}
}
}
}

// 构建最短路径
List<String> shortestPath = new ArrayList<>();
String current = end;
while (current != null) {
shortestPath.add(current);
current = predecessor.get(current);
}
Collections.reverse(shortestPath);

// 检查是否找到路径
if (shortestPath.isEmpty() || !shortestPath.get(0).equals(start)) {
return Collections.emptyList(); // 如果未找到路径,则返回空列表
}

return shortestPath;
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fpt);

EditText startEditText = findViewById(R.id.s_name);
EditText endEditText = findViewById(R.id.e_name);
Button findButton = findViewById(R.id.fpt_find);
TextView resultTextView = findViewById(R.id.fpt_result);

// 设置查询按钮点击事件
findButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String start = startEditText.getText().toString();
String end = endEditText.getText().toString();

// 检查输入是否为空
if (TextUtils.isEmpty(start) || TextUtils.isEmpty(end)) {
Toast.makeText(FptActivity.this, "请输入起点站和终点站", Toast.LENGTH_SHORT).show();
return;
}

// 调用计算最短路径的方法
List<String> shortestPath = findShortestPath(start, end);

// 检查是否找到路径
if (shortestPath.isEmpty()) {
Toast.makeText(FptActivity.this, "未找到路径,请检查输入的站点", Toast.LENGTH_SHORT).show();
return;
}

// 将结果显示在TextView中
StringBuilder result = new StringBuilder();
result.append("从 ").append(start).append(" 到 ").append(end).append(" 的最短路径为:\n");

// 添加换乘信息
for (int i = 0; i < shortestPath.size() - 1; i++) {
String currentStation = shortestPath.get(i);
String nextStation = shortestPath.get(i + 1);
result.append(currentStation).append(" -> ");
if (!subwayLines.get(currentStation).equals(subwayLines.get(nextStation))) {
result.append("换乘:").append(nextStation).append("\n");
}
}
// 添加最后一个站点
result.append(shortestPath.get(shortestPath.size() - 1));

resultTextView.setText(result.toString());
}
});


// 初始化地铁站信息
initializeStationsFromTxt();
}
}
posted @ 2024-04-08 21:32  记得关月亮  阅读(2)  评论(0编辑  收藏  举报