组合数搜索
原题在这里:
概述题意,给定u,v和x,y,问能否通过(u+v,v)or(u,u+v)到达(x,y)。
analyse:
正向搜索肯定是不能的,逆向就很轻松了。
有规律的是:
x>y,那么上一步肯定是(x-y,y)
x<y,那么上一步肯定是(x,y-x)
特别的,如果有很多连续相同的操作,那么可以优化处理成取余操作。
code:
class Solution { public: bool reachingPoints(int sx, int sy, int tx, int ty) { while (tx >= sx && ty >= sy) { if (tx == sx && ty == sy) return true; if (tx > ty) tx -= max((tx - sx) / ty, 1) * ty; else ty -= max((ty - sy) / tx, 1) * tx; } return false; } };
【Over】

浙公网安备 33010602011771号