-4

Starting from (0, 0) , I have to reach (x, y) in such a way that at any point I can move one step left/right if previous move was up/down and vice-versa. What is the minimum number of moves needed ?

  • It's approximately 2*max(x,y), give or take at most 2. But I'm not sure if Stack Overflow is the right place for this question. SO could be the right place if you had written an algorithm, and there's something wrong with it. – Dialecticus Jul 04 '14 at 13:53
  • What do you mean by zigzags here ? – user155768 Jul 04 '14 at 13:54
  • @Dialecticus, there has to be an exact answer. – user155768 Jul 04 '14 at 13:55
  • obviously `x + y` steps are necessary minimum. – holex Jul 04 '14 at 14:01
  • @holex only if abs(x-y) <= 1. If not then you need more. – Dialecticus Jul 04 '14 at 14:02
  • @Dialecticus, why would you need more? the shortest way reaching the `(1, 10)` from `(0, 0)` is exactly `11` steps you cannot reach it via shorter way than this and we don't need more (however we can walk around freely before). the `abs(1-10) = 9`, so your theory looks broken to me. – holex Jul 04 '14 at 14:08
  • @holex, no it is wrong. For answer to be 11, the path taken is move up and then take all steps right and the alternate. But it is not adhering to the constraints given. – user155768 Jul 04 '14 at 14:11
  • @user155768, oh, I see now, you need an alternate path not a straight one. you are right, in that case my suggestion is not correct at all. – holex Jul 04 '14 at 14:19

2 Answers2

0

First thing to notice: the order of the moves is not significant (up, up, left is equivalent to up, left, up). So the constraint become number of horizontal moves = number of vertical moves ± 1.

You have to move to (x, y) (x + y moves) and then move along the x axis (assuming x < y) so that number of horizontal moves = number of vertical moves ± 1.

Hence the answer is x + y + 2*k such that x + 2*k = y or x + 2*k = y - 1.

Thomash
  • 6,173
  • 1
  • 26
  • 49
  • I've tried to understand your answer (for learning purpose only), and it is not quite clear to me where the `k` comes from... it looks to me as an undefined and unknown value. what is that actually? – holex Jul 04 '14 at 15:57
0

according to the statement as every left-right movement must be followed an up-down move, and visa-versa, the following formula can give you the length of the shortest path.

let us assume the x and y are positive distances what we need to walk in both direction, so

x, y ∈ ℕ+ ⋃ {0}

then

steps = min (x, y) × 2 + 4 × floor (abs (x - y) / 2) + (x + y) mod 2

where the

  • min (a, b) gives the smaller value of a and b, like min (1, 2) = 1;
  • floor (x) gives the whole part of x without fraction part, like floor(4.5) = 4;
  • abs(x) gives the positive distance of x from zero, like abs(-3) = abs(3) = 3;
  • x mod y gives the modulus (or remainder) of x / y, like 11 / 2 = 5 with remainder of 1;

example:

  • in the case of (0, 10) the steps are 20;
  • in the case of (1, 10) the steps are 19;
  • in the case of (8, 5) the steps are 15;
  • in the case of (3, 3) the steps are 6;

etc...

holex
  • 23,420
  • 7
  • 59
  • 73
  • I assume the `x` an `y` are always equal or greater than zero. that is what I inveted the formula with. if any of those can negative you need to take the `abs(...)` of that number, before you use the formula. – holex Jul 06 '14 at 09:33
  • @user, please elaborate. – holex Jul 07 '14 at 11:03