puzzle on parks

325 Views Asked by At

A park contains paths that intersect at various places. The intersections all have the properties that they are 3-way intersections and that, with one exception, they are indistinguishable from each other. The one exception is an intersection where there is a restaurant. The restaurant is reachable from everywhere in the park. Your task is to find your way to the restaurant.

The park has strict littering regulations, so you are not allowed to modify the paths or intersections (for example, you are not allowed to leave a note an intersection saying you have been there). However, you are allowed to do some bookkeeping on a pad of paper that you bring with you at all times (in the computer-science parlance, you are allowed some state). How can you find the restaurant?

You may assume that once you enter an intersection, you can continue to the left, continue to the right, or return to where you just came from.

1

There are 1 best solutions below

3
On

Enumerate all finite strings over the alphabet $\{L,R\}$ (or at least an infinite subset that contains all fintite strings as prefixes). For each string $a_1\cdots a_n$ perform the following move sequence: $a_1,\ldots,a_n,\text{turn around},\overline{a_n},\ldots, \overline{a_1},\text{turn around}$ (where $\overline R=L$ and $\overline L=R$). Each such group will take you back to your startng position. By assumption a path to the restaurant occurs as a prefix of one of tthe strings enumerated, hence sooner or later you run tinto the restaurant.

It is a nice exercise to do this with only memory for two integers plus one bit flag on your scratchpad:

  1. Let $a\leftarrow 1, b\leftarrow 1, f\leftarrow 0$

  2. If $f=0$, let $a\leftarrow a+1$.

  3. If we are at the restaurant, terminate. Otherwise follow the road to the next intersection

  4. If $a=1$, turn around, let $a\leftarrow b$, $b\leftarrow 1$, $f\leftarrow 1-f$ and go to step 2

  5. If $a$ is even, turn left, let $a\leftarrow a/2$, $b\leftarrow 2b+1$ and go to step 3.

  6. turn right, let $a\leftarrow (a-1)/2$, $b\leftarrow 2b$ and go to step 3.