You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

A*寻路算法是否必须使用曼哈顿距离?能否改用勾股定理?

Can I Replace Manhattan Distance with Euclidean Distance in A* Pathfinding (Unity/C#)?

Great question! I’ve worked through Sebastian Lague’s A* tutorial myself, so I know exactly where you’re coming from. The short answer is: you absolutely can swap Manhattan distance for Euclidean (Pythagorean) distance—but it’s important to understand when and why you’d want to, along with a few key considerations.

Let’s break this down clearly:

Core Background: A*’s Heuristic Rule

A* depends on a heuristic function h(n) that estimates the remaining distance from a node n to the target. To guarantee A* finds the shortest path, this heuristic must be admissible—meaning it never overestimates the actual remaining distance. Both Manhattan and Euclidean distance meet this requirement, but they’re tailored to different movement scenarios.

When to Use Which Distance

  • Manhattan Distance: Perfect for grid-based movement restricted to 4 directions (up/down/left/right). It calculates the exact number of steps needed to move between two points in this grid, making it a tight, accurate heuristic. This means A* will explore fewer nodes and find paths faster since h(n) closely matches the actual path cost.

    • Example code from Sebastian’s tutorial:
      int ManhattanDistance(Node a, Node b)
      {
          return Mathf.Abs(a.gridX - b.gridX) + Mathf.Abs(a.gridY - b.gridY);
      }
      
  • Euclidean Distance: Ideal if your movement allows 8 directions (diagonals) or if you’re working in a continuous (non-grid) space. It calculates the straight-line distance between two points using the Pythagorean theorem, which aligns better with the shortest possible path when diagonal movement is allowed.

    • Replacement code (switch to floats since Euclidean returns non-integer values):
      float EuclideanDistance(Node a, Node b)
      {
          float dx = a.gridX - b.gridX;
          float dy = a.gridY - b.gridY;
          return Mathf.Sqrt(dx * dx + dy * dy);
      }
      

Key Notes for Swapping

  1. Heuristic "Guidance": For 4-directional movement, Euclidean distance will be a lower estimate than Manhattan (e.g., a point 3 units right and 4 units down has a Manhattan distance of 7, but Euclidean is 5). This weaker heuristic means A* might explore more nodes before finding the path, but it will still find the shortest path (since it’s admissible).
  2. Data Types: Manhattan uses integers, while Euclidean returns floats. You’ll need to update variables storing the heuristic (like in your priority queue or node class) to handle floats—Sebastian’s tutorial uses integers for simplicity, so this is a small but necessary tweak.
  3. Performance: Floating-point calculations are slightly slower than integer operations, but in most Unity projects (even with large grids), this difference is negligible. Only worry about it if you’re dealing with thousands of nodes and need maximum performance.

Final Takeaway

If your game allows diagonal movement, go with Euclidean distance—it’s a better fit. If you’re stuck to 4 directions, Manhattan is more efficient, but Euclidean will still work perfectly fine. The code change is straightforward, so feel free to test both and see which works better for your specific use case.

内容的提问来源于stack exchange,提问作者Theodor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 12:38:11