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: Wrong Answer)

#include <stdio.h>
#include <stdlib.h>
 
#define tate 3
#define yoko 3
#define Total (tate*yoko)

extern inline void down_0(void);
extern inline void up_0(void);
extern inline void right_0(void);
extern inline void left_0(void);

extern int  serch(int x, int y, int MaxCnt, int pre);
extern int  check(void);

int cnt,num = 0;
int val[Total] ={};


int main( void )
{
    int i = 0;
    int x,y = 0;
    
    for(i=0; i<Total; i++)
    {
        scanf("%d",&val[i]);
        
        if(val[i] == 0)
        {
            num = i;
        }
    }
    
    x = tate - (num/yoko) -1;
    y = yoko - (num%yoko) -1;
    
    for(i=1; i<31; i++)
    {
        if( serch(x,y,i,4) == 1 )
        {
            printf("%d\n",i);
            break;
        }
    }     
    
    return 0;
}

int serch(int x, int y, int MaxCnt, int pre)
{
    int result;
    int i=0;
    
    // 最大数で打ち切り
    if(MaxCnt==0)
    {
        return check();
    }
    
    // 上下左右で4回回す
    for(i=0; i<4; i++)
    {
        result = 0;
        
        switch(i)
        {
            case 0:
                if( (x > 0) && (pre != 3) )
                {
                    down_0();
                    
                    result = serch(x-1, y, MaxCnt-1, 0);
                    
                    up_0();
                }
                break;
            case 1:
                if( (y > 0) && (pre != 2) )
                {
                    right_0();
                    
                    result = serch(x, y-1, MaxCnt-1, 1);
                    
                    left_0();
                }
                break;
            case 2:
                if( (y < yoko) && (pre != 1) )
                {
                    left_0();
                    
                    result = serch(x, y+1, MaxCnt-1, 2);
                    
                    right_0();
                }
                break;
            case 3:
                if( (x < tate) && (pre != 0) )
                {
                    up_0();
                    
                    result = serch(x+1, y, MaxCnt-1, 3);
                    
                    down_0();
                }
                break;
            default:
                break;
        }
        
        if(result)
        {
            return 1;
        }
        
    }

    return 0;
}

// TRUE : 完成
// FALSE : 失敗
int check()
{
    int i=0;
    
//    printf("%d%d%d%d%d%d%d%d%d\n", val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7], val[8]);
    
    for(i=0; i<Total-1; i++)
    {
        if(val[i] != (i+1))
        {
            return 0;
        }
    }
    return 1;
}

inline void down_0()
{
    val[num] = val[num + yoko];
    num = num + yoko;
}
 
inline void up_0()
{
    val[num] = val[num - yoko];
    num = num - yoko;
}
 
inline void right_0()
{
    val[num] = val[num +1];
    num = num + 1;
}
 
inline void left_0()
{
    val[num] = val[num -1];
    num = num - 1;
}