简体   繁体   English

从坐标到坐标的可能路径

[英]Possible Path from to Co-ordinates

I recently had an interview and my algorithm only passed all test cases except one and I can't figure out why. 我最近接受了一次采访,我的算法只通过了除一个以外的所有测试用例,我无法弄清楚为什么。 The problem I need to tackle was: 我需要解决的问题是:

Given a standing point(a,b) in an 2D grid is it possible to reach destination point(x, y) or not. 给定2D网格中的站点(a,b)是否可以到达目的地点(x,y)。 The only operation he can do is to move to point(a+b, b) or (a, a+b) from some point (a,b). 他唯一能做的就是从某个点(a,b)移动到(a + b,b)或(a,a + b)点。

I tried to solve it by using gcd. 我试图用gcd来解决它。 eg If gcd(a,b) = gcd(x,y) then its is possible else not. 例如,如果gcd(a,b)= gcd(x,y)那么其他可能就没有了。 The intuition was if k be the gcd of a & b. 直觉是如果k是a&b的gcd。 then, k would also divide (a+b). 那么,k也会除(a + b)。 I used the following algorithm to calculate gcd: 我使用以下算法来计算gcd:

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}

EDIT: Also the numbers a,b,x and y are all positive integers. 编辑:数字a,b,x和y都是正整数。

GCD(3,7) = GCD(7,3) but neither is reachable from the other. GCD(3,7)= GCD(7,3),但两者都无法到达。 Your condition is necessary but not sufficient. 你的病情是必要的,但还不够。

Note that there is a unique possible predecessor to every point. 请注意,每个点都有一个独特的可能前身。 Ie for the point (a,b) if a>b then the predecessor is (ab,b) otherwise the predecessor is (a, ba). 即,对于点(a,b),如果a> b,那么前一个是(ab,b)否则前一个是(a,ba)。

You now have 2 questions on the post: 您现在在帖子上有两个问题:

  1. How do I solve the algorithm? 我该如何解决算法?
    Photon 's link covers this well. Photon链接涵盖了这一点。
  2. Why didn't GCD work? 为什么GCD没有工作?

GCD is a necessary, but not sufficient condition. GCD是必要但不充分的条件。

True, if a=b, then (x, y) is reachable iff GCD(x, y) = a = b. 是的,如果a = b,那么如果GCD(x,y)= a = b,则可以得到(x,y)。 However, this does not generalize to all problem pairs. 但是,这并不能概括为所有问题对。 The trivial counterexample is trying to reach (1, N) from (N,1), where N>1. 平凡的反例试图从(N,1)到达(1,N),其中N> 1。 Another is (2, 3) => (4, 5). 另一个是(2,3)=>(4,5)。


So, let's get to the qualitative part: "I can't figure out ...". 所以,让我们进入定性部分:“我无法弄明白......”。 I suspect that the problem is where you see a similarity between Euclid's algorithm and your addition step. 我怀疑问题是你看到Euclid算法和你的加法步骤之间的相似性。 Even stronger, the "backwards" algorithm in the link suggests that Euclid's algorithm applies. 更强大的是,链接中的“向后”算法表明Euclid的算法适用。

It can, in a way, but not as simply and universally as you've tried to use it. 它可以在某种程度上,但不像你试​​图使用它那样简单和普遍。 Think of the problem as a graph on the positive-integer lattice in the Cartesian plane. 将该问题视为笛卡尔平面中正整数格的图。 The permitted operations (directed edges) define how you can move from one point to another. 允许的操作(有向边)定义了如何从一个点移动到另一个点。

The key term here is directed : once you've "moved" from a starting point to one that defines the GCD in your system, you do not have the freedom to retrace those steps. 这里的关键术语是指向的 :一旦你从一个起点“移动”到一个定义你系统中GCD的那个,你就没有自由回溯这些步骤了。 You move either forward or backward through your graph space. 可以向前或向后移动图表空间。

For instance, although your backward transitions allow you to move from (4, 1) to (1, 1) or from (1, 4) to (1, 1), you cannot use this to conclude a path from (4, 1) to (1, 4): half of those moves are in a direction not permitted. 例如,虽然您的向后过渡允许您从(4,1)移动到(1,1)或从(1,4)移动到(1,1),但是您不能使用它来从(4,1)结束路径)至(1,4):这些动作的一半是不允许的方向。


Does that help dispel the confusion? 这有助于消除混乱吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM