Simple 2D rubiks cube algorithm.

This is a discussion on Simple 2D rubiks cube algorithm. within the Game Programming forums, part of the General Programming Boards category; I'm trying to write a program that transforms a user given pattern into a standard pattern as follows:
Standard:
1 ...

from which the computer program attempts to transform it into the standard form. In this case, the moves required are: BCABCCB.

Now, the issue for me is not to write it in C, but rather to come up with a suitable algorithm for solving the puzzle. I've tried a few methods but they didn't work, so I'm hoping someone could perhaps give me a hint?

I realize I should have clarified the different transformations from the beginning. Anyway:

Start:
1 2 3 4
8 7 6 5

A (switch rows):
8 7 6 5
1 2 3 4

B (move each square right one step):
4 1 2 3
5 8 7 6

C (rotate mid 4 clockwise):
1 7 2 4
8 6 3 5

You're right I should probably try and find an algorithm by pen and paper first. As for helping out with studies, this is actually an optional assignment which carries no extra credit whatsoever, I just found it very interesting and would love to find a solution for it.

Well, I'm only batting .333 for the moves, but yeah, paper and pencil. If you don't know how to do it by hand, you have little chance of just sitting down at the computer and getting some muse to come through for ya.

I love your spirit - that's just the kind of thing that good solutions feed off of. I don't know the solution myself, but now I've got an idea.

My initial thought was to brute force permutations to get the solution, but I don't know if this is really feasible. I mean, if we assume that no more than 10 transformations in a row are needed to solve any given puzzle, this gives 3^10 = 59,049 different combinations of A, B and C to test out. If a solution is found after, say, the 7 first transformations of a given permutation of 10, we break; or something similar. Sounds extremely clumsy though

I'd look for some short term goals - steps if you will - leading you closer and closer to the final solution.

That's how the real rubik's cube can be solved, and this is similar, although easier, imo. AFAIK, in the real cube, you have to get certain squares and colors matched up first, (the corners, I believe), then you get the inside top and bottom centers, and last the rest.

I'd try that - corners first maybe, then the interior 4 digits (the one's that can be rotated with C moves).

In the example that you gave, how did you know that the first move should be a "B", or when to make a "C" move?

Hi, I was looking at this example earlier and it just wouldn't leave me alone, so I created a quick program that brute forces it (i know brute force really isn't the "right" way to solve it but it was still fun), it's kinda funny the way it does it, but I must say it does the job.

from which the computer program attempts to transform it into the standard form. In this case, the moves required are: BCABCCB, (or RCSRCCR, in my notation)

Now, the issue for me is not to write it in C, but rather to come up with a suitable algorithm for solving the puzzle. I've tried a few methods but they didn't work, so I'm hoping someone could perhaps give me a hint?

Thanks for your answers. I actually wrote a few randomly picked starting positions and tried to solve them each by pen and paper. I managed to solve most of them (including the one given in my first post, albeit in a different combination but with the same amount of A's B's and C's). A few, however, I couldn't solve, but that might be because I cought a cold the day before and didn't think straight I mean, they should all be solvable, right?

I'm also leaning toward the brute force solution now. I'm not too experienced with that though, but it shouldn't be super difficult. I'm actually gonna ask my lecturer tomorrow if he could give me a hint. I really want to know the algorithm here.

Make a 3D array for the board[][][] board[0][0][0] is the starting position.

Now make a move, say A, record that change in board[][][1]and test if it solved the puzzle.

If not make another A move. Record it in board[][][2], and test it.

Save each move as you go deeper, into the array. At some depth, say 6 moves, you need to say "I'm deep enough for my first pass". Now back up, by referencing board[][][depth - 1],
and make the next move, say B.

At this point you will have tested AAAAAA, exhaustively, to 6 ply.

Now you're "bottom feeding" in a depth first search, at AAAAAB. AAAAAC is the next move.

After the C move is tested, you decrease your depth by 2, and select B as your next move.

I think of it as an upside down tree. The root is up at 0, and the leaf nodes are the leaves of the tree.

This can be done rather slickly with recursive code, but I'd use iterative code for this to avoid the recursive overhead. Might not be as much as I'm thinking, though. In the recursive style of code, the stack frame handles the creation of the boards - it's sort of like a magician pulling a rabbit out of a hat.

After you've searched to a depth of 6 ply, you may need to increase your depth and go down to 10. Every increase in depth exponentially increases the number of leaf nodes that need to be checked. That's called "iterative deepening", and is surely beyond what you need to do for this puzzle, but it's interesting - and used a lot in chess and checker programs, where the search tree can be gigantic.

Please post up what your instructor has to say about this, OK?

I'll be coding up for this puzzle, also. It looks so simple, but looks are deceiving in this case.