Problem 42842. The sliding puzzle: 15

Created by HH in Community

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

25.81% Correct | 74.19% Incorrect
Last solution submitted on Feb 25, 2019

Problem Comments