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 struct node{
  char s[10];
  int f;
  struct node *next;
  struct node *back;
}Node;

void move_up(char s[], int x) {
  char tmp;
  tmp = s[x];
  s[x] = s[x-3];
  s[x-3] = tmp;
}

void move_left(char s[], int x) {
  char tmp;
  tmp = s[x];
  s[x] = s[x-1];
  s[x-1] = tmp;
}

void move_right(char s[], int x) {
  char tmp;
  tmp = s[x];
  s[x] = s[x+1];
  s[x+1] = tmp;
}

void move_down(char s[], int x) {
  char tmp;
  tmp = s[x];
  s[x] = s[x+3];
  s[x+3] = tmp;
}

int heuristic(char s[]) {
  int h,i,d;
  h = 0;
  for (i = 0; i < 9; i++) {
    if(s[i] == '0') {
      h += 2-i%3 + 2-i/3;
    }
    else {
      d = abs(s[i]-'0'-1 - i);
      h += d%3 + d/3; 
    }
  }
  return h;
}

Node *add_list(Node *begin, char s[], int f) {
  Node *p;
  p = begin;
  while(p != NULL) {
    if(strcmp(p->s,s) == 0) {
      if(p->f > f) p->f = f;
      return begin;
    }
    p = p->next;
  }
  
  p = (Node*)malloc(sizeof(Node));
  strcpy(p->s,s);
  p->f = f;  
  if(begin != NULL) begin->back = p;
  p->next = begin;
  p->back = NULL;
  return p;
}

Node *remove_node(Node *begin, Node *p) {
  if(p == begin) {
    if(p->next != NULL) p->next->back = NULL;
    return p->next;
  }
  p->back->next = p->next;
  if(p->next != NULL) p->next->back = p->back;
  return begin;
}

Node *move_node(Node *begin, Node *p) {
  p->back = NULL;
  p->next = begin;
  if(begin != NULL) begin->back = p;
  return p;
}

Node *search_node(Node *begin, char s[]) {
  Node *p;
  p = begin;
  while(p != NULL) {
    if(strcmp(p->s,s) == 0) break;
    p = p->next;
  }
  return p;
}

void open_node(Node **open, Node **close, char s[], int g) {
  int f;
  Node *p;
  f = g + 1 + heuristic(s);
  p = search_node(*close,s);
  if(p != NULL ) {
    if(p->f > f) {
      p->f = f;
      *close = remove_node(*close,p);
      *open = move_node(*open,p);
    }
  }
  else {
    *open = add_list(*open,s,f);
  }
}

int solve(char s0[]) {
  int x,g,min_f;
  char s[10];
  Node *open = NULL;
  Node *close = NULL;
  Node *p,*min;
  open = add_list(open,s0,heuristic(s0));
  while(1) {
    p = open;
    min_f = p->f;
    min = p;
    while(p != NULL) {
      if(p->f < min_f) {
  	min_f = p->f;
  	min = p;
      }
      p = p->next;
    }
    strcpy(s,min->s);
    //printf("%s\n",s);
    if(strcmp(s,"123456780") == 0) break;
    g = min->f - heuristic(s);
    open = remove_node(open,min);
    close = move_node(close,min);
    for (x = 0; x < 9; x++) if(s[x] == '0') break;
    if(x > 2) {
      move_up(s,x);
      open_node(&open,&close,s,g);
      move_down(s,x-3);
    }
    if(x < 6) {
      move_down(s,x);
      open_node(&open,&close,s,g);
      move_up(s,x+3);
    }
    if(x%3 < 2) {
      move_right(s,x);
      open_node(&open,&close,s,g);
      move_left(s,x+1);
    }
    if(x%3 > 0) {
      move_left(s,x);
      open_node(&open,&close,s,g);
      move_right(s,x-1);
    }
  }
  return min->f;
}

int main()
{
  int i,d;
  char s[10];
  for (i = 0; i < 9; i++) {
    scanf("%d", &d);
    s[i] = d+'0';
  }
  s[9] = '\0';
  printf("%d\n",solve(s));
  
  return 0;
}