Jaap's Puzzle Page

Switcheroo / Hop-Over / Mag-Nif Switch / Leaping Frogs Puzzle

Switcheroo Switch

Switcheroo and Hop-Over are versions of the classic Leaping Frogs puzzle, or Frogs and Toads puzzle. There is a row of 9 holes, and at the start four holes on the left are filled with marbles of one colour, and the four holes on the right with marbles of another colour, which leaves only the centre hole empty. The aim is to swap the two colours using only forward moves (moving a marble forwards by one hole) and jumps (jumping one marble over another so that it goes forwards a distance of two holes).

Versions of this puzzle exist with a different number of pieces, and it can be solved using any number of each colour. In the classic puzzle literature there are usually 3 of each in the shape of frogs. It was described in "Puzzles Old and New" by Professor Hoffmann in 1893, where it goes by the name "Right and Left Puzzle". Other names are "Jumping Frog Puzzle" and "Jeu des Grenouilles".

'Switch' is a puzzle by Mag-Nif. It has a board with tracks that connect nine peg locations. There are 8 pegs (four red, four green) that can slide along the tracks. The nine locations are arranged as a row of five, with a row of four underneath it. Each row is connected by a horizontal track, and each location on the bottom row is connected by diagonal tracks to the two nearest locations on the top row. The game starts with the colours separated and the space in the middle. The aim is to swap the colours in as few moves as possible.

It may not be immediately obvious how similar it is to the leaping frogs puzzle. Consider the diagonal sections of the track together as forming one long path. The pieces can slide forwards or backwards along that path. Moving a piece along a horizontal section is equivalent to going forwards or backwards two places in the path. This makes the puzzle equivalent to the frog jumping puzzle but without the restrictions of only moving/jumping forwards.

If your browser supports JavaScript, then you can play Switcheroo / the Leaping Frog Puzzle by clicking the link below:

JavaScript Switcheroo / the Leaping Frog Puzzle

Notation:

Let a normal move to the left or right be denoted by L and R respectively. Left or right jumps will be denoted by LL and RR.

Solution:

Move numbersMovesResulting position
1 RAAA.ABBBBAAABB
A.BB
2 LLAAABA.BBBAAABB
AB.B
3 LAAABAB.BBAAA.B
ABBB
4-5 RR RRAA.BABABBA.AAB
ABBB
6 RA.ABABABBAAAAB
.BBB
7-9 LL LL LLABABABA.BAAAAB
BBB.
10 LABABABAB.AAAA.
BBBB
11-14RR RR RR RR.BABABABA.AAAA
BBBB
15 LB.ABABABABAAAA
.BBB
16-18 LL LL LLBBABABA.ABAAAA
BBB.
19 RBBABAB.AABAA.A
BBBA
20-21 RR RRBB.BABAAAB.AAA
BBBA
22 LBBB.ABAAABBAAA
B.BA
23 LLBBBBA.AAABBAAA
BB.A
24 RBBBB.AAAABB.AA
BBAA

The structure of the solution above is quite obvious, made especially visible on the board of Mag-Nif's Switch. It can easily be extended to a solution for a larger number of pieces.

Further Analysis

Let's consider the general case where there are m marbles of one colour and n of the other colour, on a board with m+n+1 holes. I'll write this case as (m,n).

Adjacency Lemma:
Two marbles of the same colour should only be adjacent if one of the following two conditions holds:
  1. Neither marble has moved - the space and all marbles of the other colour still lie ahead of them
  2. Both marbles have moved to the other side - so the space and all marbles of the other colour lie behind them
If two marbles of the same colour are adjacent and neither of the two conditions holds, then the puzzle has reached an unsolvable state.
Proof: Click to view proof.
Jump Corollary:
A marble will never jump a marble of the same colour.
Proof: Click to view proof.

The above information is enough to give you the complete solution if you can look two moves ahead. At any point during the solution there are at most two options (provided we already exclude jumping over a marble of the same colour). In the starting position both options are equally valid, and in most other cases one of the options immediately leads to a position that violates the conditions of the adjacency lemma. In the remaining cases with two options, the wrong move leads to a position where the only options left will violate the lemma. These rules for the cases with two available moves can be captured in a few simple patterns as shown below.

Two move look-ahead:
ABA.B -> ABAB.
A.BAB -> .ABAB
One option fails the lemma:
A.AB -> ABA.
AB.B -> .BAB
Both choices valid:
A.B -> AB.
A.B -> .AB
Order Corollary:
The order of the marbles of one colour remains the same.
Proof: Click to view proof.
Move Count Theorem:
In the solution to the (m,n) puzzle, there will be mn jumps and m+n normal moves.
Proof: Click to view proof.
Move Corollary:
In an (n,n) puzzle with n even, then every marble moves once and jumps n/2 times.
Proof: Click to view proof.
Backwards moves Theorem:
If the restriction on only moving pieces forwards is removed, then the standard solution is the shortest. For example, the shortest solution to the Switch puzzle is the same as the solution for the normal (4,4) puzzle.
Proof: Click to view proof.

Recursive solution for (n,n)

Now let's examine the (n,n) solution more carefully, and see how it can be built up from smaller parts.

Suppose you start with an (n, n) puzzle in the starting arrangement. There is a sequence of moves and jumps that rearranges the marbles to a position where the blank space is at the right end, the marble at the left end has not moved yet, and the row of marbles alternates in colour. Let's call this sequence of moves Tn. It is illustrated in the picture below, together with its mirror image which I will call Tn'.

A...A.B...B
Tn
AB......AB.
A...A.B...B
Tn'
.AB......AB

So now we have a position where the marbles are arranged so that the marble at one end hasn't moved, the marbles alternate in colour, with the empty spot on the other end. The n marbles of one colour (including the one that hasn't moved) can all jump forwards one by one so that you reach a similar position, but mirrored. The empty space is on the other end now, and the marbles still alternate in colour. Let's call this sequence of n jumps Sn. This, and its mirror image Sn' is shown in the next picture.

AB......AB.
Sn
.BA......BA
.AB......AB
Sn'
BA......BA.

A solution for the (n,n) puzzle consists of Tn, followed by Sn, followed by the inverse of Tn'.

A...A.B...B
Tn
AB......AB.
Sn
.BA......BA
Tn' -1
B...B.A...A

While the sequence Sn is completely known, Tn is not, so we need to break that up into smaller steps. On an (n,n) puzzle we can apply Tn-1 and Sn-1 to the board excluding the outermost marble at each end. The single move of the loose outer marble, then brings the marbles into a fully alternating position, the end result of what Tn' would have done. Therefore for n>1, Tn is broken down into Tn-1', Sn-1', and a single move. T1 is just a single move.

Tn
AA...A.B...BB
Tn-1'
A.AB......ABB
Sn-1'
ABA......BA.B
L
ABA......BAB.
Tn'
AA...A.B...BB
Tn-1
AAB......AB.B
Sn-1
A.BA......BAB
R
.ABA......BAB

With this breakdown, we can verify that this solution satisfies the move count theorem. Sn obviously consists of n jumps and no normal moves. Tn consists of one more move and n-1 more jumps than Tn-1. Since T1 has only one normal move, it is easily seen that Tn consists of n normal moves and 0+1+2+3+...+(n-1) = n(n-1)/2 jumps. The complete solution consists of one Sn and two Tn, so has 2n normal moves and n + 2*n(n-1)/2 = n2 jumps, as expected.

MovesJumps
Tn or Tn'nn(n-1)/2
Sn or Sn'0n
Solution (n,n) = 2Tn+Sn2nn2

Recursive solution for (m,n), m<n

If the number of marbles of each colour are different, then we can do a very similar breakdown. Let's assume that the colour with the fewest marbles (m marbles) starts on the left. We can apply Tm to it, simply by ignoring the rightmost n-m marbles. This time it does make a difference if it is done in mirror image or not, but both lead to a solution.

A...A.B...B B...B
Tm
AB......AB. B...B
A...A.B...B B...B
Tm'
.AB......AB B...B

So we now have two possible arrangements of 2m alternating marbles and the space, with n-m marbles of the more abundant colour on the right. Applying Sm or Sm' followed by a single move shifts the arrangement one space to the right, past one of the abundant marbles. By doing this n-m times, the alternating arrangement reaches the right hand side.

AB...AB. B
Sm
.BA...BAB
L
B .A...BAB
.AB...AB B
Sm'
BA...BA.B
L
B A...BAB.

At this point we just have to finish this in the same way as the normal (m,m) case, by doing Sm followed by the inverse of Tm' (or Sm' followed by the inverse of Tm).

Again we can verify that this solution satisfies the move count theorem. It consists of two applications of Tm, n-m+1 applications of Sm, and n-m normal moves. That gives 2m+n-m = m+n normal moves and 2m(m-1)/2 + (n-m+1)m = mn jumps, as expected.

It is interesting to note that the sequence of moves and jumps is palindromic if we ignore their directions. The beginning Tm matches the final Tm-1 (or Tm' -1), and in between those we have a sequence of applications of Sm/Sm' interspersed with single moves which is also palindromic.