用java代码完成对景点信息的管理和维护,设计我国海洋著名旅游景点的平面图及其存储结构,至少包括10个以上的场所,抽象成一个无向带权图(无向图),以图中顶点表示旅游景点各场所,边上的权值表示两景点之间的距离。能够每到一处景点就显示或语言播报景点信息。采用适合的路径导游算法,接收GUI部分传来的路径起始点与终止点,通过GUI返回源点到终止点的路径上各景点的核心信息与各条路线的距离及行进方向。通过IO流读取地图数据。
时间: 2024-03-11 20:50:34 浏览: 26
好的,这是一个比较复杂的问题,需要一定的时间和精力来回答。我会尽力给您提供一个基本的思路和代码示例。
首先,我们需要定义一个景点类,包括景点名称、景点介绍、坐标等属性。代码示例如下:
```java
public class Scene {
private String name; // 景点名称
private String introduction; // 景点介绍
private double x; // 横坐标
private double y; // 纵坐标
// 构造方法
public Scene(String name, String introduction, double x, double y) {
this.name = name;
this.introduction = introduction;
this.x = x;
this.y = y;
}
// getter和setter方法
// ...
}
```
接下来,我们需要定义一个图类,表示景点之间的联系。代码示例如下:
```java
public class Graph {
private int vertexNum; // 顶点数
private int edgeNum; // 边数
private Scene[] vertex; // 顶点数组
private int[][] edge; // 边数组
// 构造方法
public Graph(int vertexNum) {
this.vertexNum = vertexNum;
this.edgeNum = 0;
this.vertex = new Scene[vertexNum];
this.edge = new int[vertexNum][vertexNum];
for (int i = 0; i < vertexNum; i++) {
for (int j = 0; j < vertexNum; j++) {
this.edge[i][j] = -1; // 初始化为-1,表示两点之间没有边
}
}
}
// 添加边
public void addEdge(int from, int to, int weight) {
this.edge[from][to] = weight;
this.edge[to][from] = weight; // 无向图,需要添加双向边
this.edgeNum++;
}
// 获取邻接点
public List<Integer> getAdjList(int vertex) {
List<Integer> adjList = new ArrayList<>();
for (int i = 0; i < vertexNum; i++) {
if (edge[vertex][i] != -1) {
adjList.add(i);
}
}
return adjList;
}
// 获取边权
public int getWeight(int from, int to) {
return edge[from][to];
}
// getter和setter方法
// ...
}
```
然后,我们需要读取地图数据并构建图。数据可以保存在一个文本文件中,每行表示一个景点,格式为“名称 介绍 横坐标 纵坐标”。代码示例如下:
```java
public class Map {
private Graph graph; // 地图图表示
private List<Scene> sceneList; // 景点列表
// 从文件中读取地图数据
public void readMapData(String path) {
try {
BufferedReader reader = new BufferedReader(new FileReader(path));
String line;
int vertexNum = 0;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(" ");
String name = parts[0];
String introduction = parts[1];
double x = Double.parseDouble(parts[2]);
double y = Double.parseDouble(parts[3]);
Scene scene = new Scene(name, introduction, x, y);
sceneList.add(scene);
vertexNum++;
}
graph = new Graph(vertexNum);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 构建地图图表示
public void buildGraph() {
for (int i = 0; i < sceneList.size(); i++) {
for (int j = i + 1; j < sceneList.size(); j++) {
Scene scene1 = sceneList.get(i);
Scene scene2 = sceneList.get(j);
double distance = Math.sqrt(Math.pow(scene1.getX() - scene2.getX(), 2) + Math.pow(scene1.getY() - scene2.getY(), 2));
graph.addEdge(i, j, (int)distance); // 添加边
}
}
}
// 打印地图图表示
public void printGraph() {
for (int i = 0; i < sceneList.size(); i++) {
System.out.print(sceneList.get(i).getName() + ": ");
List<Integer> adjList = graph.getAdjList(i);
for (Integer adj : adjList) {
System.out.print(sceneList.get(adj).getName() + "(" + graph.getWeight(i, adj) + ") ");
}
System.out.println();
}
}
// getter和setter方法
// ...
}
```
最后,我们需要实现路径导游算法。这里选择Dijkstra算法。代码示例如下:
```java
public class Dijkstra {
private Graph graph; // 图
private int[] dist; // 源点到各顶点的最短距离
private boolean[] visited; // 是否已访问过
// 构造方法
public Dijkstra(Graph graph) {
this.graph = graph;
this.dist = new int[graph.getVertexNum()];
this.visited = new boolean[graph.getVertexNum()];
}
// Dijkstra算法
public void dijkstra(int start) {
Arrays.fill(dist, Integer.MAX_VALUE);
Arrays.fill(visited, false);
dist[start] = 0;
for (int i = 0; i < graph.getVertexNum(); i++) {
int minDist = Integer.MAX_VALUE;
int minVertex = -1;
for (int j = 0; j < graph.getVertexNum(); j++) {
if (!visited[j] && dist[j] < minDist) {
minDist = dist[j];
minVertex = j;
}
}
if (minVertex == -1) {
break;
}
visited[minVertex] = true;
List<Integer> adjList = graph.getAdjList(minVertex);
for (Integer adj : adjList) {
int weight = graph.getWeight(minVertex, adj);
if (!visited[adj] && dist[minVertex] + weight < dist[adj]) {
dist[adj] = dist[minVertex] + weight;
}
}
}
}
// 获取源点到终止点的路径
public List<Integer> getPath(int start, int end) {
List<Integer> path = new ArrayList<>();
int cur = end;
while (cur != start) {
path.add(cur);
List<Integer> adjList = graph.getAdjList(cur);
int minDist = Integer.MAX_VALUE;
int minVertex = -1;
for (Integer adj : adjList) {
if (dist[adj] < minDist) {
minDist = dist[adj];
minVertex = adj;
}
}
cur = minVertex;
}
path.add(start);
Collections.reverse(path);
return path;
}
// getter和setter方法
// ...
}
```
以上就是实现对景点信息的管理和维护,以及路径导游的基本思路和代码示例。在GUI部分,可以使用Swing或JavaFX等GUI库实现界面,通过调用上述代码实现功能。需要注意的是,由于界面部分比较复杂,这里不再给出具体实现。