迷宫这一类题目的真正难点不在「能不能生成」或「能不能找到路」,而在「生成出来的东西是否可控」以及「找到的路是否好用」。多数教程用 DFS 生成、用 BFS 求解,跑通之后会发现两个问题:一是生成形态完全由随机数决定,想让迷宫更像长走廊或更像灌木丛都无从下手;二是求解出来的路径是一格一格的阶梯折线,长度比人眼看到的「直线距离」长得多,看起来非常笨。
本文用两个组合手段解决这两个问题:生成端用生长树(Growing Tree),用一个连续参数在 DFS 与 Prim 之间插值,让形态可控;求解端用 Theta*,允许把路径在拐角处直接拉直,代价从「格子数」变成「真实的欧氏长度」。
一、生长树:一个参数插值两种经典生成器
生长树算法的骨架极其简单:维护一个「活跃单元」集合,每轮从集合里挑一个单元,向它的某个未访问邻居打通一面墙,再把新单元加入集合;当某个单元已经没有未访问邻居时,把它从集合中剔除。
关键在于「挑哪个单元」这一步。这一步的取法直接决定了最终迷宫长什么样:
| 取法 | 等价算法 | 形态特征 |
|---|---|---|
| 永远取最新加入的单元 | 深度优先搜索 DFS | 长走廊、分叉少、回溯感强,像一条缠绕的蛇 |
| 永远随机取 | 随机 Prim | 分叉密集、路径短促、像灌木丛 |
按概率 newestProb 取最新 |
两者混合 | 形态在以上两端连续插值 |
实现上只需要一行三元表达式:rnd.nextDouble() < newestProb ? active.size()-1 : rnd.nextInt(active.size())。newestProb = 1.0 时行为完全等于 DFS,= 0.0 时完全等于 Prim,中间值则是混合体。这就是它相比「只写 DFS」或「只写 Prim」的工程价值——形态是一个可调参数,而不是一个需要换算法的离散选择。
下面的实现中,用的是逻辑格子坐标 (cx, cy),映射到网格 (2*cx+1, 2*cy+1),墙位于偶数下标处。这种「格子 + 墙」的双倍分辨率网格是迷宫生成的通用做法,因为墙成为独立的一格,打通时只需把两格之间的那一格置为通路。
package com.example.maze;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* 迷宫网格:使用「生长树(Growing Tree)」算法生成完美迷宫。
*
* 坐标约定:逻辑格子 (cx, cy),cx∈[0, cols),cy∈[0, rows);
* 映射到网格坐标 (2*cx+1, 2*cy+1)。墙位于偶数下标处。
*
* 生长树与 DFS / 随机 Prim 的关系:挑「最新」单元的比率由 newestProb 控制,
* 1.0 退化为 DFS,0.0 退化为随机 Prim,中间值是两者混合。
*/
public final class Maze implements Grid {
private final int[][] grid; // 1 = 墙,0 = 通路
private final int cols;
private final int rows;
private final long seed;
private final double newestProb;
private Maze(int[][] grid, int cols, int rows, long seed, double newestProb) {
this.grid = grid;
this.cols = cols;
this.rows = rows;
this.seed = seed;
this.newestProb = newestProb;
}
public static Maze generate(int cols, int rows, double newestProb, long seed) {
return generate(cols, rows, newestProb, 0.0, seed);
}
/**
* @param braidRatio 死胡同被打开成回路的比例,0 = 完美迷宫
*/
public static Maze generate(int cols, int rows, double newestProb,
double braidRatio, long seed) {
if (cols < 2 || rows < 2) {
throw new IllegalArgumentException("迷宫至少需要 2x2 个逻辑格子");
}
int gw = 2 * cols + 1;
int gh = 2 * rows + 1;
int[][] grid = new int[gh][gw];
for (int y = 0; y < gh; y++) {
for (int x = 0; x < gw; x++) {
grid[y][x] = 1; // 先全部填墙
}
}
Random rnd = new Random(seed);
boolean[][] visited = new boolean[rows][cols];
List<int[]> active = new ArrayList<>(); // 活跃单元集合
int sx = rnd.nextInt(cols);
int sy = rnd.nextInt(rows);
visited[sy][sx] = true;
grid[2 * sy + 1][2 * sx + 1] = 0;
active.add(new int[]{sx, sy});
int[] dx = {1, -1, 0, 0};
int[] dy = {0, 0, 1, -1};
while (!active.isEmpty()) {
// 核心一行:取最新(DFS 分支)还是随机取(Prim 分支)
int idx = (rnd.nextDouble() < newestProb)
? active.size() - 1
: rnd.nextInt(active.size());
int[] cell = active.get(idx);
int cx = cell[0];
int cy = cell[1];
List<Integer> candidates = new ArrayList<>(4);
for (int d = 0; d < 4; d++) {
int nx = cx + dx[d];
int ny = cy + dy[d];
if (nx >= 0 && nx < cols && ny >= 0 && ny < rows && !visited[ny][nx]) {
candidates.add(d);
}
}
if (candidates.isEmpty()) {
// 死胡同:从活跃集合永久移除(用末尾元素填补空洞,O(1) 删除)
int last = active.size() - 1;
active.set(idx, active.get(last));
active.remove(last);
continue;
}
int d = candidates.get(rnd.nextInt(candidates.size()));
int nx = cx + dx[d];
int ny = cy + dy[d];
// 打通两格之间那面墙
grid[2 * cy + 1 + dy[d]][2 * cx + 1 + dx[d]] = 0;
grid[2 * ny + 1][2 * nx + 1] = 0;
visited[ny][nx] = true;
active.add(new int[]{nx, ny});
}
// 开两个口:左上角与右下角的外墙
grid[1][0] = 0;
grid[gh - 2][gw - 1] = 0;
Maze m = new Maze(grid, cols, rows, seed, newestProb);
if (braidRatio > 0) {
m.braid(braidRatio, rnd);
}
return m;
}
/**
* 编织(braid):随机拆掉死胡同的一面墙,把「树」变成「图」,制造回路。
* 完美迷宫的任意两点只有唯一路径,Theta* 拉直几乎没有收益;
* 一旦存在回路,「拉直」才等价于「抄近路」。
*/
private void braid(double ratio, Random rnd) {
int[] dx = {1, -1, 0, 0};
int[] dy = {0, 0, 1, -1};
for (int cy = 0; cy < rows; cy++) {
for (int cx = 0; cx < cols; cx++) {
if (rnd.nextDouble() > ratio) {
continue;
}
int degree = 0; // 只处理死胡同:通路邻居恰好 1 个
for (int d = 0; d < 4; d++) {
int nx = cx + dx[d];
int ny = cy + dy[d];
if (nx >= 0 && nx < cols && ny >= 0 && ny < rows
&& grid[2 * cy + 1 + dy[d]][2 * cx + 1 + dx[d]] == 0) {
degree++;
}
}
if (degree != 1) {
continue;
}
List<Integer> walls = new ArrayList<>(4);
for (int d = 0; d < 4; d++) {
int nx = cx + dx[d];
int ny = cy + dy[d];
if (nx >= 0 && nx < cols && ny >= 0 && ny < rows
&& grid[2 * cy + 1 + dy[d]][2 * cx + 1 + dx[d]] == 1) {
walls.add(d);
}
}
if (walls.isEmpty()) {
continue;
}
int d = walls.get(rnd.nextInt(walls.size()));
grid[2 * cy + 1 + dy[d]][2 * cx + 1 + dx[d]] = 0;
}
}
}
@Override
public int width() {
return grid[0].length;
}
@Override
public int height() {
return grid.length;
}
@Override
public boolean walkable(int x, int y) {
return x >= 0 && y >= 0 && x < width() && y < height() && grid[y][x] == 0;
}
@Override
public int[][] raw() {
return grid;
}
public double newestProb() {
return newestProb;
}
/** 统计通路格与死胡同数,用来量化「形态」而不是靠肉眼判断 */
public int[] shapeStats() {
int open = 0;
int deadEnds = 0;
for (int y = 1; y < height() - 1; y++) {
for (int x = 1; x < width() - 1; x++) {
if (grid[y][x] == 0) {
open++;
int degree = 0;
if (walkable(x + 1, y)) degree++;
if (walkable(x - 1, y)) degree++;
if (walkable(x, y + 1)) degree++;
if (walkable(x, y - 1)) degree++;
if (degree == 1) deadEnds++;
}
}
}
return new int[]{open, deadEnds};
}
}
braid() 这一段是刻意加的,理由在第四节会用数据说明:完美迷宫里 Theta* 几乎无法把路径拉短,因为任意两点之间的通路是唯一的一条,拉直等于绕开墙,而墙不可穿。只有存在回路时,「拉直」才等于「抄近路」。所以评价任意角寻路必须换到有回路的地形或开放场地,否则会得出「Theta* 没用」的错误结论。
二、Theta*:唯一的改动是允许「祖父」当父亲
A* 的父子关系必须是相邻格,路径只能沿八邻域一格一格走。Theta* 改动极小:在松弛邻居 n 时,先检查当前节点 s 的父节点 p 与 n 是否互相可见(Line of Sight);若可见,就把 p 直接设为 n 的父节点,代价按 g(p) + dist(p, n) 计算,跳过 s。
这个改动带来两个直接后果:一是路径在拐角处被「拉直」,节点数从几十个降到个位数;二是每次松弛都要做一次视线查询,losChecks 成为主要额外开销。因此视线判定的正确性与成本,就是 Theta* 的全部工程难点。
视线判定用的是 Bresenham 直线遍历,但相比标准实现必须补上一处:当步进同时跨越 x 与 y(对角移动)时,两个「拐角格」至少有一个必须可通行,否则就是从两堵墙的斜对角缝隙里擦过去——几何上不合法,会产生视觉上明显穿墙的路径。
package com.example.maze;
/**
* 视线(Line of Sight)判定:Theta* 能拉直路径的前提。
* Bresenham 直线遍历 + 对角跨越时的拐角合法性检查,
* 避免路径从两堵墙的斜对角缝隙里「擦过去」。
*/
public final class LineOfSight {
private LineOfSight() {
}
public static boolean clear(Grid maze, int x0, int y0, int x1, int y1) {
if (!maze.walkable(x0, y0) || !maze.walkable(x1, y1)) {
return false;
}
int dx = Math.abs(x1 - x0);
int dy = Math.abs(y1 - y0);
int sx = Integer.signum(x1 - x0);
int sy = Integer.signum(y1 - y0);
int x = x0;
int y = y0;
// err = dx - dy 的累积误差:判断该向 x 迈步还是向 y 迈步
int err = dx - dy;
while (true) {
if (!maze.walkable(x, y)) {
return false;
}
if (x == x1 && y == y1) {
return true;
}
int e2 = 2 * err;
boolean steppedX = false;
boolean steppedY = false;
if (e2 > -dy) {
err -= dy;
x += sx;
steppedX = true;
}
if (e2 < dx) {
err += dx;
y += sy;
steppedY = true;
}
if (steppedX && steppedY) {
// 对角跨越:两个拐角格至少要有一个可通行
if (!maze.walkable(x - sx, y) && !maze.walkable(x, y - sy)) {
return false;
}
}
}
}
/** 欧氏距离:Theta* 的启发函数与直连代价都用它 */
public static double euclid(int x0, int y0, int x1, int y1) {
double dx = x1 - x0;
double dy = y1 - y0;
return Math.sqrt(dx * dx + dy * dy);
}
}
搜索主体如下。为了公平对照,A* 与 Theta* 共用同一份代码,只由一个 useTheta 布尔量切换是否需要做视线查询与祖父连边:
package com.example.maze;
import java.util.*;
/**
* A* 与 Theta* 的对照实现。
* A*:父节点必为相邻格,路径只能沿八邻域一格一格折线前进。
* Theta*:允许把「与当前格互相可见的祖先」直接设为父节点,路径在拐角处被拉直,
* 代价是额外的视线查询开销。两者共用同一套八邻域 + 欧氏代价。
*/
public final class ThetaStar {
private ThetaStar() {
}
public static final class Result {
public final List<int[]> path; // 网格坐标序列(可能被拉直,步长 > 1)
public final double length; // 整条路径的欧氏长度
public final int expanded; // 从 OPEN 弹出并展开的节点数
public final int losChecks; // 视线查询次数
public final double millis;
Result(List<int[]> path, double length, int expanded, int losChecks, double millis) {
this.path = path;
this.length = length;
this.expanded = expanded;
this.losChecks = losChecks;
this.millis = millis;
}
}
private static final int[] DX8 = {1, -1, 0, 0, 1, 1, -1, -1};
private static final int[] DY8 = {0, 0, 1, -1, 1, -1, 1, -1};
private static final int CLOSED = 1;
private static final int OPEN = 2;
/**
* @param useTheta true = Theta*(可见祖先可作父节点);false = 普通 A*
*/
public static Result search(Grid maze, int sx, int sy, int gx, int gy, boolean useTheta) {
long t0 = System.nanoTime();
int w = maze.width();
int n = w * maze.height();
double[] g = new double[n];
int[] parent = new int[n];
int[] state = new int[n];
Arrays.fill(g, Double.POSITIVE_INFINITY);
Arrays.fill(parent, -1);
// 惰性删除:同一节点可多次入队,弹出时用 state 判过期
PriorityQueue<double[]> open =
new PriorityQueue<>((a, b) -> Double.compare(a[0], b[0]));
int[] losChecks = {0};
int expanded = 0;
int startIdx = sy * w + sx;
int goalIdx = gy * w + gx;
g[startIdx] = 0;
parent[startIdx] = startIdx; // 起点自指,取「祖父」时才不会越界
state[startIdx] = OPEN;
open.add(new double[]{LineOfSight.euclid(sx, sy, gx, gy), startIdx});
while (!open.isEmpty()) {
double[] top = open.poll();
int idx = (int) top[1];
if (state[idx] == CLOSED) {
continue; // 过期条目
}
state[idx] = CLOSED;
expanded++;
if (idx == goalIdx) {
break;
}
int cx = idx % w;
int cy = idx / w;
int pIdx = parent[idx]; // 当前节点的父节点 p
int px = pIdx % w;
int py = pIdx / w;
for (int d = 0; d < 8; d++) {
int nx = cx + DX8[d];
int ny = cy + DY8[d];
int nIdx = ny * w + nx;
if (!maze.walkable(nx, ny) || state[nIdx] == CLOSED) {
continue;
}
// 斜向禁止穿过两面墙之间的角缝
if (d >= 4 && !maze.walkable(cx + DX8[d], cy)
&& !maze.walkable(cx, cy + DY8[d])) {
continue;
}
double cand;
int newParent;
if (useTheta && LOS(maze, px, py, nx, ny, losChecks)) {
// 核心分支:绕过当前节点,直接连到 p,路径在此处被拉直
cand = g[pIdx] + LineOfSight.euclid(px, py, nx, ny);
newParent = pIdx;
} else {
cand = g[idx] + LineOfSight.euclid(cx, cy, nx, ny);
newParent = idx;
}
if (cand < g[nIdx]) {
g[nIdx] = cand;
parent[nIdx] = newParent;
state[nIdx] = OPEN;
open.add(new double[]{
cand + LineOfSight.euclid(nx, ny, gx, gy), nIdx});
}
}
}
List<int[]> path = new ArrayList<>();
if (g[goalIdx] == Double.POSITIVE_INFINITY) {
return new Result(Collections.emptyList(), Double.POSITIVE_INFINITY,
expanded, losChecks[0], (System.nanoTime() - t0) / 1e6);
}
int cur = goalIdx;
while (true) {
path.add(new int[]{cur % w, cur / w});
if (cur == startIdx) {
break;
}
cur = parent[cur];
}
Collections.reverse(path);
// 折线段的真实欧氏长度:这才是「拉直」收益的度量
double len = 0;
for (int i = 1; i < path.size(); i++) {
int[] a = path.get(i - 1);
int[] b = path.get(i);
len += LineOfSight.euclid(a[0], a[1], b[0], b[1]);
}
return new Result(path, len, expanded, losChecks[0],
(System.nanoTime() - t0) / 1e6);
}
private static boolean LOS(Grid maze, int x0, int y0, int x1, int y1, int[] counter) {
counter[0]++;
return LineOfSight.clear(maze, x0, y0, x1, y1);
}
}
有一个容易被忽略的实现细节:起点必须自指父节点(parent[startIdx] = startIdx)。否则在 pIdx = -1 时取坐标会算出错误的 px/py,视线查询直接对脏数据下判断,结果可能是随机穿墙。这是从 A* 改 Theta* 时最常见的 bug 来源。
三、工程结构
一个可直接运行的 Maven 布局:
maze-project/
├── pom.xml
└── src/
└── main/
└── java/
└── com/example/maze/
├── Grid.java 只读网格接口(迷宫 / 障碍场地共用)
├── Maze.java 生长树生成 + 编织改造 + 形态统计
├── OpenField.java 稀疏障碍场地生成
├── LineOfSight.java Bresenham 视线判定
├── ThetaStar.java A* / Theta* 统一实现
└── Main.java 形态谱、对照演示、批量基准
pom.xml 只需最简配置,Java 17 即可:
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>maze-project</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
把 Grid 抽成接口是刻意的设计:迷宫与开放场地只是两种障碍布局,搜索代码一行都不用改。很多对比实验失败,恰恰是因为换了地形就顺手换了搜索实现,结论无法归因。
package com.example.maze;
/** 只读网格:把「迷宫」与「稀疏障碍场地」统一成同一份寻路输入 */
public interface Grid {
int width();
int height();
boolean walkable(int x, int y);
int[][] raw(); // 1 = 障碍
}
开放场地就是一片空地加零散障碍,这才是 Theta* 论文里的标准场景:
package com.example.maze;
import java.util.Random;
/** 稀疏障碍场地:Theta* 的主场,开放空间中绕障后可长距离直线前进 */
public final class OpenField implements Grid {
private final int[][] grid;
private final int w;
private final int h;
private OpenField(int[][] grid, int w, int h) {
this.grid = grid;
this.w = w;
this.h = h;
}
/** @param density 每个内格成为障碍的概率 */
public static OpenField generate(int w, int h, double density, long seed) {
int[][] grid = new int[h][w];
Random rnd = new Random(seed);
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
boolean border = x == 0 || y == 0 || x == w - 1 || y == h - 1;
grid[y][x] = (border || rnd.nextDouble() < density) ? 1 : 0;
}
}
return new OpenField(grid, w, h);
}
@Override public int width() { return w; }
@Override public int height() { return h; }
@Override public boolean walkable(int x, int y) {
return x >= 0 && y >= 0 && x < w && y < h && grid[y][x] == 0;
}
@Override public int[][] raw() { return grid; }
}
四、实测数据:形态谱与拉直收益
4.1 生长树的形态确实可量化
固定种子、30×20 逻辑格,只改 newestProb,统计死胡同(只有 1 个通路邻居的通路格)占比:
=== 生长树形态谱(30x20 逻辑格,同种子) ===
newestProb 通路格 死胡同 死胡同占比
1.00 1199 56 4.7 %
0.80 1199 118 9.8 %
0.60 1199 162 13.5 %
0.40 1199 178 14.8 %
0.00 1199 153 12.8 %
newestProb = 1.0(纯 DFS)死胡同占比仅 4.7%,说明走廊很长、很少走进死路;降到 0.4 时死胡同占比升到 14.8%,接近三倍。通路格总数恒为 1199——因为生长树无论怎么挑单元,都会生成恰好覆盖全部格子的树,与挑法无关。这说明 newestProb 调节的是拓扑形状而非规模,这正是「可控」的含义:规模由尺寸参数决定,形态由概率参数决定,两个维度解耦。
4.2 完美迷宫里 Theta* 拉不直路
=== 完美迷宫(41x31,newestProb=0.7) ===
A* 长度= 61.11 路径节点= 53 展开节点= 145 LOS= 0
Theta* 长度= 60.93 路径节点= 29 展开节点= 145 LOS= 232
长度变化 -0.29%,路径节点减少 45.3%
路径长度只缩短 0.29%,但路径节点从 53 降到 29,减少 45.3%。两个数字的差异揭示了一个关键事实:完美迷宫里只有唯一通路,长度几乎无解可优化;能优化的只有「表达方式」——把一串拐来拐去的格子合并成少数几段直线。所以如果只看长度指标,会误判 Theta* 无效;若看路径表示的紧凑度(下游动画平滑、单位转向、车辆朝向控制都用得上),它已经省掉近一半的路径点。
4.3 换到有回路的地形,拉直才是「抄近路」
同一套代码,把地形换成 51×31、障碍密度 0.20 的开放场地:
A* 长度= 61.36 路径节点= 52 展开节点= 468 LOS= 0
Theta* 长度= 57.38 路径节点= 9 展开节点= 110 LOS= 452
拉直收益:路径长度减少 6.49%,路径节点减少 82.7%
长度缩短 6.49%,节点数从 52 掉到 9(减少 82.7%),展开节点也从 468 降到 110。展开节点下降说明拉直不只是「看起来好看」——它改变了搜索树的形状,让搜索更早地逼近目标,这是启发函数与路径形状一致带来的真实效率提升。
批量基准(每档 20 张取平均,仅统计可达样本)进一步确认这个规律随规模稳定:
-- 稀疏障碍场地(density=0.20)
20x20 A*: 长度= 16.7 展开= 44 | Theta*: 长度= 16.1 展开= 35 LOS= 133 | 长度 -3.5%
40x40 A*: 长度= 42.0 展开=261 | Theta*: 长度= 40.5 展开=175 LOS= 626 | 长度 -3.7%
80x80 A*: 长度= 63.1 展开=704 | Theta*: 长度= 61.2 展开=477 LOS= 1669 | 长度 -3.0%
-- 完美迷宫(纯走廊)
20x20 A* 长度= 101.6 | Theta* 长度= 101.6 | 长度 -0.07%
40x40 A* 长度= 226.7 | Theta* 长度= 226.6 | 长度 -0.04%
障碍密度对收益的影响也值得单独测一遍(61×61 场地,40 个种子平均):
| 障碍密度 | 可达样本 | A* 平均长度 | Theta* 平均长度 | 长度收益 |
|---|---|---|---|---|
| 0.05 | 36/40 | 82.9 | 82.2 | 0.85% |
| 0.10 | 31/40 | 83.8 | 82.4 | 1.61% |
| 0.15 | 26/40 | 85.2 | 83.0 | 2.58% |
| 0.20 | 22/40 | 86.8 | 83.9 | 3.33% |
| 0.25 | 16/40 | 89.3 | 85.8 | 3.94% |
| 0.30 | 11/40 | 92.7 | 88.7 | 4.22% |
障碍越密,A* 的阶梯折线被迫绕得越远,Theta* 的拉直收益越大(0.85% → 4.22%)。但也注意「可达样本」一列:密度超过 0.2 后大量种子直接无解,实验中必须把这些样本剔除,否则 Infinity 会把平均值污染成 NaN——这也是做寻路基准时最常见的统计陷阱。
五、什么时候该用哪一个
| 场景 | 推荐 | 理由 |
|---|---|---|
| 只能沿格移动(棋盘、格子塔防) | A* | 移动本身就受格约束,拉直没有意义 |
| 连续移动、只看朝向(RTS 单位、车辆、NPC) | Theta* | 路径点骤减,转向平滑,动画成本低 |
| 纯走廊迷宫 | A* | Theta* 唯一可优化的是表示方式,收益有限 |
| 开放场地 + 零散障碍 | Theta* | 长度与展开节点同时下降 |
| 需要严格最优且不许穿角 | A* + 角缝禁止 | Theta* 是近似最优,穿角规则必须额外实现 |
| 动态障碍(频繁变化) | D* Lite | Theta* 无增量重规划能力,重算代价高 |
一条容易被忽略的实践建议:Theta* 的路径必须在下游重新做碰撞校验。它给出的是节点序列,节点之间的直连段由 clear() 认证过;但如果下游是对连续位置做插值移动(而非按节点瞬移),插值采样点的碰撞仍需单独验证,尤其在单位有体积时——视线判定针对的是零宽度的数学线段,不是有半径的实体。这是从格子寻路迁到连续寻路时最常踩的坑。
六、小结
生长树的价值不在「又一个生成算法」,而在把 DFS 与 Prim 收进一个连续参数里:newestProb 是形态旋钮,尺寸是规模旋钮,两者正交。Theta* 的价值不在「比 A* 短多少」,而在把路径从「格子序列」变成「几何折线」,让长度度量回归欧氏真实值。但两者的收益都强依赖地形:没有回路的地形上,任何「拉直」都无从下手。所以做算法对比时,先确认地形是否给了算法发挥空间,再去判断算法本身是否有效——这比换算法更重要。