<<Previous

A Simple Algorithm 

Next>>
Introduction

The puzzle
Overview
The Goal
Playing

The algorithm
Simple
Best solution
Search
Heuristic

Program Design
Search Classes
Extensions
Display

Download

Run

Comments

A simple algorithm: A simple way to solve the 15 puzzle is to try to place the block with the number "1" in its final position. Then, without moving the "1", place the block numbered "2" in its final place. Then, without moving the blocks, numbered "1" and "2", move the "3".... and so on.

If one attempts this, one will find that the first two rows can be organized quite easily.

However, when it comes to the third and fourth row, it is not always possible to move a later block, without moving an earlier block. 

For instance, consider this arrangement:

The blocks from 1 though 11 are arranged already. However, we must move them, if we want to get the block numbered "12" to its position.

A Solution:
  1. Move "14" up
  2. Move "12", "15' and "13" right
  3. Move "9" down
  4. Move "10", "11" and "14" left.

We arrive at this:

continuing...

  1. Move "12" up
  2. Move "15" right
  3. Drop "14" down
  4. Move "11", "10" and "9" back to their final places
  5. Do the same for "13", "14" and "15" by shifting them to the left.
Notice that though 9 steps are listed, some steps (e.g. step 2) require moving more than one block.

If we count each block moved as one move, then these 9 steps contain 17 moves.