Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

15 Puzzle

The goal of the 15 puzzle problem is to complete pieces on $4 \times 4$ 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 15 as shown below.

1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15

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
9 10 11 12
13 14 15 0

Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.

Input

The $4 \times 4$ integers denoting the pieces or space are given.

Output

Print the fewest steps in a line.

Constraints

The given puzzle is solvable in at most 45 steps.

Sample Input

1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15

Sample Output

8

Solution in C# (judge verdict: Accepted)

using System;

namespace ALDS1_13_C
{
    class Puzzle
    {
        internal int[] f;
        internal int space, MD;
        public Puzzle()
        {
            f = new int[16];
            space = 0;
            MD = 0;
        }
    }
    class Program
    {
        const int N = 4;
        const int N2 = 16;
        const int LIMIT = 45;
        static int limit;
        static int[] path = new int[LIMIT];
        static int[,] MDT = new int[N2, N2];
        static int[] dx = { 0, -1, 0, 1 };
        static int[] dy = { 1, 0, -1, 0 };
        static char[] dir = { 'r', 'u', 'l', 'd' };
        static Puzzle state;


        static bool checker()
        {
            for (int i = 0; i < N2; i++)
            {
                if (state.f[i] != i + 1) return false;
            }
            return true;
        }
        static void P_COPY(Puzzle x, Puzzle y)
        {
            x.f = (int[])y.f.Clone();
            x.space = y.space;
            x.MD = y.MD;
        }
        static bool DFS(int depth, int prev)
        {
            if (state.MD == 0)
            {
                //Console.WriteLine(string.Join(" ",state.f));
                return true;
            }
            if (depth + state.MD > limit) return false;

            int sx = state.space / N;
            int sy = state.space % N;


            for (int r = 0; r < 4; r++)
            {
                //Console.WriteLine(state.MD +" "+state.space +"  " + string.Join(" ", state.f));
                int tx = sx + dx[r];
                int ty = sy + dy[r];
                if (tx < 0 || ty < 0 || tx >= N || ty >= N) continue;
                if (Math.Abs(prev - r) == 2) continue;

                var tmp = new Puzzle();
                P_COPY(tmp, state);
                state.MD -= MDT[tx * N + ty, state.f[tx * N + ty] - 1];
                state.MD += MDT[sx * N + sy, state.f[tx * N + ty] - 1];

                SWAP(state.f, tx * N + ty, sx * N + sy);
                state.space = tx * N + ty;
                if (DFS(depth + 1, r))
                {
                    path[depth] = r;
                    return true;
                }

                state = new Puzzle();
                P_COPY(state, tmp);
            }
            return false;
        }
        static void SWAP(int[] a, int x, int y)
        {
            var t = a[x];
            a[x] = a[y];
            a[y] = t;
        }
        static int GetAllMD(Puzzle P)
        {
            int sum = 0;
            for (int i = 0; i < N2; i++)
            {
                if (P.f[i] == N2) continue;
                sum += MDT[i, P.f[i] - 1];
            }
            return sum;
        }
        static string IDA(Puzzle P)
        {
            P.MD = GetAllMD(P);
           // Console.WriteLine(string.Concat(P.f) + P.space + P.MD);

            for (limit = P.MD; limit <= LIMIT ; limit++)
            {
                state = new Puzzle();
                P_COPY(state, P);
                if(DFS(0, -45))
                {
                    var ans = "";
                    for (int i = 0; i < limit; i++)
                    {
                        ans += dir[path[i]];
                    }
                    return ans;
                }
            }

            return "unsolvable"; 
        }
        static void Main(string[] args)
        {
            var P = new Puzzle();

            for (int i = 0; i < N2; i++)
            {
                for (int j = 0; j < N2; j++)
                {
                    MDT[i, j] = Math.Abs(i / N - j / N) + Math.Abs(i % N - j % N);
                }
            }       
            for (int i = 0; i < N; i++)
            {
                var t = scan;
                for (int j = 0; j < N; j++)
                {
                    P.f[N * i + j] = t[j];
                    if (t[j] == 0)
                    {
                        P.f[N * i + j] = N2;
                        P.space = N * i + j;
                    }
                }
                
            }
            string ans = IDA(P);
            //Console.WriteLine(ans);
            Console.WriteLine(ans.Length);
        }
      

        static int[] scan { get { return Array.ConvertAll(Console.ReadLine().Split(), int.Parse); } }
    }
}