Monday, 13 April 2020

River crossing problems and two-dimensional mazes

River crossing problems, like the famous problem of the wolf, sheep and cabbage, are, at first sight, completely different from solving two-dimensional mazes. If you look a little deeper, you can quickly see that both problems simply reduce to finding hamiltonian paths inside of graphs, so their similarity is quite obvious, but today I am not going to talk about the graph-theoretical representations of both problems. I am going to demonstrate a technique to turn a river crossing problem into a 2-dimensional maze.

The wolf, goat and cabbage


According to Wikipedia, this problem can be described as follows:
Once upon a time a farmer went to a market and purchased a wolf, a goat, and a cabbage. On his way home, the farmer came to the bank of a river and rented a boat. But crossing the river by boat, the farmer could carry only himself and a single one of his purchases: the wolf, the goat, or the cabbage. If left unattended together, the wolf would eat the goat, or the goat would eat the cabbage. The farmer's challenge was to carry himself and his purchases to the far bank of the river, leaving each purchase intact. How did he do it?
Now, the solution can be found on the same Wikipedia article, but how can we reformulate this problem as a maze. At first one can notice that a wolf and a cabbage are completely similar in this riddle. Because in essence, it does not matter who eats who, the thing that matters is that they both cannot be left alone with a goat. So the problem could be restated as a farmer with a goat and two cabbages.
Now that this is out of the way, let us draw a grid, where the rows represent the number of goats left on the left bank of the river, and the columns the number of cabbages and wolfs. Now split each cell of this grid in two triangles, where the upper triangle means the boat is on the right side, and the bottom triangle means it is on the left.

If we state the problem like this, we can interpret the problem as a pawn, roaming around on the triangles of this grid, where each triangle represents a given state of the problem, so a given location of both the boat, as the wolf, goat and cabbage.
Here crossing a horizontal line means sailing to the other side with the goat, crossing a vertical line means crossing with a wolf or cabbage, and crossing a diagonal line means crossing alone. If we then mark all the tiles where the goat eats a cabbage or is eaten in red, we can get a nice maze, that can be solved easily:
Each crossing of one of the lines is a certain action. If we follow the blue line, we see that the following actions have to be taken:
  1. Cross with goat
  2. Cross alone
  3. Cross with wolf (or cabbage)
  4. Cross with goat
  5. Cross with Cabbage (or Wolf)
  6. Cross alone
  7. Cross with goat
What is also the correct solution to the problem.

the two children and two adults

A similar river crossing problem is the following one:
Two children and two adults have to cross a river, and there is only one rowing boat. A boat can carry one adult or two children. The boat cannot sail on its own. How do the two children and adults get to the other side?
Using the solution as before, one can make a grid with the following allowed moves:
Note that the allowed moves do not align with the grid anymore. This does not disallow us from solving the problem, as the solution equals this:
But it is way less obvious. Can't we reshape the grid so we can only move to adjacent squares? Well, if we move all the white tiles one unit to the right, and in addition, move the second row one tile to the right and the third row 2 tiles, then the grid is reshaped as:
Where again, crossing a vertical line means 2 children cross the river, crossing a diagonal means one child crosses the river, and crossing a horizontal line means an adult crosses the river. This gives rise to the following solution:
As before, this can be transformed back to a valid solution:
  1. 2 Children cross
  2. 1 Child goes back
  3. 1 adult crosses
  4. Other child goes back
  5. 2 Children cross
  6. 1 Child goes back
  7. Other adult crosses
  8. Other child goes back
  9. 2 Children cross
So the children will have to do a lot of rowing.

Evil Apprentices

Three mages and three apprentices arrive at a river. There is one boat that can carry two people. The apprentices will do as the mages ask, even when left alone, as they want to stay in the good graces of the mages. But the apprentices are also opportunistic, if at any point, the apprentices outnumber the mages at any side, they will attack the mages and take over. How can the mages get everyone to the other side?
This problem is equivalent to this problem
The exact solution is similar, but now it is not possible anymore to convert the maze to a system where all possible moves correspond to adjacent tiles. But we can still optimise the layout by rotating all the tiles and shifting the blue tiles one unit down and right. This gives the following solution:
Here, crossing the diagonal line is crossing with one mage and one apprentice, the horizontal and vertical lines are a mage and apprentice crossing alone, and crossing the corners of the triangles is crossing with two mages or two apprentices. Solving the maze is a bit harder, but still quite straight forward.

Evil Apprentices 2

You are a Wizard. you arrive with 3 apprentices and 4 bodyguards at a large canyon. You conjure a large floating disk, which you can use to transfer yourself and maximal one other person to the other side. You have to stay on the disk yourself, as your apprentices are not powerful enough to keep the disk afloat themselves. But if you take everyone to the other side one by one, you have a problem. Each of your apprentices is waiting for an opportunity to become wizard themselves, and overthrow you. They will not try anything if there is another apprentice there, because they do not want to share the glory, or if there are at least 3 guards present. How do you get with all your apprentices and guards to the other side?
There is a lot to unpack here, but notice that this is quite easy when you consider it as a mage. You do not have to transform the mage or anything, as you can only bring one unit at a time. The solution is drawn below.
In other words, bring your guards to the other side until you have 3 of them there, then bring one apprentice to the right, bring a guard back, transfer another apprentice, bring another guard back, move the last apprentice, and then carry the remaining guards to the other side. Easy.