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 Python3 (judge verdict: Wrong Answer)

import sys;
import queue;

def iterative(i,j,c_depth,c_puz,costs):
    if(puzzleCheck(c_puz)):
        global finding
        finding = 1
        print(c_depth)

    if(finding == 1 or (c_depth+checkSumCost(costs)) > depth):
        return

    if(i != 0 and finding != 1):
        _c_puz = swapPuz(c_puz[0:],i,j,i-1,j)
        _costs = costs[0:]
        _costs[i*3+j] = HS(i,j,_c_puz[i*3+j])
        _costs[(i-1)*3+j] = HS(i,j,_c_puz[(i-1)*3+j])
        iterative(i-1,j,c_depth+1,_c_puz[0:],_costs[0:])
    if(i != 2 and finding != 1):
        _c_puz = swapPuz(c_puz[0:],i,j,i+1,j)
        _costs = costs[0:]
        _costs[i*3+j] = HS(i,j,_c_puz[i*3+j])
        _costs[(i+1)*3+j] = HS(i,j,_c_puz[(i+1)*3+j])
        iterative(i+1,j,c_depth+1,_c_puz[0:],_costs[0:])
    if(j != 0 and finding != 1):
        _c_puz = swapPuz(c_puz[0:],i,j,i,j-1)
        _costs = costs[0:]
        _costs[i*3+j] = HS(i,j,_c_puz[i*3+j])
        _costs[i*3+j-1] = HS(i,j,_c_puz[i*3+j]-1)
        iterative(i,j-1,c_depth+1,_c_puz[0:],_costs[0:])
    if(j != 2 and finding != 1):
        _c_puz = swapPuz(c_puz[0:],i,j,i,j+1)
        _costs = costs[0:]
        _costs[i*3+j] = HS(i,j,_c_puz[i*3+j])
        _costs[i*3+j+1] = HS(i,j,_c_puz[i*3+j+1])
        iterative(i,j+1,c_depth+1,_c_puz[0:],_costs[0:])

def checkSumCost(costs):
    value = 0
    for i in range(len(costs)):
        value+=costs[i]
    return value

def simpleHS(c_puz):
    count = 0
    for i in range(len(correctPuz)):
        if(correctPuz[i] != c_puz[i]):
            count+=1
    return count

def HS(i,j,num):
    k = correctPuz.index(num)
    ki = (int)(k/3)
    kj = k - ki*3
    value = abs(i-ki)+abs(j-kj) 
    return value


def swapPuz(c_puz, i, j, i2,j2):
    swap = c_puz[i*3+j]
    c_puz[i*3+j] = c_puz[i2*3+j2]
    c_puz[i2*3+j2] = swap
    return c_puz[0:]

def puzzleCheck(c_puz):
    if(c_puz[8] == 0):
        for i in range(len(correctPuz)):
            if(correctPuz[i] != c_puz[i]):
                return False
        return True
    return False

correctPuz = [i+1 for i in range(9)]
correctPuz[8] = 0
puz = [0 for i in range(9)]
costs = [0 for i in range(9)]

i_start = 0
j_start = 0

for i in range(3):
    puz[i*3],puz[i*3+1],puz[i*3+2] = map(int, input().split());
    if(puz[i*3] == 0):
        i_start,j_start = i,0
    elif(puz[i*3+1] == 0):
        i_start,j_start = i,1
    elif(puz[i*3+2] == 0):
        i_start,j_start = i,2

for i in range(len(costs)):
    _i = (int)(i/3)
    _j = i - _i*3
    costs[i] = HS(_i,_j,puz[i])

finding = 0
depth = 0
while True:
    if(finding == 1):
        break
    iterative(i_start,j_start,0,puz[0:],costs[0:])
    depth+=1