My Informatics teacher gave me an interesting problem to solve, it's more like a logical one, but anyway.
Let's say we have elements that have 2 properties: weight an label.
The weight is always more than 0 and there are 3 possible labels: A, B, C.
If we have a list of those elements "1B 2B 3C 4A 5C" it's always sorted by weight in ascending order. There are label sub groups in each list (1B and 2B are part of the B label group). In each label group there is a max and a min element (3C is min, 5C is max in C label group). Elements can switch labels only if they are the max in their group and will be again the max element in the new label group.
So my teacher asked us on how are we going to switch all elements to a single label group?
That is relatively easy if we have a list of 2 element's let's say "1B 2B" and we want to switch to the A label group. The list of actions is as follows:
2B -> 2C
1B -> 1A
2C -> 2A
In this order of actions the constraints for being a max element in it's current and future label group are kept. My problem is when I've more elements and they are not from the same group - "1B 2B 3C 4B 5A 6C". If I try moving everything to the A label group, I think it's not possible or am I missing something?
My question is if there is something similar in puzzle solving that I can apply the logic from, so that it's robust algorithm and even being able to say that the action is not possible. I will have to write it in C++ code, but that's my problem and is relatively easy if I've an algorithm or at least an idea.
Grouping puzzle
2 Answers
This is the Tower of Hanoi problem with the lightest elements representing the largest disks. You have to do what is needed to move 1B to A, which means all the other elements must have a C label at that time. To do that you need to move element 2 to C, so all the higher weights have to have an A label. This suggests a recursive algorithm:
Is element 1 in the right place?
If yes, leave it and do the algorithm starting from element 2.
If no
- move all the rest of the elements to the other label
- move element 1to the right label
- move the rest of the elements to the right label.
-
0Yes, this seems like something like it. Thank you. – 2017-02-08
Hint:
This is indeed a variant of the Hanoi Tower, which is solved using the elementary logics: to move the tower from A to B, move the tower but the last disk from A to C, move the last disk to B and move the partial tower back from C to B.
This may sound like a circular procedure, but you perform it recursively and as the tower to be processed loses one disk every time, the moving has an end.
To move N disks from A to B:
if N = 0
done
Move N-1 disks from A to C
Move one disk from A to B
Move N-1 disks from C to B
This version of the problem is slightly more complicated than the canonical one where you start with the whole tower in A.
A possible solution would be to simulate the moving of a complete tower until you reach the given configuration, then continue with real moves.
-
0The main problem here is the simulation of movement, considering I might have disks in all 3 towers that are not grouped and the actual "simulation" is the hard part and the spare tower is not empty. – 2017-02-10