package com.test;
import org.jgrapht.Graph;
import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleWeightedGraph;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Node {
int x;
int y;
boolean isStation;
String name;
int count;
int max;
public Node(int x, int y, String name) {
this.x = x;
this.y = y;
this.name = name;
this.isStation = false;
}
public Node(int x, int y, int count, int max, String name) {
this.x = x;
this.y = y;
this.name = name;
this.isStation = true;
this.count = count;
this.max = max;
}
// 其他方法...
@Override
public String toString() {
if (isStation) {
return name + "(" + x + "," + y + ") 自行车:" + count + "/ 停车上限:" + max;
}
return name + "(" + x + "," + y + ")";
}
}
public class WeightedGraphExample {
// A|北门|B
//西门| 人|东门
// C|南门|D
// 人 x(5,15) y(5,15)
static Node man = new Node(7, 14, "人");
// 添加车站
static Node stationA = new Node(0, 20, 50, 100,"A");
static Node stationB = new Node(20, 20, 30, 50,"B");
static Node stationC = new Node(0, 0, 80, 80,"C");
static Node stationD = new Node(20, 0, 20, 30,"D");
// 添加学校大门
static Node northGate = new Node(10, 15,"北门");
static Node eastGate = new Node(15, 10,"东门");
static Node southGate = new Node(10, 5,"南门");
static Node westGate = new Node(5, 10,"西门");
static ArrayList<Node> nodes = new ArrayList<Node>();
public static void main(String[] args) {
nodes.add(man);
nodes.add(stationA);
nodes.add(stationB);
nodes.add(stationC);
nodes.add(stationD);
nodes.add(northGate);
nodes.add(eastGate);
nodes.add(southGate);
nodes.add(westGate);
while (true) {
System.out.println("欢迎使用车站查询系统");
System.out.println("1、查询区域车站信息");
System.out.println("2、查询最近的有车车站");
System.out.println("3、查询最近的可停靠车站");
System.out.println("4、退出");
String string = new Scanner(System.in).nextLine();
if (Integer.parseInt(string) == 1) {
while (true) {
try {
System.out.println("请输入坐标x范围,例:0,20");
String[] split = new Scanner(System.in).nextLine().split(",");
int xStart = Integer.parseInt(split[0]);
int xEnd = Integer.parseInt(split[1]);
System.out.println("请输入坐标y范围,例:0,20");
split = new Scanner(System.in).nextLine().split(",");
int yStart = Integer.parseInt(split[0]);
int yEnd = Integer.parseInt(split[1]);
for (Node node:nodes) {
if (node.isStation) {
if (node.x >= xStart && node.x <= xEnd && node.y >= yStart && node.y <= yEnd) {
System.out.println(node);
}
}
}
break;
}catch (Exception e) {
System.out.println("输入有误,请重新输入");
}
}
}else if (Integer.parseInt(string) == 2) {
while (true) {
try {
System.out.println("请输入坐标x,y");
String[] split = new Scanner(System.in).nextLine().split(",");
int x = Integer.parseInt(split[0]);
int y = Integer.parseInt(split[1]);
if (x < 5 || x > 15 || y < 5 || y > 15) {
throw new Exception();
}
man.x = x;
man.y = y;
findStation(false);
break;
} catch (Exception e) {
System.out.println("x,y范围是5,15,假设在校园内部,必须经过大门,请重新输入");
}
}
}else if (Integer.parseInt(string) == 3) {
while (true) {
try {
System.out.println("请输入坐标x,y");
String[] split = new Scanner(System.in).nextLine().split(",");
int x = Integer.parseInt(split[0]);
int y = Integer.parseInt(split[1]);
if (x < 5 || x > 15 || y < 5 || y > 15) {
throw new Exception();
}
man.x = x;
man.y = y;
findStation(true);
break;
} catch (Exception e) {
System.out.println("x,y范围是5,15,假设在校园内部,必须经过大门,请重新输入");
}
}
}else if (Integer.parseInt(string) == 4) {
break;
}
}
}
public static void findStation(boolean isDocked) {
Graph<Node, DefaultWeightedEdge> graph = new SimpleWeightedGraph<>(DefaultWeightedEdge.class);
graph.addVertex(man);
graph.addVertex(stationA);
graph.addVertex(stationB);
graph.addVertex(stationC);
graph.addVertex(stationD);
graph.addVertex(northGate);
graph.addVertex(eastGate);
graph.addVertex(southGate);
graph.addVertex(westGate);
// 添加带权边,表示节点之间的距离
graph.setEdgeWeight(graph.addEdge(stationA, stationB), calculateDistance(stationA, stationB));
graph.setEdgeWeight(graph.addEdge(stationB, stationC), calculateDistance(stationB, stationC));
graph.setEdgeWeight(graph.addEdge(stationC, stationD), calculateDistance(stationC, stationD));
graph.setEdgeWeight(graph.addEdge(stationD, stationA), calculateDistance(stationD, stationA));
graph.setEdgeWeight(graph.addEdge(northGate, stationA), calculateDistance(northGate, stationA));
graph.setEdgeWeight(graph.addEdge(northGate, stationB), calculateDistance(northGate, stationB));
graph.setEdgeWeight(graph.addEdge(eastGate, stationB), calculateDistance(eastGate, stationB));
graph.setEdgeWeight(graph.addEdge(eastGate, stationD), calculateDistance(eastGate, stationD));
graph.setEdgeWeight(graph.addEdge(southGate, stationC), calculateDistance(southGate, stationC));
graph.setEdgeWeight(graph.addEdge(southGate, stationD), calculateDistance(southGate, stationD));
graph.setEdgeWeight(graph.addEdge(westGate, stationA), calculateDistance(westGate, stationA));
graph.setEdgeWeight(graph.addEdge(westGate, stationC), calculateDistance(westGate, stationC));
graph.setEdgeWeight(graph.addEdge(man, northGate), calculateDistance(man, northGate));
graph.setEdgeWeight(graph.addEdge(man, eastGate), calculateDistance(man, eastGate));
graph.setEdgeWeight(graph.addEdge(man, southGate), calculateDistance(man, southGate));
graph.setEdgeWeight(graph.addEdge(man, westGate), calculateDistance(man, westGate));
if (isDocked) {
// 查找最近有车的节点
Node nearestNodeWithDifferentCountMax = findNearestNodeWithDifferentCountMax(graph, man);
System.out.println("最近的可停靠车站: " + nearestNodeWithDifferentCountMax);
// 输出�
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
公共自行车停放点信息查询及导航【问题陈述】公共自行车是为方便广大市民出行推行的绿色出行便民政策,但在使用中经常会遇到骑行无车,停靠无空位的情况,本设计就是模拟一个公共自行车停靠信息的查阅,并为客户提供最近停靠点的推荐。【基本要求】以学校周边为例,把所有公共自行车停靠点的信息进行管理,能够按照大约的地理位置进行查询。此外以用户当前位置为用户提供最近的停靠点和路线。【实现提示】在公交软件上查询出学校区域的公共自行车点,用线性表存储相关信息。再构造一个图,把这些公共自行车点作为顶点,通过查找最短(最优路径)为用户推荐停靠点。 在公交软件上查询出学校区域的公共自行车点,用线性表存储相关信息。再构造一个图,把这些公共自行车点作为顶点,通过查找最短(最优路径)为用户推荐停靠点。 在公交软件上查询出学校区域的公共自行车点,用线性表存储相关信息。再构造一个图,把这些公共自行车点作为顶点,通过查找最短(最优路径)为用户推荐停靠点。 在公交软件上查询出学校区域的公共自行车点,用线性表存储相关信息。再构造一个图,把这些公共自行车点作为顶点,通过查找最短(最优路径)为用户推荐停靠点。 在公交软件上查询出学校区
资源推荐
资源详情
资源评论
收起资源包目录
公共自行车停放点信息查询及导航.zip (13个子文件)
公共自行车停放点信息查询及导航
pom.xml 882B
src
test
java
main
resources
java
com
test
WeightedGraphExample.java 11KB
.idea
jarRepositories.xml 1KB
uiDesigner.xml 9KB
workspace.xml 5KB
misc.xml 528B
compiler.xml 523B
.gitignore 0B
encodings.xml 261B
target
classes
com
test
Node.class 1KB
WeightedGraphExample.class 9KB
WeightedGraphExample$NodeWrapper.class 671B
generated-sources
annotations
.gitignore 490B
共 13 条
- 1
资源评论
Al资料站与复盘笔记
- 粉丝: 2399
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功