Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • You can only move one unit at a time, so many times you have to retrace your path I think Commented Feb 9, 2013 at 20:56
  • I don't really follow you.It seems that the requirement is just to move forward and swap numbers.You already know the current location and the target location.Just swap them (using the cart variable as you say it) and move to the next pair Commented Feb 9, 2013 at 20:57
  • Consider this counterexample: (1, 10), (10, 1), (2, 3), (3, 4). The optimal way to do this would be to carry object 1 to position 10, then pick up the object at position 10 and carry it to position 1, then to carry the 2 to the 3 and the 3 to the 4. Doing this in sorted order of starting position would carry the 1 to the 10, then back all the way up to the start to carry the 2 to the 3, the 3 to the 4, then go all the way to the end to pick up the 10 and bring it back. Commented Feb 9, 2013 at 20:58
  • @templatetypedef:I see what you mean.Updated answer Commented Feb 9, 2013 at 21:13
  • In your updated answer, does the "current index" just indicate the current position? Commented Feb 9, 2013 at 22:31