This is a supplemental to Kendall's answer that proves its correctness:
Because there are four bolts, each with two possible positions, there are 24=16 states. You might think we'd want to categorize the states by the number of bolts that are currently blocking one of the doors, but that won't really help us. What we want is to group all possible states by how they react to a button press and what state it leaves them in.
I'd suggest that instead of thinking of the bolts being arranged vertically, we think of them as being arranged in a circle:
1
4 2
3
So now the buttons have these behaviors:
- Button A: slide a random bolt
- Button B: slide two adjacent bolts
- Button C: slide two opposite bolts
This makes it easier to see what our states should be
- State 1: All in one door. This lets us leave, so we wouldn't be pushing buttons anymore.
- State 2: One bolt (I'll call it the odd one) is different from the others
- State 3: Two bolts are in each door, with the two bolts in a door being next to each other
- State 4: Two bolts are in each door, with the two bolts in a door being opposite each other
Now consider what can happen at each state (other than 1) when a button is pushed:
- State 2:
- Button A:
- We get lucky and the odd bolt slides, taking us to state 1
- A bolt next to the odd bolt slides, taking us to state 3
- The bolt opposite the odd bolt slides, taking us to state 4
- Button B:
- The odd bolt and a bolt next to it slide, leaving us in state 2
- Two other bolts slide to join the odd bolt, still leaving us in state 2
- Button C:
- The odd bolt and the bolt opposite it slide, leaving us in state 2
- The two bolts next to the odd bolt slide, leaving us in state 2
- State 3:
- Button A:
- One of the pairs will be disrupted, taking us to state 2
- Button B:
- We could get lucky and have one of the pairs slide, taking us to state 1
- One from each pair will slide, taking us to state 4
- Button C:
- One from each pair will slide, leaving us in state 3
- State 4:
- Button A:
- One pair will be disrupted, taking us to state 2
- Button B:
- One from each pair will slide, taking us to state 3
- Button C:
- One of the pairs will slide, taking us to state 1
So based on this, here's the state transition matrix:
|
A |
B |
C |
2 |
134 |
2 |
2 |
3 |
2 |
14 |
3 |
4 |
2 |
3 |
1 |
Note that this is exactly the transition matrix Kendall came up with, with the row for state 1 removed. Also, we see that if we are in state 4, then pushing button C will always get us out, and if we are in state 2 or 3 then pushing the button doesn't change what state we are in. Also, if we are in state 2, then pushing either B or C will not change what state we are in.
Suppose we were told that we were in either state 3 or 4. What would we do? Well, if we push button C we will get out if we were in state 4. If we're still not free, then we'd know that we had been in state 3, and since button C doesn't affect that then we know we are still in state 3. Then we could push button B, either freeing us or taking us to state 4. From state 4 we can push button C again to get free.
So if we know we are in state 3 or 4, we should push C, then B, then C (checking after each button push). This guarantees that we will be able to get out by the third button push at the latest.
What if we did that while we were in state 2? Since buttons B and C don't change us out of state 2, we would still be in state 2! So if we don't know what state we are in, we push CBC and are either free, or started in state 2. If we started in state 2 then we would still be in state 2. Then, we can push button A. Pushing button A from state 1 will take us to one of the other states. If we aren't free, then we'll know we are now in state 3 or 4. We covered this already though, so we know we should hit CBC again and we'll be free!
So that's why the pattern CBC A CBC will always allow us to escape. Just remember to check after each push, because the first press of the button C might be what allows us to escape!