Java使用贪婪算法获取一组坐标的最短路径

优化后的目的地顺序:
目的地1: 113.0787409570716, 28.17748698056088
目的地3: 112.9994095942885, 28.16504090914147
目的地2: 113.0544746932484, 28.18304377689985

import java.util.HashMap;
import java.util.Map;

public class 坐标最短距离 {

    public static void main(String[] args) {
        // 初始坐标
        String startingPoint = "112.979, 28.326228";
        Map<String, String> destinations = new HashMap<>();
        destinations.put("目的地1", "113.0787409570716, 28.17748698056088");
        destinations.put("目的地2", "113.0544746932484, 28.18304377689985");
        destinations.put("目的地3", "112.9994095942885, 28.16504090914147");

        Map<String, String> optimizedOrder = optimizeDestinationOrder(startingPoint, destinations);

        System.out.println("优化后的目的地顺序:");
        for (Map.Entry<String, String> entry : optimizedOrder.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }

    // 使用贪婪算法优化目的地顺序
    private static Map<String, String> optimizeDestinationOrder(String startingPoint, Map<String, String> destinations) {
        Map<String, String> optimizedOrder = new HashMap<>();
        String currentLocation = startingPoint;

        while (!destinations.isEmpty()) {
            double minDistance = Double.MAX_VALUE;
            String nearestDestinationName = null;
            String nearestDestination = null;

            for (Map.Entry<String, String> entry : destinations.entrySet()) {
                String name = entry.getKey();
                String destination = entry.getValue();
                double distance = calculateDistance(currentLocation, destination);
                if (distance < minDistance) {
                    minDistance = distance;
                    nearestDestinationName = name;
                    nearestDestination = destination;
                }
            }

            if (nearestDestinationName != null) {
                optimizedOrder.put(nearestDestinationName, nearestDestination);
                destinations.remove(nearestDestinationName);
                currentLocation = nearestDestination;
            }
        }

        return optimizedOrder;
    }


    // 计算两个坐标之间的距离
    private static double calculateDistance(String coordinate1, String coordinate2) {
        // 将坐标解析为经度和纬度
        double lat1 = Double.parseDouble(coordinate1.split(", ")[1]);
        double lon1 = Double.parseDouble(coordinate1.split(", ")[0]);
        double lat2 = Double.parseDouble(coordinate2.split(", ")[1]);
        double lon2 = Double.parseDouble(coordinate2.split(", ")[0]);

        // 地球半径(千米)
        double earthRadius = 6371.0;

        // 将经纬度转换为弧度
        double lat1Rad = Math.toRadians(lat1);
        double lon1Rad = Math.toRadians(lon1);
        double lat2Rad = Math.toRadians(lat2);
        double lon2Rad = Math.toRadians(lon2);

        // Haversine公式
        double dlon = lon2Rad - lon1Rad;
        double dlat = lat2Rad - lat1Rad;
        double a = Math.sin(dlat / 2) * Math.sin(dlat / 2) + Math.cos(lat1Rad) * Math.cos(lat2Rad) * Math.sin(dlon / 2) * Math.sin(dlon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double distance = earthRadius * c;
        return distance;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值