Wednesday, June 22, 2011

4 Locks on a Treasure Chest

Problem: A round treasure chest has 4 locks. The distance between any adjacent locks are 90 degree. You can't see the status of the locks because they are inside. You can only touch them and set them. Every time, you can chose to touch any two locks and set them. A lock can be set to either 1 or 0. If all the four locks are set as 1 or 0, the chest will open. Once you had set two locks, the round chest will rotate. The rotation distance is random. Is there a way you can guarantee to open the chest?

Solution: The initial status is unknown, but definitely it is not 4 "1" or "0".  Try to think from the last step (under what condition, you can definitely open the box with one more move). We can do as follow:

  1. Set two adjacent locks to "0", if still not open, go to step 2.
  2. Set two locks that are opposite to each other to "0", if still not open, go to step 3.
  3. If you reach here, it means there is one "1" and three "0". Try two adjacent locks, if one is '1" and one is "0", Bingo! Set the "1" to "0". But if you encounter two "0", you need to set one of them to "1". Then there are two cases: a) "1 0 1 0"  and b) "1 1 0 0". Let's go to step 4.
  4. if it is a), then we try to set two locks that are opposite to each other to either "0" or "1". We are done! If it is b), we still try to set two locks that are opposite to each other. Basically, we just toggle them (or do nothing). Then step 5.
  5. So we still have  "1 1 0 0". This time try to set two adjacent locks, if we encounter two "1" or two "0", we are done. If not, we toggle them. Then we have "1 0 1 0". Then go to step 5.
  6. Try to set two locks that are opposite to each other, we got the treasure.
Extension Question: One condition in previous problem is actually redundant. Say you can't feel the lock (you can't tell the status of the lock) and you can just toggle it. Every time you can toggle 1 or 2 locks. Still, we can solve it in 7 steps:
  1. Toggle two opposite ones.
  2. Toggle two adjacent ones.
  3. Toggle two opposite ones.
  4. Toggle a random one.
  5. Toggle two opposite ones.
  6. Toggle two adjacent ones.
  7. Toggle two opposite ones.
Generally, if the initial status is unknown, there are two type of cases: 1) two "1" and two "0" 2) three "1" (or "0") and one "0" (or "1"). For the case 1, setp 1~3 can guarantee we finish the game. Step 4 is to handle case 2). After step 4, we are facing case 1), so just repeat the three steps again. 

    No comments:

    Post a Comment