Problem 42842. The sliding puzzle: 15
If you are unfamiliar with the sliding puzzle, enter the command fifteen in your MATLAB command window (or search online, of course). In this problem you are tasked with solving the puzzle.
Given a scrambled classic 4-by-4 sliding puzzle, return a set of moves that solves it.
The puzzle is represented by a size [4 4] array, p, with integers from 1 to 15 representing the different tiles, and 0 representing the open slot. A single move is represented by an integer that is the linear index of the the tile you wish to slide into an adjacent open slot. A solution is represented by a row vector, m, of moves, the application of which results in a correctly arranged puzzle.
The solution does not have to be efficient. It must simply result in a correctly solved puzzle. Illegal moves, such as trying to slide a tile that is not adjacent to the open slot, will be ignored.
Example: (the leading zeros are added only for easier visualization)
p = [ 01 02 03 04;
05 10 06 07;
09 00 11 08;
13 14 15 12]
m = [6 10 14 15 16]
Solution Stats
Problem Comments
-
3 Comments
Are Mjaavatten
on 14 Jul 2018
The most challenging cody problem I have tried so far. Had to teach myself some stuff that I knew little about before, That is a quality indicator!
HH
on 15 Jul 2018
Thanks, Are. Now try problem 44376. https://www.mathworks.com/matlabcentral/cody/problems/44376
Sophia Nguyen
on 21 Feb 2020
I have tried repeatedly. The code works well and only took 2s max in my computer, but the system keeps outputting error caused by long running matlab code.
Solution Comments
Show commentsProblem Recent Solvers27
Suggested Problems
-
7666 Solvers
-
2255 Solvers
-
1293 Solvers
-
Get the length of a given vector
11039 Solvers
-
Calculate the area of a triangle between three points
3005 Solvers
More from this Author45
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!