Variation on the Towers of Hanoi (Twin-Towers) - algorithm

I am trying to solve a Variation of the Towers of Hanoi. In this case I have two towers, same height and the disks have the same sizes. I can stack disks onto each other as long as they are the same size or smaller. The color does not factor in to the stacking capabilities.
I have three pegs and two towers and the task is to swap both towers.
My initial approach is to build one tower with alternating colors and then going backwards with just a different peg to move to.
I am just thinking that this is not the most elegant solution. Is there a better way to do this?
Update:
I thought I was pretty close to figuring this out, but I wasn't. I have all the moves (for n=3) on paper and it looks pretty similar to the original algorithm just that lots of moves are done twice. Unfortunately I am not able to put this into a recursive algorithm. This is pretty frustrating. Does someone have an idea?

There is a website that addresses various versions of the Tower of Hanoi.
Also, if you want to do this recursively, you don't actually need to know what is going to happen. You can just try all available moves recursively until you reach the solution.

Related

Solving the sliding puzzle-like problem with arbitrary number of holes

I've tried searching for a while, but haven't come across a solution, so figured I would ask my own.
Consider an MxM 2D grid of holes, and a set of N balls which are randomly placed in the grid. You are given some final configuration of the N balls in the grid, and your goal is to move the balls in the grid to achieve this final configuration in the shortest time possible.
The only move you are allowed to make is to move any contiguous subsection of the grid (on either a row or column) by one space. That sounds a bit confusing; basically you can select any set of points in a straight line in the grid, and shift all the balls in that subsection by one spot to the left or right if it is a row, or one spot up or down if it is a hole. If that is confusing, it's fine to consider the alternate problem where the only move you can make is to move a single ball to any adjacent spot. The caveat is that two balls can never overlap.
Ultimately this problem basically boils down to a version of the classic sliding tile puzzle, with two key differences: 1) there can be an arbitrary number of holes, and 2) we don't a priori know the numbering of the tiles - we don't care which balls end up in the final holes, we just want to final holes to be filled after it is all said and done.
I'm looking for suggestions about how to go about adapting classic sliding puzzle solutions to these two constraints. The arbitrary number of holes is likely pretty easy to implement efficiently, but the fact that we don't know which balls are destined to go in which holes at the start is throwing me for a loop. Any advice (or implementations of similar problems) would be greatly appreciated.
If I understood well:
all the balls are equal and cannot be distinguished - they can occupy any position on the grid, the starting state is a random configuration of balls and holes on the grid.
there are nxn = balls + holes = number of cells in the grid
your target is to reach a given configuration.
It seems a rather trivial problem, so maybe I missed some constraints. If this is indeed the problem, solving it can be approached like this:
Consider that you move the holes, not the balls.
conduct a search between each hole and each hole position in the target configuration.
Minimize the number of steps to walk the holes to their closest target. (maybe with a BFS if it is needed) - That is to say that you can use this measure as a heuristic to order the moves in a flavor of A* maybe. I think for a 50x50 grid, the search will be very fast, because your heuristic is extremely precise and nearly costless to calculate.
Solving the problem where you can move a hole along multiple positions on a line, or a file is not much more complicated; you can solve it by adding to the possible moves/next steps in your queue.

Alternate plain English algorithm for Towers of Hanoi

There are four plain English algorithms for the Towers of Hanoi Puzzle available on Wikipedia, but when I was first solving the puzzle, I came up with an algorithm that is different from any of the solutions I have seen.
Wikipedia algorithms:
Iterative solution
Simpler statement of iterative solution
Equivalent iterative solution
Recursive solution
Of course the results of the algorithms are the same, and they are really just different ways of thinking about the same thing, but I am talking about plain English ways of describing the process.
My process goes like this:
Never move same tile twice in a row(obviously)
Prioritize moving right
When moving right, move to the closest pole that can be legally moved to.
When moving left, move to the farthest pole that can be legally moved to.
..
These rules differ from other descriptions of the algorithm in that:
The initial stack can be placed on any of the 3 pillars and still work without any adjustment to the rules needed.(Unlike solutions 2 and 3 and 4)
You don't have to number the disks(Unlike solutions 1 and 3 and 4)
I have tested this programmatically, and it always solved the puzzle in (2^n)-1 moves where n is the number of rings.
It seems to me that my description really is different from the other plain English descriptions I have found. Has any one seen this description before? If so, please show reference.
I think your description is pretty much the same as Iterative Solution. Just imagine the posts arranged around a circle or a triangle, mod 3 style. Your instructions and Wikipedia's instructions translate to the same thing in that way of viewing things.
This solution is a unidirectional version of the first iterative solution.
The difference between the unidirectional solution and the mono-directional version is the unidirectional solution doesn't specify an end position.
A simple solution for the toy puzzle: Alternate moves between the
smallest piece and a non-smallest piece. When moving the smallest
piece, always move it to the next position in the same direction (to
the right if the starting number of pieces is even, to the left if the
starting number of pieces is odd). If there is no tower position in
the chosen direction, move the piece to the opposite end, but then
continue to move in the correct direction. For example, if you started
with three pieces, you would move the smallest piece to the opposite
end, then continue in the left direction after that. When the turn is
to move the non-smallest piece, there is only one legal move. Doing
this will complete the puzzle in the fewest number of moves.
This description of the mono-directional version can be changed to be unidirectional if direction choices of direction are replaced with the rules from the unidirectional solution revolving around prioritizing moving right.

I am looking for an algorithm to solve a "sliding puzzle", the kind of puzzle where you have an image broken into 16 pieces in a 4x4 frame

I am looking for an algorithm to solve a "sliding puzzle", the kind of puzzle where you have an image broken into 16 pieces in a 4x4 frame, that you try to reassemble for a scrambled starting position.
This particular puzzle has a restriction, the rows move only to the right (and wrap around), the whole row at once and the columns move only up (and wrap around), the whole column at once, both in one-tile steps.
Is the math too complex?
Anyone has experience with this kind of problem?
This link will provide you the answer. They talk about the different distance functions used by the heuristic. A* is simpler to find open source implementations of.
as for pretty much any problem, one "easy/simple" method to solve such a problem is to represent puzzle states as a graph, and use a graph search / path finding algorithm (DFS,BFS,Dijkstra,A*,etc.). Maybe there is some genius special algorithm that fits this problem better, but you would probably need quite a lot of insight to get better than A* / bidirectional dijkstra.

Cutting Heuristic Solving Algorithm for n-edged polygone

I want to programm a tool that can place objects on a rectangle with the minumum of waste, this problem is also known as the cutting problem.
So i looked around to find some algorithms and i found out there are a few for rectangles but not that much for n-edged polygones.
my first approach was to get a bounding box for the polygone, then run the normal rectangle algorithm. After that you cound slowly try to increase the number of edges but still have only isometric lines (only vertical and horizontal), to approximate the polygone.
I wonder if there is any good algorithm that implement such thing, but is more common than create my own stuff.
the other way ive come up with could be something with two dimensional knapsack and some sorting heuristics that sort the best fitting polygones and try to put them on the rectangle.
But all i come up with has some good detection of special polygones (such as a square or normal rectangle) but does not work on common polygones.

Towers of Hanoi variation pseudocode

This is a variation to the original Towers of Hanoi problem. The same rules apply but instead of having just one stack of n disks there are two. One stack of red disks on the left pole and another stack of purple disks on the right. The final configuration should be the purple on the left and red on the right. There are a total of 3 poles.
I'm having trouble understanding/creating the pseudocode for an algorithm that solves this problem. Please help.
The problem as you have presented it is not generally solvable. According to wikipedia, the most trivial multi-stack game has two stacks and four poles, and in general there are twice as many poles/pegs as stacks.
In the 2 stacks x 3 poles case you can see fairly quickly that for n > 1 you can't get very far. The smallest two discs occupy the top of two or one poles, and you can therefore never swap the second-smallest two discs since that always requires one temporary pole.
Since this is homework so giving you the answer would be wrong, I would suggest that you graphically solve the Towers of Hanoi problem.
Then, add in the modification, and see how your original solution works with the new change.
You should see where the original may fail, but it would be easier to make the modification while looking at it graphically.
If you pick a language that is easy to do graphic solutions then you can do this very fast.
For example, GWT may be a good choice, or Rails, though TCL/TK would also be easy.

Resources