Saturday 20 September 2014

Specific Solutions for the Broken Combination Lock problem

So how to come up with an actual solution for an n x n x n cube? (Assume n is odd, it's easier/symmetrical if even). At a high level:

  1. Pick a corner (let's say, starting at (1,1,1)) of a cube of size (n+1)/2 that is in a corner of the overall large cube), and solve according to below, then
  2. Pick the 'opposite' cube (the opposite corner of the overall large cube - say, starting at ((n+1)/2,(n+1)/2,(n+1)/2)) which will be a cube of size (n-1)/2), and solve it as below too.
  3. The combination is the total solution. 

So let's do it. Starting at one corner (for simplicity, assume (1,1,1) then keeping 'z' constant, for each guess add 1 to each of x and y a total of (n+1)/2 times (or (n-1)/2 times for the second cube...let's just call it 'N' for now).
Then add 1 to z and start x,y at x+1, y+0 from the starting point of your first guess at z=1. Continue adding 1 to each of x and y, 'rolling over' if you go over N.
Continue for each z until z= N, starting each level with x at 1 more than the previous level.
So for n=7, a solution is:
(1,1,1), (2,2,1), (3,3,1), (4,4,1),
(2,1,2), (3,2,2), (4,3,2), (1,4,2),
(3,1,3), (4,2,3), (1,3,3), (2,4,3),
(4,1,4), (1,2,4), (2,3,4), (3,4,4),
- the above handles the cube of size (n+1)/2
(5,5,5), (6,6,5), (7,7,5),
(6,5,6), (7,6,6), (5,7,6),
(7,5,7), (5,6,7), (6,7,7)
- handles the rest

Two notes initially:
1. The selection of an (x,y,z) covers all guesses (x,y,?), (x,?,z), and (?,y,z) where ? represents any value from 1 to n
2. The guess can be seen to be within a 'sub-cube' of m x m x m (where 'm' = max value of x,y,z).

(sidebar) To help viusalize, imagine initially that n is even, and you divide the cube into 8 equal 'sub-cubes' of size n/2 x n/2 x n/2 by cutting the larger cube along the lines of symmetry. (See below...also the second diagram in my previous post)

a) The selection of an (x,y,z) within a 'sub-cube' covers guesses in 4 of the 8 'sub-cubes' (the one in which the selection is made, plus the three others that contain the same 'x and y', 'x and z' and 'y and z'...in other words, the volume shown within the red lines above), but will never cover any guesses in the other 4 'sub-cubes' (symmetrical).
b) The minimum number of guesses to cover the selected 'sub-cube' (and the 3 other sub-cubes affected) must be equal to the area of one side of the 'sub-cube' (n/2)**2, since each guess can only cover one square in this cross-sectional area.
c) To complete the overall cube, you must also cover the other 4 'sub-cubes' that are 'opposite' from your first guesses. As noted, this can be done in (n**2 / 4) guesses by selecting the 'sub-cube' that contains (n/2+1,n/2+1,n/2+1) through (n,n,n). The selection of guesses in any other 'sub-cube' will, by definition, not complete the cube.

Now, back to the general proof. The 'sub-cube' selected (for your x,y,z guess) does not have to be of size n/2. In fact, the cube can be solved by solving any two 'sub-cubes' where one is of size 'i' (i<=n) (starting at 1,1,1 for simplicity) and the other of size n-i which is 'opposite' to the first 'sub-cube' within the large cube. In these cases, the same '3-dimensional L' visualization applies (although the big cube is not divided into 8 sub-cubes now, just 2 sub-cubes of different size plus the other volumes that get 'filled' as you solve each sub-cube). The solution is
S = solution for first sub-cube + solution for second sub-cube
 = i**2 + (n-i)**2
 = n**2 -2ni +2(i**2)
==> if n is even, this is optimal (minimum) at i=n/2

If n is odd (actually, even is just a special case of odd)
S = solution for first sub-cube + solution for second sub-cube
 = i**2 + (n-i)**2 (where i <> n-i)
 = n**2 -2ni +2(i**2)  (as before)
==> this is optimal (minimum) at |n-2i|=min, or in other words, when the two sub-cubes are closest in size.

This comes down to the familiar equations of
S = n**2 / 2   (for n=even)
S = (n**2+1) / 2  (for n=odd)

No comments:

Post a Comment