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>

int data[9],first[9],limit = 32, cnt = 0;
int process(int, int);
int check();
void change1(int, int);
void change2(int, int);
void debug(int, int);

int main(){

  int i;

  for(i = 0; i < 9; i++)
  {
    scanf("%d",&data[i]);
    first[i] = data[i];
  }



  for(i = 0; i < 9; i++)
  {
    if(data[i] == 0)break;
  }

  process(i, -1);

  printf("%d\n",limit);

  return 0;
}

int process(int x, int back)
{
  int i, a;
  //debug(x,back);
  if(cnt >= limit)return 0;
  if(check() == 1)return 1;

  switch(x){
    /*
    case 0:
    printf("x = %d, back = %d\n",x,back);
    if(back != 1)
    {
      change1(0,1);
      process(1,0);
      change2(0,1);
    }
    if(back != 3)
    {
      change1(0,3);
      process(1,0);
      change2(0,3);
    }
    return 0;
    */
    case 0:
    if(back != 1 && data[x] == 0)
    {
      change1(0,1);
      process(1,0);
      change2(0,1);
    }
    if(back != 3 && data[x] == 0)
    {
      change1(0,3);
      process(3,0);
      change2(0,3);
    }
    return 0;

    case 1:
    //if(cnt == 1)printf("x = %d, back = %d\n",x,back);
    if(back != 0 && data[x] == 0)
    {
      change1(1,0);
      process(0,1);
      change2(1,0);
    }
    if(back != 2 && data[x] == 0)
    {
      change1(1,2);
      process(2,1);
      change2(1,2);
    }
    if(back != 4 && data[x] == 0)
    {
      change1(1,4);
      process(4,1);
      change2(1,4);
    }
    return 0;

    case 2:
    if(back != 1 && data[x] == 0)
    {
      change1(2,1);
      process(1,2);
      change2(2,1);
    }
    if(back != 5 && data[x] == 0)
    {
      change1(2,5);
      process(5,2);
      change2(2,5);
    }
    return 0;

    case 3:
    if(back != 0 && data[x] == 0)
    {
      change1(3,0);
      process(0,3);
      change2(3,0);
    }
    if(back != 4 && data[x] == 0)
    {
      change1(3,4);
      process(4,3);
      change2(3,4);
    }
    if(back != 6 && data[x] == 0)
    {
      change1(3,6);
      process(6,3);
      change2(3,6);
    }
    return 0;

    case 4:
    if(back != 1 && data[x] == 0)
    {
      change1(4,1);
      process(1,4);
      change2(4,1);
    }
    if(back != 3 && data[x] == 0)
    {
      change1(4,3);
      process(3,4);
      change2(4,3);
    }
    if(back != 5 && data[x] == 0)
    {
      change1(4,5);
      process(5,4);
      change2(5,4);
    }
    if(back != 7 && data[x] == 0)
    {
      change1(4,7);
      process(7,4);
      change2(4,7);
    }
    return 0;

    case 5:
    if(back != 2 && data[x] == 0)
    {
      change1(5,2);
      process(2,5);
      change2(5,2);
    }
    if(back != 4 && data[x] == 0)
    {
      change1(5,4);
      process(4,5);
      change2(5,4);
    }
    if(back != 8 && data[x] == 0)
    {
      change1(5,8);
      process(8,5);
      change2(5,8);
    }
    return 0;

    case 6:
    if(back != 3 && data[x] == 0)
    {
      change1(6,3);
      process(3,6);
      change2(6,3);
    }
    if(back != 7 && data[x] == 0)
    {
      change1(6,7);
      process(7,6);
      change2(6,7);
    }
    return 0;

    case 7:
    if(back != 4 && data[x] == 0)
    {
      change1(7,4);
      process(4,7);
      change2(7,4);
    }
    if(back != 6 && data[x] == 0)
    {
      change1(7,6);
      process(6,7);
      change2(7,6);
    }
    if(back != 8 && data[x] == 0)
    {
      change1(7,8);
      process(8,7);
      change2(7,8);
    }
    return 0;

    case 8:
    if(back != 5 && data[x] == 0)
    {
      change1(8,5);
      process(5,8);
      change2(8,5);
    }
    if(back != 7 && data[x] == 0)
    {
      change1(8,7);
      process(7,8);
      change2(8,7);
    }
    return 0;
  }
  return 0;
}

void change1(int x, int y)
{
  int temp;
  cnt++;
  temp = data[x];
  data[x] = data[y];
  data[y] = temp;
}

void change2(int x, int y)
{
  int temp;
  cnt--;
  temp = data[x];
  data[x] = data[y];
  data[y] = temp;
}


int check()
{
  int i;
  for(i = 0; i < 8; i++)
  {
    if(data[i] != i + 1)return 0;
  }
  limit = cnt;
  return 1;
}

void debug(int x, int back)
{
  int i;
  if(cnt == 1)printf("--------------------------------\n\n\n\n");
  printf("cnt = %2d || limit = %2d || x:%d  back:%d ||%d → %d\n",cnt,limit,x,back,back,x);
  for(i = 0; i < 9; i++)
  {
    printf("%d ",data[i]);
    if(i % 3 == 2)printf("\n");
  }
}