780. 到达终点

exiaohu 于 2022-04-09 发布

题目链接:780. 到达终点

这道题有点东西。从 (sx, sy) 推到 (tx, ty) 的搜索空间太大,随着变换次数增加,搜索空间指数级增长。

但是如果换个方向,从 (tx, ty) 倒推到 (sx, sy) 。每一步其实只能进行一种操作:

此时具有约束:

class Solution:
    def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:
        while (tx >= sx and ty > sy) or (ty >= sy and tx > sx):
            if tx > ty:
                tx -= (tx - sx) // ty * ty or ty
            else:
                ty -= (ty - sy) // tx * tx or tx

        return ty == sy and tx == sx