Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

8 Puzzle

The goal of the 8 puzzle problem is to complete pieces on $3 \times 3$ cells where one of the cells is empty space.

In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 8 as shown below.

1 3 0
4 2 5
7 8 6

You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps).

1 2 3
4 5 6
7 8 0

Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.

Input

The $3 \times 3$ integers denoting the pieces or space are given.

Output

Print the fewest steps in a line.

Constraints

There is a solution.

Sample Input

1 3 0
4 2 5
7 8 6

Sample Output

4

Solution in C (judge verdict: Time Limit Exceeded)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef char Board[10];
Board ini_board, board, *history;
const Board target = "123456780";
int nhistories=0;

typedef enum { U, D, L, R } Direction;

typedef struct {
  char pos;
  char dir;
} Motion;

int minstep;

const Motion motion[9][5] =
  {{{1,L},{3,U},{-1}}, {{0,R},{2,L},{4,U},{-1}}, {{1,R},{5,U},{-1}},
   {{0,D},{4,L},{6,U},{-1}}, {{1,D},{5,L},{7,U},{3,R},{-1}},
   {{2,D},{8,U},{4,R},{-1}},
   {{3,D},{7,L},{-1}}, {{4,D},{8,L},{6,R},{-1}}, {{5,D},{7,R},{-1}}  };

int search_path(int step, char space)
{
  int i, j;
  if (strcmp(board, "123456780")==0) {
    //    printf("STEP=%d\n", step);                                                 
    //printf("%d\n", step);                                                          
    if (step<minstep) minstep=step;
    printf("%d\n", step);
    exit(0);
  } else {
    if (step>=minstep) return 1;
    if (step>0 && strcmp(board, ini_board)==0) return 1;

    Board board_bak;
    strcpy(board_bak, board);
    int nhistories_bak=nhistories;

   for (i=0; motion[space][i].pos>=0; i++) {
      Motion m = motion[space][i];

      if (m.pos==0 && board[0]=='1') continue;
      if ((m.pos==1||m.pos==2) && board[0]=='1'&&board[1]=='2'&&board[2]=='3') conti\
nue;
      if ((m.pos==3||m.pos==6) && board[0]=='1'&&board[3]=='4'&&board[6]=='7')contin\
ue;
      switch (m.dir) {
      case U:
        if (m.pos<3) continue;
        board[m.pos-3] = board[m.pos];
        break;
      case D:
        if (m.pos>5) continue;
        board[m.pos+3] = board[m.pos];
        break;
      case L:
        if (m.pos%3==0) continue;
        board[m.pos-1] = board[m.pos];
        break;
      case R:
        if (m.pos%3==2) continue;
        board[m.pos+1] = board[m.pos];
        break;
      }
      board[m.pos] = '0';

      for (j=0; j<nhistories; j++) {
        if (strcmp(board, history[j])==0)     break;
      }
      if (j==nhistories) {
        strcpy(history[nhistories++], board);
        search_path(step+1, m.pos);
      }
      nhistories = nhistories_bak;
      strcpy(board, board_bak);
    }
  }
  return 1;
}

int main()
{
  fscanf(stdin, "%c %c %c\n", &board[0],&board[1],&board[2]);
  fscanf(stdin, "%c %c %c\n", &board[3],&board[4],&board[5]);
  fscanf(stdin, "%c %c %c\n", &board[6],&board[7],&board[8]);
  strcpy(ini_board, board);
  for (minstep=1; minstep<32; minstep++) {
    strcpy(board, ini_board);
    history = (Board*)malloc(sizeof(Board)*100000);
    search_path(0, strchr(board, '0')-board);
    free(history);
  }
  return 0;
}