class Solution: def move(self, n, x1, y1, z1, x2, y2, z2) -> bool: if x1 + y1 + z1 != x2 + y2 + z2: return False x1, y1, z1 = sorted((x1, y1, z1)) x2, y2, z2 = sorted((x2, y2, z2)) if x1 == x2 and y1 == y2: return True if x2 % 3 == y2 % 3 == z2 % 3: return False mymap = set() mylist = [(x1, y1, z1)] while mylist: x, y, z = mylist.pop() if x == x2 and y == y2: return True if (x, y) not in mymap: mymap.add((x, y)) for (x, y, z) in [(x, y, z), (x, z, y), (y, x, z), (y, z, x), (z, x, y), (z, y, x), ]: if 0 <= 2 * y - x + 1 <= n and 0 <= 2 * x - y - 1 <= n: x, y, z = sorted((2 * y - x + 1, 2 * x - y - 1, z)) if (x, y) not in mymap: mylist.append((x, y, z)) return False 本地运行,n=3000 坐标1500左右,时间3.4s