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: Memory Limit Exceeded)

#include <iostream>
#include <string>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>

using namespace std;

typedef long long Board;

const int N = 9;
const Board GOAL =     1      | 2 << (1*4)  | 3 << (2*4)
                 | 4 << (3*4) | 5 << (4*4)  | 6 << (5*4)
                 | 7 << (6*4) | 8LL << (7*4) | 0LL << (8*4);

struct state {
    Board board;
    int count;
};

queue<state> q;

inline void swap(int numpos, int spacepos, Board& board) {
    long long num = (board >> (numpos*4)) & 15;
    board |= num << (spacepos*4);
//    board |= static_cast<long long>(num) << (spacepos*4);
    board &= ~(15LL << (numpos*4));
}

inline int swap_and_check(int numpos, int spacepos, state s) {
    swap(numpos, spacepos, s.board);
    ++s.count;
    if (s.board == GOAL) {
        return s.count;
    }
    q.push(s);
    return -1;
}

void print(Board b) {
    for (int i = 0; i < N; ++i) {
        printf("%2lld", (b >> (i*4) & 15));
    }
    printf("\n");
}

int main(void) {

    Board board = 0;
    int i = 0;
    while (i != N) {
        char ch;
        scanf("%c", &ch);
        if (isdigit(ch)) {
            board |= static_cast<long long>((ch - '0')) << (i*4);
            ++i;
        }
    }

//    print(GOAL);
//    print(board);

    q.push({ board, 0 });
    int count = 0;
    while (!q.empty()) {
        state s = q.front();
        q.pop();
        // ?????????????????????
        int pos;
        for (int i = 0; i < N; ++i) {
            if (((s.board >> (i*4)) & 15) == 0) {
                pos = i;
                break;
            }
        }

        if (pos < 3) {
            if (pos < 2) {
                if ((count = swap_and_check(pos + 1, pos, s)) > 0) {
                    break;
                }
            }
            if (pos > 0) {
                if ((count = swap_and_check(pos - 1, pos, s)) > 0) {
                    break;
                }
            }
            if ((count = swap_and_check(pos + 3, pos, s)) > 0) {
                break;
            }
        }
        else if (pos < 6) {
            if ((count = swap_and_check(pos - 3, pos, s)) > 0) {
                break;
            }
            if (pos < 5) {
                if ((count = swap_and_check(pos + 1, pos, s)) > 0) {
                    break;
                }
            }
            if (pos > 3) {
                if ((count = swap_and_check(pos - 1, pos, s)) > 0) {
                    break;
                }
            }
            if ((count = swap_and_check(pos + 3, pos, s)) > 0) {
                break;
            }
        }
        else {
            if ((count = swap_and_check(pos - 3, pos, s)) > 0) {
                break;
            }
            if (pos < 8) {
                if ((count = swap_and_check(pos + 1, pos, s)) > 0) {
                    break;
                }
            }
            if (pos > 6) {
                if ((count = swap_and_check(pos - 1, pos, s)) > 0) {
                    break;
                }
            }
        }
    }

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

    return 0;
}