From 96f987b030f4c961a985f07079ba12abd865fdb2 Mon Sep 17 00:00:00 2001
From: Junjie <DELL@qq.com>
Date: 星期三, 17 十二月 2025 08:12:24 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/common/utils/NavigateSolution.java | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 files changed, 115 insertions(+), 4 deletions(-)
diff --git a/src/main/java/com/zy/common/utils/NavigateSolution.java b/src/main/java/com/zy/common/utils/NavigateSolution.java
index 0cd7bed..e189e68 100644
--- a/src/main/java/com/zy/common/utils/NavigateSolution.java
+++ b/src/main/java/com/zy/common/utils/NavigateSolution.java
@@ -10,6 +10,8 @@
import com.zy.common.model.NavigateNode;
import com.zy.core.enums.MapNodeType;
import java.util.*;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.BiFunction;
/**
* A*绠楁硶瀹炵幇绫�
@@ -23,9 +25,9 @@
//鐢ㄦ潵瀛樻斁宸茬粡鍑虹幇杩囩殑缁撶偣銆�
Map<String, Integer> bestGMap = new HashMap<>();
- public List<List<NavigateNode>> getStationMap() {
+ public List<List<NavigateNode>> getStationMap(int lev) {
BasMapService basMapService = SpringUtils.getBean(BasMapService.class);
- BasMap basMap = basMapService.selectOne(new EntityWrapper<BasMap>().eq("lev", 1));
+ BasMap basMap = basMapService.selectOne(new EntityWrapper<BasMap>().eq("lev", lev));
if (basMap == null) {
throw new CoolException("鍦板浘涓嶅瓨鍦�");
}
@@ -63,9 +65,9 @@
return navigateNodeList;
}
- public List<List<NavigateNode>> getRgvTrackMap() {
+ public List<List<NavigateNode>> getRgvTrackMap(int lev) {
BasMapService basMapService = SpringUtils.getBean(BasMapService.class);
- BasMap basMap = basMapService.selectOne(new EntityWrapper<BasMap>().eq("lev", 1));
+ BasMap basMap = basMapService.selectOne(new EntityWrapper<BasMap>().eq("lev", lev));
if (basMap == null) {
throw new CoolException("鍦板浘涓嶅瓨鍦�");
}
@@ -157,6 +159,115 @@
return null;
}
+ public List<List<NavigateNode>> allSimplePaths(
+ List<List<NavigateNode>> map,
+ NavigateNode start,
+ NavigateNode end,
+ int maxDepth, // 鏈�澶ф鏁�(杈规暟). 寤鸿锛�50/100/200锛�<=0 琛ㄧず涓嶉檺鍒讹紙涓嶅缓璁級
+ int maxPaths, // 鏈�澶ц繑鍥炴潯鏁�. 寤鸿锛�100/500/2000锛�<=0 琛ㄧず涓嶉檺鍒讹紙涓嶅缓璁級
+ int maxCost // 鏈�澶ф�讳唬浠�(鍚嫄鐐规儵缃�). <=0 琛ㄧず涓嶉檺鍒�
+ ) {
+ List<List<NavigateNode>> results = new ArrayList<>();
+ if (map == null || map.isEmpty() || map.get(0).isEmpty()) return results;
+ if (start == null || end == null) return results;
+ if (start.getValue() == MapNodeType.DISABLE.id || end.getValue() == MapNodeType.DISABLE.id) return results;
+
+ // visited 鐢ㄥ潗鏍� key锛岄伩鍏� NavigateNode equals/hashCode 涓嶅彲闈犲鑷撮噸澶嶅垽鏂け鏁�
+ Set<String> visited = new HashSet<>(map.size() * map.get(0).size() * 2);
+ LinkedList<NavigateNode> path = new LinkedList<>();
+
+ String startKey = keyOf(start);
+ visited.add(startKey);
+ path.add(start);
+
+ AtomicInteger pathCount = new AtomicInteger(0);
+
+ dfsAllSimplePaths(map, start, end,
+ visited, path, results,
+ 0, // depth
+ 0, // cost
+ maxDepth, maxPaths, maxCost,
+ pathCount
+ );
+
+ return results;
+ }
+
+ /**
+ * DFS + 鍥炴函锛氭灇涓炬墍鏈夌畝鍗曡矾寰勶紙璺緞涓笉鍏佽閲嶅鑺傜偣锛�
+ */
+ private void dfsAllSimplePaths(
+ List<List<NavigateNode>> map,
+ NavigateNode current,
+ NavigateNode end,
+ Set<String> visited,
+ LinkedList<NavigateNode> path,
+ List<List<NavigateNode>> results,
+ int depth, // 褰撳墠姝ユ暟锛堣竟鏁帮級
+ int cost, // 褰撳墠鎬讳唬浠凤紙浣犲彲浠ヨ涓烘槸 g锛�
+ int maxDepth,
+ int maxPaths,
+ int maxCost,
+ AtomicInteger pathCount
+ ) {
+ // 闃茬垎锛氭潯鏁伴檺鍒�
+ if (maxPaths > 0 && pathCount.get() >= maxPaths) return;
+
+ // 鍒拌揪缁堢偣锛氭敹闆嗚矾寰�
+ if (current.getX() == end.getX() && current.getY() == end.getY()) {
+ results.add(new ArrayList<>(path));
+ pathCount.incrementAndGet();
+ return;
+ }
+
+ // 闃茬垎锛氭繁搴﹂檺鍒讹紙depth 琛ㄧず宸茶蛋鐨勮竟鏁帮級
+ if (maxDepth > 0 && depth >= maxDepth) return;
+
+ // 鎵╁睍閭诲眳锛堜弗鏍煎鐢ㄤ綘鑷繁鐨勫彲琛岃蛋鏂瑰悜瑙勫垯锛�
+ ArrayList<NavigateNode> neighbors = extend_current_node(map, current);
+ if (neighbors == null || neighbors.isEmpty()) return;
+
+ for (NavigateNode next : neighbors) {
+ // 闃茬垎锛氭潯鏁伴檺鍒�
+ if (maxPaths > 0 && pathCount.get() >= maxPaths) return;
+
+ if (next == null) continue;
+ if (next.getValue() == MapNodeType.DISABLE.id) continue;
+
+ String nk = keyOf(next);
+
+ // 绠�鍗曡矾寰勶細涓嶅厑璁搁噸澶嶈妭鐐�
+ if (visited.contains(nk)) continue;
+
+ // 浣犵殑浠d环瑙勫垯锛氭瘡姝� 1 + 鎷愮偣鎯╃綒
+ int stepCost = 1 + calcNodeExtraCost(current, next, end);
+ int newCost = cost + stepCost;
+
+ // 闃茬垎锛氭�讳唬浠烽檺鍒�
+ if (maxCost > 0 && newCost > maxCost) continue;
+
+ // 杩涘叆
+ visited.add(nk);
+ path.addLast(next);
+
+ dfsAllSimplePaths(map, next, end,
+ visited, path, results,
+ depth + 1,
+ newCost,
+ maxDepth, maxPaths, maxCost,
+ pathCount
+ );
+
+ // 鍥炴函
+ path.removeLast();
+ visited.remove(nk);
+ }
+ }
+
+ private String keyOf(NavigateNode n) {
+ return n.getX() + "_" + n.getY();
+ }
+
public ArrayList<NavigateNode> extend_current_node(List<List<NavigateNode>> map, NavigateNode current_node) {
//鑾峰彇褰撳墠缁撶偣鐨剎, y
int x = current_node.getX();
--
Gitblit v1.9.1