Find the minimum number of steps to reach a position

179 Views Asked by At

I am currently at a position (0,0) i want to reach a position (0,x) , and i am allowed to go to any point such that distances from my current position is exactly d or e ( Euclidean distance). i.e Locus is a circle
What is the minimum steps required to that ?

1

There are 1 best solutions below

0
On

$\lceil\frac{x}{ \max\{d,e\}}\rceil$
Lets define:
$m:=\max\{d,e\}$
Suppose that x is an exact multiple of $m$, then we can just go there directly. Otherwise, use $k$ steps to go to $(0,k\cdot m)$ where:
$k\cdot m + m < x < k\cdot m + 2\cdot m$
Now define:
$r:=x-k\cdot m$ Now step to:
$(\sqrt{m^2-\frac{r^2}{4}},k\cdot m + \frac{r}{2})$
Then step to:
$(0, k\cdot m + r) = (0,x)$
The only edge case is when $0<x<m$ in which case you need 2 steps instead of the 1 as suggested by the initial formula. This case can be handled by 'side-stepping' similarly as the last 2 steps for larger x.

edit: in the comments you gave the example of:
$x = 3$
$d = 1$
$e = 2$
Using the above we get that we need 2 steps. We have $m=2$, $r=3$ and $k=0$. So we step from $(0,0)$ to $(\sqrt{4 - \frac{9}{4}}, \frac{3}{2})$ and then to $(0,3)$.