From ed266efb662c5b8460ecc22e31a070a718f35f9c Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期二, 31 三月 2026 16:08:59 +0800
Subject: [PATCH] #地图编辑路径联通性导致算法计算失败问题修复

---
 src/main/java/com/zy/common/utils/NavigateSolution.java |  190 +++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 181 insertions(+), 9 deletions(-)

diff --git a/src/main/java/com/zy/common/utils/NavigateSolution.java b/src/main/java/com/zy/common/utils/NavigateSolution.java
index 8ddeabe..adf527f 100644
--- a/src/main/java/com/zy/common/utils/NavigateSolution.java
+++ b/src/main/java/com/zy/common/utils/NavigateSolution.java
@@ -183,6 +183,18 @@
             int maxPaths,    // 鏈�澶ц繑鍥炴潯鏁�. 寤鸿锛�100/500/2000锛�<=0 琛ㄧず涓嶉檺鍒讹紙涓嶅缓璁級
             int maxCost      // 鏈�澶ф�讳唬浠�(鍚嫄鐐规儵缃�). <=0 琛ㄧず涓嶉檺鍒�
     ) {
+        return allSimplePaths(map, start, end, maxDepth, maxPaths, maxCost, Collections.emptyList());
+    }
+
+    public List<List<NavigateNode>> allSimplePaths(
+            List<List<NavigateNode>> map,
+            NavigateNode start,
+            NavigateNode end,
+            int maxDepth,
+            int maxPaths,
+            int maxCost,
+            List<Integer> guideStationIds
+    ) {
         List<List<NavigateNode>> results = new ArrayList<>();
         if (map == null || map.isEmpty() || map.get(0).isEmpty()) return results;
         if (start == null || end == null) return results;
@@ -197,13 +209,16 @@
         path.add(start);
 
         AtomicInteger pathCount = new AtomicInteger(0);
+        Map<Integer, NavigateNode> guideNodeMap = buildGuideNodeMap(map, guideStationIds);
 
         dfsAllSimplePaths(map, start, end,
                 visited, path, results,
                 0,  // depth
                 0,  // cost
                 maxDepth, maxPaths, maxCost,
-                pathCount
+                pathCount,
+                guideStationIds,
+                guideNodeMap
         );
 
         return results;
@@ -224,7 +239,9 @@
             int maxDepth,
             int maxPaths,
             int maxCost,
-            AtomicInteger pathCount
+            AtomicInteger pathCount,
+            List<Integer> guideStationIds,
+            Map<Integer, NavigateNode> guideNodeMap
     ) {
         // 闃茬垎锛氭潯鏁伴檺鍒�
         if (maxPaths > 0 && pathCount.get() >= maxPaths) return;
@@ -242,6 +259,7 @@
         // 鎵╁睍閭诲眳锛堜弗鏍煎鐢ㄤ綘鑷繁鐨勫彲琛岃蛋鏂瑰悜瑙勫垯锛�
         ArrayList<NavigateNode> neighbors = extend_current_node(map, current);
         if (neighbors == null || neighbors.isEmpty()) return;
+        neighbors = sortNeighborsByGuide(current, path, neighbors, guideStationIds, guideNodeMap);
 
         for (NavigateNode next : neighbors) {
             // 闃茬垎锛氭潯鏁伴檺鍒�
@@ -271,7 +289,9 @@
                     depth + 1,
                     newCost,
                     maxDepth, maxPaths, maxCost,
-                    pathCount
+                    pathCount,
+                    guideStationIds,
+                    guideNodeMap
             );
 
             // 鍥炴函
@@ -282,6 +302,116 @@
 
     private String keyOf(NavigateNode n) {
         return n.getX() + "_" + n.getY();
+    }
+
+    private Map<Integer, NavigateNode> buildGuideNodeMap(List<List<NavigateNode>> map, List<Integer> guideStationIds) {
+        Map<Integer, NavigateNode> guideNodeMap = new HashMap<>();
+        if (map == null || map.isEmpty() || guideStationIds == null || guideStationIds.isEmpty()) {
+            return guideNodeMap;
+        }
+        for (Integer stationId : guideStationIds) {
+            if (stationId == null || guideNodeMap.containsKey(stationId)) {
+                continue;
+            }
+            NavigateNode stationNode = findStationNavigateNode(map, stationId);
+            if (stationNode != null) {
+                guideNodeMap.put(stationId, stationNode);
+            }
+        }
+        return guideNodeMap;
+    }
+
+    private ArrayList<NavigateNode> sortNeighborsByGuide(NavigateNode current,
+                                                         LinkedList<NavigateNode> path,
+                                                         ArrayList<NavigateNode> neighbors,
+                                                         List<Integer> guideStationIds,
+                                                         Map<Integer, NavigateNode> guideNodeMap) {
+        if (current == null || neighbors == null || neighbors.size() <= 1
+                || guideStationIds == null || guideStationIds.isEmpty()
+                || guideNodeMap == null || guideNodeMap.isEmpty()) {
+            return neighbors;
+        }
+
+        Integer nextGuideStationId = resolveNextGuideStationId(path, guideStationIds);
+        if (nextGuideStationId == null) {
+            return neighbors;
+        }
+        NavigateNode guideTargetNode = guideNodeMap.get(nextGuideStationId);
+        if (guideTargetNode == null) {
+            return neighbors;
+        }
+
+        neighbors.sort((left, right) -> compareGuideNeighbor(current, left, right, nextGuideStationId, guideTargetNode));
+        return neighbors;
+    }
+
+    private Integer resolveNextGuideStationId(LinkedList<NavigateNode> path, List<Integer> guideStationIds) {
+        if (path == null || path.isEmpty() || guideStationIds == null || guideStationIds.isEmpty()) {
+            return null;
+        }
+
+        int cursor = 0;
+        Set<Integer> seen = new HashSet<>();
+        for (NavigateNode node : path) {
+            Integer stationId = extractStationId(node);
+            if (stationId == null || !seen.add(stationId)) {
+                continue;
+            }
+            if (cursor < guideStationIds.size() && stationId.equals(guideStationIds.get(cursor))) {
+                cursor++;
+            }
+        }
+        if (cursor >= guideStationIds.size()) {
+            return null;
+        }
+        return guideStationIds.get(cursor);
+    }
+
+    private int compareGuideNeighbor(NavigateNode current,
+                                     NavigateNode left,
+                                     NavigateNode right,
+                                     Integer nextGuideStationId,
+                                     NavigateNode guideTargetNode) {
+        int leftDirect = isGuideStation(left, nextGuideStationId) ? 0 : 1;
+        int rightDirect = isGuideStation(right, nextGuideStationId) ? 0 : 1;
+        if (leftDirect != rightDirect) {
+            return Integer.compare(leftDirect, rightDirect);
+        }
+
+        int leftDistance = calcNodeCost(left, guideTargetNode);
+        int rightDistance = calcNodeCost(right, guideTargetNode);
+        if (leftDistance != rightDistance) {
+            return Integer.compare(leftDistance, rightDistance);
+        }
+
+        int leftTurnCost = calcNodeExtraCost(current, left, guideTargetNode);
+        int rightTurnCost = calcNodeExtraCost(current, right, guideTargetNode);
+        if (leftTurnCost != rightTurnCost) {
+            return Integer.compare(leftTurnCost, rightTurnCost);
+        }
+
+        return 0;
+    }
+
+    private boolean isGuideStation(NavigateNode node, Integer stationId) {
+        Integer nodeStationId = extractStationId(node);
+        return nodeStationId != null && nodeStationId.equals(stationId);
+    }
+
+    private Integer extractStationId(NavigateNode node) {
+        JSONObject valueObj = parseNodeValue(node == null ? null : node.getNodeValue());
+        return valueObj == null ? null : valueObj.getInteger("stationId");
+    }
+
+    private JSONObject parseNodeValue(String nodeValue) {
+        if (nodeValue == null || nodeValue.trim().isEmpty()) {
+            return null;
+        }
+        try {
+            return JSON.parseObject(nodeValue);
+        } catch (Exception ignore) {
+            return null;
+        }
     }
 
     public ArrayList<NavigateNode> extend_current_node(List<List<NavigateNode>> map, NavigateNode current_node) {
@@ -377,18 +507,60 @@
     }
 
     public NavigateNode findStationNavigateNode(List<List<NavigateNode>> map, int stationId) {
+        NavigateNode bestNode = null;
+        int bestExternalConnectionCount = -1;
+        int bestNeighborCount = -1;
         for(int x = 0; x < map.size(); x++) {
             for(int y = 0; y < map.get(0).size(); y++) {
                 NavigateNode node = map.get(x).get(y);
-                if("devp".equals(node.getNodeType())) {
-                    JSONObject valueObj = JSON.parseObject(node.getNodeValue());
-                    if(valueObj.getInteger("stationId") == stationId) {
-                        return node;
-                    }
+                Integer currentStationId = extractStationId(node);
+                if (currentStationId == null || currentStationId != stationId) {
+                    continue;
+                }
+                ArrayList<NavigateNode> neighbors = extend_current_node(map, node);
+                int externalConnectionCount = countExternalConnectionCount(stationId, neighbors);
+                int neighborCount = neighbors == null ? 0 : neighbors.size();
+                if (externalConnectionCount > bestExternalConnectionCount
+                        || (externalConnectionCount == bestExternalConnectionCount && neighborCount > bestNeighborCount)) {
+                    bestNode = node;
+                    bestExternalConnectionCount = externalConnectionCount;
+                    bestNeighborCount = neighborCount;
                 }
             }
         }
-        return null;
+        return bestNode;
+    }
+
+    private int countExternalConnectionCount(Integer currentStationId, List<NavigateNode> neighbors) {
+        Set<Integer> connectedStationIdSet = new LinkedHashSet<>();
+        if (neighbors == null || neighbors.isEmpty()) {
+            return 0;
+        }
+        for (NavigateNode neighbor : neighbors) {
+            connectedStationIdSet.addAll(resolveAdjacentStationIds(currentStationId, neighbor));
+        }
+        return connectedStationIdSet.size();
+    }
+
+    private Set<Integer> resolveAdjacentStationIds(Integer currentStationId, NavigateNode node) {
+        Set<Integer> stationIdSet = new LinkedHashSet<>();
+        JSONObject valueObj = parseNodeValue(node == null ? null : node.getNodeValue());
+        if (valueObj == null) {
+            return stationIdSet;
+        }
+        Integer directStationId = valueObj.getInteger("stationId");
+        if (directStationId != null && !directStationId.equals(currentStationId)) {
+            stationIdSet.add(directStationId);
+        }
+        if (valueObj.getJSONArray("bridgeStationIds") == null) {
+            return stationIdSet;
+        }
+        for (Integer bridgeStationId : valueObj.getJSONArray("bridgeStationIds").toJavaList(Integer.class)) {
+            if (bridgeStationId != null && !bridgeStationId.equals(currentStationId)) {
+                stationIdSet.add(bridgeStationId);
+            }
+        }
+        return stationIdSet;
     }
 
     public NavigateNode findTrackSiteNoNavigateNode(List<List<NavigateNode>> map, int trackSiteNo) {

--
Gitblit v1.9.1