views:

1424

answers:

5

Edit: I should rephrase this, what would be a relatively easy algorithm to code in Java for solving a Rubik's cube. Efficiency is also important but a secondary consideration.

Orig: What is the easiest algorithm to code for solving a Rubik's cube? It could be the least efficient but I am looking for something easy to code right now.

+14  A: 

Perform random operations until you get the right solution. The easiest algorithm and the least efficient.

Rushyo
Hahaha with 4.33 * 10^19 permutations that is very truely the least efficient :-)
Yannick M.
+1 for doing the maths ;)
Rushyo
@ Rushyo lol well played sir @Yannick can you explain how you calculated the permutations please?
kokokok
I am not very good at maths but am I right if I say because the 6 middle pieces will always stay in the same spot we have, 6*8 = 48 spots and 8 pieces of 6 different colors which can occupy those spots so the possible permutations will be (48!)/((8!)(8!)(8!)(8!)(8!)(8!))
kokokok
=)) this not real :))
George Profenza
The author is correct: this is the easiest algorithm to implement but also the worst performing algorithm. It will not find a solution in our lifetimes.
vy32
_May_ not. It's entirely possible it would find the perfect solution first time... infinite monkeys.
Rushyo
And technically, the worst performing algorithm would be one that explicitly avoided the correct answer until a trigger told it to find the solution.
Rushyo
Quoted from wikipedia: 'The original (3×3×3) Rubik's Cube has eight corners and twelve edges. There are 8! (40,320) ways to arrange the corner cubes. Seven can be oriented independently, and the orientation of the eighth depends on the preceding seven, giving 37 (2,187) possibilities. There are 12!/2 (239,500,800) ways to arrange the edges, since an odd permutation of the corners implies an odd permutation of the edges as well. Eleven edges can be flipped independently, with the flip of the twelfth depending on the preceding ones, giving 211 (2,048) possibilities.[19]'
Yannick M.
+4  A: 

Might want to check out: http://peter.stillhq.com/jasmine/rubikscubesolution.html

Has a graphical representation of an algorithm to solve a 3x3x3 Rubik's cube

Yannick M.
+5  A: 

The simplest non-trivial algorithm I've found is this one:

http://www.chessandpoker.com/rubiks-cube-solution.html

It doesn't look too hard to code up. The link mentioned in Yannick M.'s answer looks good too, but the solution of 'the cross' step looks like it might be a little more complex to me.

There are a number of open source solver implementations which you might like to take a look at. Here's a Python implementation. This Java applet also includes a solver, and the source code is available. There's also a Javascript solver, also with downloadable source code.

Anthony Gatlin's answer makes an excellent point about the well-suitedness of Prolog for this task. Here's a detailed article about how to write your own Prolog solver. The heuristics it uses are particularly interesting.

ire_and_curses
+3  A: 

I understand your question is related to Java, but on a practical note, languages like Prolog are much better suited problems like solving a Rubik's cube. I assume this is probably for a class though and you may have no leeway as to the choice of tool.

Anthony Gatlin
that's right, unfortunately for the class I have to do it in Java
kokokok
+1 for reminding me of Prolog. ;)
ire_and_curses
Mmmmm, a use for prolog.
Rushyo
The solution is obvious: write a prolog interpreter in Java!
John Fouhy
Take your pick: http://www.is-research.de/info/vmlanguages/logic-programming/
Thorbjørn Ravn Andersen
A: 

actually I'm using heise's beginning solution to implement the algo also :) maybe you want to team up, lemme know

xxxxxxx