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: Accepted)

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

char * VAR00 ;
int VAR01 , VAR02 ;
int * VAR03 ;
int VAR04 [ 10 ] ;

int FUNC00 ( int ARG00 ) {
  if ( ARG00 == 0 ) return 1 ;
  return FUNC00 ( ARG00 - 1 ) * ARG00 ;
}

int FUNC01 ( char * ARG01 ) {
  int VAR05 = 0 ;
  int VAR06 ;
  for ( VAR06 = 0 ; VAR06 < 9 ; VAR06 ++ ) {
    int VAR07 , VAR08 ;
    VAR07 = 0 ;
    for ( VAR08 = 0 ; VAR08 < VAR06 ; VAR08 ++ ) {
      if ( ARG01 [ VAR08 ] < ARG01 [ VAR06 ] ) VAR07 ++ ;
    }
    VAR05 += VAR04 [ 8 - VAR06 ] * ( ARG01 [ VAR06 ] - '0' - VAR07 ) ;
  }
  return VAR05 ;
}

char * FUNC02 ( int ARG02 ) {
  int VAR09 ;
  char * VAR10 ;
  VAR10 = ( char * ) malloc ( sizeof ( char ) * 10 ) ;
  for ( VAR09 = 0 ; VAR09 < 9 ; VAR09 ++ ) {
    VAR10 [ VAR09 ] = ARG02 % VAR04 [ 9 - VAR09 ] / VAR04 [ 8 - VAR09 ] + '0' ;
  }
  VAR10 [ 9 ] = '\0' ;
  for ( VAR09 = 7 ; VAR09 >= 0 ; VAR09 -- ) {
    int VAR11 ;
    for ( VAR11 = 8 ; VAR11 > VAR09 ; VAR11 -- ) {
      if ( VAR10 [ VAR09 ] <= VAR10 [ VAR11 ] ) VAR10 [ VAR11 ] ++ ;
    }
  }
  return VAR10 ;
}


char * FUNC03 ( char ARG03 [ ] , int ARG04 ) {
  if ( ARG04 < 3 ) return NULL ;
  char * VAR12 ;
  VAR12 = ( char * ) malloc ( sizeof ( char ) * 10 ) ;
  strcpy ( VAR12 , ARG03 ) ;
  VAR12 [ ARG04 ] = ARG03 [ ARG04 - 3 ] ;
  VAR12 [ ARG04 - 3 ] = ARG03 [ ARG04 ] ;
  return VAR12 ;
}

char * FUNC04 ( char ARG05 [ ] , int ARG06 ) {
  if ( ARG06 % 3 == 0 ) return NULL ;
  char * VAR13 ;
  VAR13 = ( char * ) malloc ( sizeof ( char ) * 10 ) ;
  strcpy ( VAR13 , ARG05 ) ;
  VAR13 [ ARG06 ] = ARG05 [ ARG06 - 1 ] ;
  VAR13 [ ARG06 - 1 ] = ARG05 [ ARG06 ] ;
  return VAR13 ;
}

char * FUNC05 ( char ARG07 [ ] , int ARG08 ) {
  if ( ARG08 % 3 == 2 ) return NULL ;
  char * VAR14 ;
  VAR14 = ( char * ) malloc ( sizeof ( char ) * 10 ) ;
  strcpy ( VAR14 , ARG07 ) ;
  VAR14 [ ARG08 ] = ARG07 [ ARG08 + 1 ] ;
  VAR14 [ ARG08 + 1 ] = ARG07 [ ARG08 ] ;
  return VAR14 ;
}

char * FUNC06 ( char ARG09 [ ] , int ARG10 ) {
  if ( ARG10 > 5 ) return NULL ;
  char * VAR15 ;
  VAR15 = ( char * ) malloc ( sizeof ( char ) * 10 ) ;
  strcpy ( VAR15 , ARG09 ) ;
  VAR15 [ ARG10 ] = ARG09 [ ARG10 + 3 ] ;
  VAR15 [ ARG10 + 3 ] = ARG09 [ ARG10 ] ;
  return VAR15 ;
}

int FUNC07 ( char ARG11 [ ] , int ARG12 ) {
  if ( strcmp ( ARG11 , "123456780" ) == 0 ) return ARG12 ;
  int VAR16 = FUNC01 ( ARG11 ) ;
  if ( VAR00 [ VAR16 ] ) return 0 ;
  VAR00 [ VAR16 ] = ARG12 ;
  VAR03 [ VAR02 ] = VAR16 ;
  VAR02 ++ ;
  return 0 ;
}

int FUNC08 ( char * ARG13 ) {
  int VAR17 = 0 ;
  while ( ARG13 [ VAR17 ] != '0' ) VAR17 ++ ;
  return VAR17 ;
}

int FUNC09 ( ) {
  char * VAR18 , * VAR19 , * VAR20 , * VAR21 ;
  int VAR22 , VAR23 ;

  VAR23 = VAR03 [ VAR01 ] ;
  char * VAR24 = FUNC02 ( VAR23 ) ;
  int VAR25 = FUNC08 ( VAR24 ) ;

  if ( ( VAR18 = FUNC05 ( VAR24 , VAR25 ) ) != NULL ) {
    if ( ( VAR22 = FUNC07 ( VAR18 , VAR00 [ VAR23 ] + 1 ) ) ) return VAR22 ;
    free ( VAR18 ) ;
  }
  if ( ( VAR19 = FUNC04 ( VAR24 , VAR25 ) ) != NULL ) {
     if ( ( VAR22 = FUNC07 ( VAR19 , VAR00 [ VAR23 ] + 1 ) ) ) return VAR22 ;
     free ( VAR19 ) ;
  }
  if ( ( VAR20 = FUNC03 ( VAR24 , VAR25 ) ) != NULL ) {
    if ( ( VAR22 = FUNC07 ( VAR20 , VAR00 [ VAR23 ] + 1 ) ) ) return VAR22 ;
    free ( VAR20 ) ;
  }
  if ( ( VAR21 = FUNC06 ( VAR24 , VAR25 ) ) != NULL ) {
    if ( ( VAR22 = FUNC07 ( VAR21 , VAR00 [ VAR23 ] + 1 ) ) ) return VAR22 ;
    free ( VAR21 ) ;
  }
  free ( VAR24 ) ;
  VAR01 ++ ;
  return 0 ;
}


int FUNC10 ( char ARG14 [ ] ) {
  int VAR26 ;
  if ( ( VAR26 = FUNC07 ( ARG14 , 1 ) ) ) return VAR26 ;

  while ( ! ( VAR26 = FUNC09 ( ) ) ) {
    if ( VAR01 == VAR02 ) break ;
  }
  return VAR26 ;
}

int main ( )
{

  int VAR27 , VAR28 ;
  char VAR29 [ 10 ] ;
  for ( VAR27 = 0 ; VAR27 < 10 ; VAR27 ++ ) VAR04 [ VAR27 ] = FUNC00 ( VAR27 ) ;
  VAR00 = ( char * ) malloc ( sizeof ( char ) * VAR04 [ 9 ] ) ;
  memset ( VAR00 , 0 , VAR04 [ 9 ] ) ;
  VAR03 = ( int * ) malloc ( sizeof ( int ) * VAR04 [ 9 ] ) ;
  VAR01 = 0 ;
  VAR02 = 0 ;
  for ( VAR27 = 0 ; VAR27 < 9 ; VAR27 ++ ) {
    scanf ( "%d" , & VAR28 ) ;
    VAR29 [ VAR27 ] = VAR28 + '0' ;
  }
  VAR29 [ 9 ] = '\0' ;
  printf ( "%d\n" , FUNC10 ( VAR29 ) - 1 ) ;

  return 0 ;
}