algorithm to traverse 3D coordinates without revisiting
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 algorit开发者_运维百科hm 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
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.
Maybe you can generalize Gray Codes, which seem to solve a special case of the problem.
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 * . . .
精彩评论