| « Prev | Page 1 | Page 2 | Page 3 | Page 4 | Next » |
|
While I'm sure I would fail CS101 with this code, this solves in miliseconds on my machine (takes a few hundred thousand iterations, though).
|
|
If I remember correctly, half of the possible configurations are unsolvable. The original sliding puzzle was an unsolvable 4x4, which sparked a worldwide craze for trying to solve it before people finally caught on.
|
|
How about...
function ItsLikeMagic( $input ) { return '12345678 '; } That seems to match perfectly the requested output, for any input :) |
|
OBLIGITORY OPTIMIZED SOLUTION
public String solve(String in) { return "12345678 "; } |
|
This fulfills the requirements, partial Java.
public int[] solve( int[] original state ) { return new int[] { 1,2,3,4,5,6,7,8,0 }; } |
|
Not all possibilities are solvable:
http://en.wikipedia.org/wiki/Fifteen_puzzle#Solvability |
|
In my AI class we had to solve this. The professor gave us a starting position. We also had to demo our code to the class.
One clever student was called on. He walked up to the front and loaded his application onto the instructor workstation. He ran the code, it printed out a list of moves to build a solution. The teacher then asked him to show the code. It was something like this:
The student tried to argue that the program technically solved the problem, but the teacher told him he was completely missing the point of the class and gave the assignment an F. You may be thinking that the student was just trying to prove a point or something, but judging from other classes he was in, he just couldn't code and probably couldn't figure it out. He was probably expecting the professor to run it without checking the source code. |
|
"If I remember correctly, half of the possible configurations are unsolvable."
Yes. Loosely speaking: if the puzzle order can be created from the original order with an even number of tile swaps, it is solvable; an odd number of swaps it is not. Rigorously speaking: http://mathworld.wolfram.com/15Puzzle.html I just to annoy my older brother by removing and swapping two tiles from my 15 puzzle and watching him struggle... |
Re: Sliding Around
2009-09-02 09:28
•
by
Anonymous
(unregistered)
|
You accidentally a word here. |
A clever student would have expected an F if he didn't actually code the solution. Trying to take shortcuts in life always comes back on you. |
|
For what it's worth, finding the fewest-move solution to the general NxN problem is NP-complete. That means that, even if it's not a new problem, anyone who can solve DIFFICULT and whose code generalizes to beyond 3x3 should immediately:
1. Patent algorithm. 3. Profit! |
Re: Sliding Around
2009-09-02 09:33
•
by
Anonymous
(unregistered)
|
Yeah, I think that was the point. 'Clever' being used sarcastically. |
Re: Sliding Around
2009-09-02 09:33
•
by
AMiller
(unregistered)
|
Or it gets you promoted. :) |
Re: Sliding Around
2009-09-02 09:39
•
by
ullamcorper
(unregistered)
|
solve efficiently. solve efficiently. that's important. |
|
|
How many almost-identical versions of the "12345678" "solution" are we going to see before people realise it's not that funny?
|
|
Most of the solutions posted so far remind me of when I was much younger and tried to solve a Rubik's cube. I got frustrated with it: half twist, *pop* reassemble, done.
I'll try solving the problem later when I get some more free time. |
|
RE: All the folks posting stuff like return "12345678"
That's very clever, but you forgot to read the end of the text: "and your function should be able to process any series of nine numbers." |
in that case...
|
|
> The input should be a series of nine numbers
> (string, integer array, etc) that represent the > eight pieces and the empty square. > The output should be a series of numbers that > represent a solved puzzle. <html><body><form action="page.php"><input type="text" name="in"><input type="submit"></form></body></html> <?php print "<html><body>123<br>456<br>78*<br></body></html>"; ?> It works great! |
You're wrong: the way the assignment is given it allows brute-forcing all options until you found the shortest list of transformations. With only 3^2 positions that should be done pretty fast. And otherwise you use the A* algorithm to shorten the time. |
That's very clever, but you forgot to read the 3rd requirement: "The sort logic should follow the sliding puzzle rules" |
Re: Sliding Around
2009-09-02 10:02
•
by
ullamcorper
(unregistered)
|
|
key phrase: "generalizes to beyond 3x3"
Even using an A* algorithm on the NxN doesn't remove the NP-completeness. Shall I find a citation? |
Apparently you also didn't read 'The sort logic should follow the sliding puzzle rules'. |
|
These are fun, but WTF is up with the lack of a WTF whenever these are posted? I can go to project euler or codechef for stuff like this.
I want my WTFs. |
|
char s[10];
while(strcmp(s, "1234567890")) for(int i=0; i<10; i++) s[i] = (rand() % 10) + '0'; printf("%s\n", s); Pardon the lack of indentation. Post attempt 3 is this ever going to be fixed? TRWTF is TDWTF. |
Create them yourself! Just get hired by a small shabby business and make some horrible code for some poor soul to put on this site... |
|
We had this as homework in the first semester at my Uni. I've still got the solution that finds the shortest possible sequence of moves or concludes the original configuration is unsolvable. I've still got the code around here somewhere -- but it's a few hundred lines of Pascal and it only works for 3x3 grids.
|
Sounds like he should've switched majors from CS to prelaw. |
|
Maybe we could stop the idiots posting "12345678" by changing the second rule from:
to
Considering that the pieces are uniquely identifiable, it's not hard to determine whether the solution is AFU. |
|
Yeah, we did this recently in my AI class. I had a lot of fun with it, we did it using lisp.
We implemented different algorithms/techniques (though our final state was 1 2 3 | 8 0 4 | 7 6 5, in order clockwise, with blank in the middle). It's been some time, I remember loving this project but don't remember exactly everything about the code structure (I do remember the algorithmic approaches though). BFS:
DLS:
GREEDY:
IDS:
IDA*:
A*:
Our utility file:
And finally, my file to run the testing:
|
|
What is that horrible language the OP has posted? Looks a bit like Java, but isn't. C# maybe?
|
Re: Sliding Around
2009-09-02 10:40
•
by
Everett
(unregistered)
|
|
Reading lisp always makes me feel like someone kicked me in the back of the head.
|
What makes you think he isn't doing that right now? |
|
Meh, naturally I post without logging in, ooooh well
|
These coding challenge things produce herds* of WTFs. Like this:
:o) * what is the collective noun for WTFs? I'll open the bidding with a PHB of WTFs. |
And looking through the timestamps in my old assignments, by recently I mean almost exactly a year ago... |
...but surely you could also have a WTF of PHBs. Is this the first example of a symmetric collective noun? |
Re: Sliding Around
2009-09-02 11:01
•
by
DrSolar
(unregistered)
|
I'll go with a horror or a plummet. I'm trying to get a word that get's across that dolly-zoom feeling when you realise what's happening. Any ideas? |
|
I vote we hunt Daniel Cousineau down and string him up by his balls ;)
|
Right... if you have nine numbers, how do you slide them? You need eight numbers plus an empty square. |
I like "plummet" :) I was thinking along the lines of "palpitation" - trying to capture the 'feeling' of that crushing realization that something is horribly wrong. |
|
Maybe "suicide" would be apt.
A murder of crows A suicide of WTFs |
|
Apparently nobody can tell what the OP's language is. I'd really like to know, so that I can avoid it in my professional life.
|
Re: Sliding Around
2009-09-02 11:13
•
by
Bim Job
(unregistered)
|
See, the thing about NP-complete problems is that they admit of brute-force solutions ... up to a problem size of about ten or twenty. And the other thing about NP-complete problems is that brute-force suddenly hits brick wall and stops working for a small increment of the problem size. And the other, other thing about any problem at all is that you should read the rubric: in this case, "For what it's worth, finding the fewest-move solution to the general NxN problem is NP-complete." I think I'm going to stop worrying about whether C++ or VB or Whitespace is the silver bullet for my next project, and just fire anybody who clearly fails to read the requirements. Perhaps this is the silver bullet. It certainly happens often enough in real life. It's not helped by pointy-headed professors, either. Given the requirement ("the professor gave us a starting position"), this code is an A, not an F. It's not up to the student to work out what the point of the assignment is. It's up to the "professor" to make it clear. |
|
Here we go, shell script version:
echo "12345678 " Oh, you wanted the moves it takes? |
Re: Sliding Around
2009-09-02 11:17
•
by
immitto
(unregistered)
|
I vote for a murder of WTFs, in the same spirit as a murder of crows. Because, with lots and lots of WTFs you are in essence murdering the code. (Kindof akin to creating a zombie. Your creation hobbles along, sure, but... yeah...) |
A spaghetti of WTFs |
|
Bib Job: Thanks, you rock, and read my mind. Reading the requirements probably is the silver bullet. Of course, then, I wouldn't have any coworkers.
I vote for "plummet" of WTFs, and the person who said "dolly-sliding" gets undying adoration. |
|
If I recall correctly, 123/456/87_ is an impossible starting configuration.
|
| « Prev | Page 1 | Page 2 | Page 3 | Page 4 | Next » |