对称法求最短路径
给定一条直线和同侧的两点,求经过直线上一点的最短路径。把一个点关于直线对称,问题就变成一条直线段——拖动接触点,观察总距离如何变化。
6 分钟难度 2/3交互演示区域不展示广告。
AP + PB481.6Minimum475.1Extra6.6
Because PB = PB′ (mirror), AP + PB = AP + PB′, which is shortest when A, P, B′ lie on one straight line. Optimal P is at x ≈ 361, distance 475.1.
Drag the slider (or point P) to move the contact point. The amber path A→P→B is what the ant walks; the green dashed line is the unfolded straight route through B′. The minimum happens exactly where they align.
你看到的是什么
1
把 B 关于直线对称到 B′。对直线上任意接触点 P,PB = PB′,所以 AP + PB = AP + PB′。
2
当 A、P、B′ 三点共线时 AP + PB′ 最短。此时对应的 P 就是答案。
3
把 P 拖离最优位置,总距离只会变大。
为什么重要
这个对称技巧(海伦问题)是许多最短路径与光学结论的基础,包括反射定律:光走最短路径。
复杂度与细节
求最优解为 O(1),可视化实时采样位置。
常见问题
为什么要对称,而不是直接测量?+
对称把一个带约束的两段路径变成一条直线,而直线显然最短。