update distance

This commit is contained in:
bridge
2025-12-08 22:39:44 +08:00
parent 303bffe413
commit 8124537cff
10 changed files with 66 additions and 21 deletions

22
src/utils/distance.py Normal file
View File

@@ -0,0 +1,22 @@
import math
def chebyshev_distance(p1: tuple[int, int], p2: tuple[int, int]) -> int:
"""
计算切比雪夫距离max(|x1-x2|, |y1-y2|)
适用于允许对角线移动的网格地图
"""
return max(abs(p1[0] - p2[0]), abs(p1[1] - p2[1]))
def manhattan_distance(p1: tuple[int, int], p2: tuple[int, int]) -> int:
"""
计算曼哈顿距离:|x1-x2| + |y1-y2|
适用于只允许四向移动的网格地图
"""
return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])
def euclidean_distance(p1: tuple[int, int], p2: tuple[int, int]) -> float:
"""
计算欧几里得距离sqrt((x1-x2)^2 + (y1-y2)^2)
"""
return math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)