| | |
| | | |
| | | public class DijkstraUtils { |
| | | |
| | | HashMap<PathPlan.Place, PathPlan.Distance> distances; |
| | | HashMap<PathPlan.Place, PathPlan.Distance> distances;//每个点的信息集合 |
| | | |
| | | public DijkstraUtils(HashMap<PathPlan.Place, PathPlan.Distance> distances) { |
| | | this.distances = distances; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 执行 |
| | | * 执行算法 |
| | | * @param start 起点 |
| | | * @param end |
| | | */ |
| | |
| | | ArrayList<PathPlan.Place> places = new ArrayList<>(); |
| | | places.add(start); |
| | | while (distances.size()>0){//循环遍历 |
| | | PathPlan.Place nearWay = findNearWay(startDis); |
| | | startDis = distances.get(nearWay); |
| | | deletePlace(nearWay); |
| | | places.add(nearWay); |
| | | PathPlan.Place nearWay = findNearWay(startDis);//获取与其他点的最短个那一个 |
| | | startDis = distances.get(nearWay);//距离信息 |
| | | deletePlace(nearWay);//从集合中删除,避免下次循环再次出现该地点 |
| | | places.add(nearWay);//添加到线路集合记录中 |
| | | } |
| | | if(end != null){ |
| | | if(end != null){//如果有终点则最后追加 |
| | | end.distance = endDis.distanceMap.get(places.get(places.size()-1)); |
| | | places.add(end); |
| | | } |
| | | return places; |
| | | return places;//返回规划的路线集合 |
| | | |
| | | } |
| | | |
| | |
| | | ArrayList<ArrayList<PathPlan.Place>> arrayLists = new ArrayList<>();//路线记录 |
| | | /** |
| | | * 自定义规划路线算法 |
| | | * @param start 起始地点 |
| | | * @param end 末尾地点 |
| | | */ |
| | | public ArrayList<ArrayList<PathPlan.Place>> initCustomPathPlan( PathPlan.Place start,PathPlan.Place end){ |
| | | arrayLists.clear();//清空记录 |
| | | ArrayList<PathPlan.Place > places = new ArrayList<>(distances.keySet()); |
| | | ArrayList<PathPlan.Place > places = new ArrayList<>(distances.keySet());//所有地点集合 |
| | | for(int i=1;i<=places.size();i++){ |
| | | sortPlace(places,new ArrayList<PathPlan.Place>(),i,start,end); |
| | | } |
| | |
| | | |
| | | /** |
| | | * 组合 |
| | | * @param datas |
| | | * @param target |
| | | * @param num |
| | | * @param start 起始 |
| | | * @param end 末尾 |
| | | * @param datas 地点数据 |
| | | * @param target 整理的新路线 |
| | | * @param num 循环的下标位置,即路线规划的地点数量 |
| | | * @param start 起始地点 |
| | | * @param end 末尾地点 |
| | | */ |
| | | private void sortPlace(ArrayList<PathPlan.Place> datas, ArrayList<PathPlan.Place> target,int num,PathPlan.Place start,PathPlan.Place end) { |
| | | if (target.size() == num) { |
| | | ArrayList newDatas = new ArrayList( ); |
| | | ArrayList<PathPlan.Place > places = new ArrayList<>(distances.keySet()); |
| | | //数量不够,起始和末尾不对应 |
| | | ArrayList<PathPlan.Place > places = new ArrayList<>(distances.keySet());//所有地点集合 |
| | | //数量不够,起始和末尾不对应,则不再执行 |
| | | if(places.size()> num || start!=null && !target.get(0).equals(start) || end!=null && !target.get(target.size()-1).equals(end)){ |
| | | return; |
| | | } |
| | |
| | | //记录最短距离 |
| | | if(dist == -1 || dist >= tempDist){ |
| | | if(dist > tempDist){ |
| | | arrayLists.clear(); |
| | | arrayLists.clear();//清除之前的数据 |
| | | } |
| | | dist = tempDist; |
| | | arrayLists.add(newDatas); |
| | |
| | | sortPlace(newDatas, newTarget,num,start,end); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 创建一个随机位置 坐标在0-100内 |
| | | * @return |
| | | */ |
| | | public static Point random(){ |
| | | Point point = new Point(); |
| | | |