views:

94

answers:

3

Say we have a set of 3D (integer) coordinates from (0,0,0) to (100,100,100) We want to visit each possible coordinate (100^3 possible coordinates to visit) without visiting each coordinate more than once.

The sum of the differences between each coordinate in adjacent steps cannot be more than 2 (I don't know if this is possible. If not, then minimized) for example, the step from (0,2,1) to (2,0,0) has a total difference of 5 because |x1-x2|+|y1-y2|+|z1-z2| = 5

How do we generate such a sequence of coordinates?

for example, to start: (0,0,0) (0,0,1) (0,1,0) (1,0,0) (1,0,1) (0,0,2) (0,1,1) (0,2,0) (1,1,0) (2,0,0) (3,0,0) (2,0,1) (1,0,2) (0,0,3) etc...

Anyone know an algorithm that will generate such a sequence to an arbitrary coordinate (x,y,z) where x=y=z or can prove that it is impossible for such and algorithm to exist? Thanks

Extra credit: Show how to generate such a sequence with x!=y!=z :D

A: 

Maybe you can generalize Gray Codes, which seem to solve a special case of the problem.

Yuval F
A: 

Seems trivial at first but once started, it is tricky! Especially the steps can be 1 or 2.
This is not an answer but more of a demostration of the first 10+ steps for a particular sequence which hopefully can help others to visualise. Sid, please let me know if the following is wrong:

s = No. of steps from the prev corrdinates
c1 = Condition 1 (x = y = z)
c2 = Condition 2 (x!= y!= z)
(x,y,z) s c1 c2
---------------
(0,0,0)   *    (start)
(0,0,1) 1 
(0,1,0) 2
(1,0,0) 2
(1,0,1) 1
(1,1,0) 2
(1,1,1) 1 *
(2,1,1) 1
(2,0,1) 1    *
(2,0,0) 1
(2,1,0) 1    *
(2,2,0) 1
(2,2,1) 1
(2,2,2) 1 *
(2,3,2) 1 
(2,3,3) 1 
(3,3,3) 1 *
(3,3,1) 2 
(3,2,1) 1    *
(3,2,0) 1    *
  .
  .
  .
o.k.w
Hmm, I don't know if that will work because you missed some coordinates (such as 0,1,1) and I don't know if it is possible to double back after to reach them, but yea, it is that sort of idea
Sid Ayden
@Sid: I know I missed some coordinates, thats the issue I want to highlight. In another sequence, I can always use step always 1 like what mjv did. There are many possible sequences from one point to another even if it's using step = 1 not to mention 2.
o.k.w
+4  A: 

One of the tricks (there are other approaches) is to do it one line [segment] at a time, one plane [square] at a time. Addressing the last part of the question, this approach works, even if the size of the volume visited is not the same in each dimension (ex: a 100 x 6 x 33 block).

In other words:

Start at (0,0,0), 
move only on the Z axis till the end of the segment, i.e.
(0,0,1), (0,0,2), (0,0,3), ... (0,0,100), 
Then move to the next line, i.e.
(0,1,100)
and come backward on the line, i.e.
(0,1,99), (0,1,98), (0,1,97), ... (0,1,0), 
Next to the next line, going "forward"

And repeat till the whole "panel is painted", i.e ending at
... (0,100,99), (0,100,100),
Then move, finally, by 1, on the X axis, i.e.
(1,100,100)
 and repeat on the other panel,but on this panel going "upward"
etc. 

Essentially, each move is done on a single dimension, by exactly one. It is a bit as if you were "walking" from room to room in a 101 x 101 x 101 building where each room can lead to any room directly next to it on a given axis (i.e. not going joining diagonally).

Implementing this kind of of logic in a programming language is trivial! The only mildly challenging part is to deal with the "back-and-forth", i.e. the fact that sometimes, some of the changes in a given dimension are positive, and sometimes negative).

Edit: (Sid's question about doing the same diagonally):
Yes! that would be quite possible, since the problem states that we can have a [Manhattan] distance of two, which is what is required to go diagonally.
The path would be similar to the one listed above, i.e. doing lines, back-and-forth (only here lines of variable length), then moving to the next "panel", in the third dimension, and repeating, only going "upward" etc.

(0,0,0) (0,0,1)
(0,1,0)                  first diagonal, only 1 in lengh.
(0,2,0)                  "turn around"
(0,1,1) (0,0,2)          second diagonal: 2 in length
(0,0,3)                  "turn around"
(0,1,2) (0,2,1) (0,3,0)  third diagonal: 3 in length
(0,4,0)                  turn around
etc.

It is indeed possible to mix-and-match these approaches, both at the level of complete "panel", for example doing one diagonally and the next one horizontally, as well as within a given panel, for example starting diagonally, but when on the top line, proceeding with the horizontal pattern, simply stopping a bit earlier when looping on the "left" side, since part of that side has been handled with the diagonals.
Effectively this allows a rather big (but obviously finite) number of ways to "paint" the whole space. The key thing is to avoid leaving (too many) non painted adjacent area behind, for getting back to them may either bring us to a dead-end or may require a "jump" of more than 2.

mjv
@mjv: this will work, but do note there's a clause of the stepping can be 1 or 2, that's tricky (for me at least). From (0,0,0), you can move to (0,1,1), (1,0,1), (1,1,0). Not to mention from (2,2,2), you can move to (2,1,1), (0,2,2), (1,2,3)......... (i gave up)
o.k.w
Thanks, this worked. Also, I'm wondering if it's possible to do the same diagonally, where the sum of the coordinates would increment for each block of coordinates in the sequence, like the small example I gave in the question.
Sid Ayden
@Sid: At any given point within the space other than corners and edges, the point can move to 8 (adjecent neighbours) + 6 (2 steps non-diagonal) = 14 other possible points. I don't have to do the math do 'feel' the complexity.
o.k.w
@Sid, see my edit which shows how going diagonally is possible, and in fact mix-and-matching diagonals and horizontal.
mjv
Good effort, mjv! +1 : )
o.k.w
Thanks mjv, I understand this pattern now. Your explanation helped me alot!
Sid Ayden