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: Runtime Error)

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>

#define HEAP_SIZE 10000000
#define HASH_SIZE 10000000
#define SORT_FREQ 100

typedef struct _node {
	unsigned long long data;
	int steps;
	int f;
	struct _node * next;
} node;

unsigned int hash(unsigned long long data) {
	unsigned long long hash = 0;
	hash += data * 9966091;
	return (unsigned int)(hash % HASH_SIZE);
}

node * hash_unique_create(node ** hash_table, unsigned long long data, int steps, int f) {
	unsigned int hash_value = hash(data);
	//std::printf("%016llx, %x, %d\n", data, hash_value, steps);
	node * p = hash_table[hash_value];
	if (p == NULL) {
		node *q = (node*)malloc(sizeof(node));
		q->data = data;
		q->steps = steps;
		q->f = f;
		q->next = NULL;
		hash_table[hash_value] = q;
		return q;
	}
	if (p->data == data) {
		if (p->steps > steps) {
			p->f = f;
			p->steps = steps;
			return p;
		}
		return NULL;
	}
	while (p->next != NULL) {
		p = p->next;
		if (p->data == data) {
			if (p->steps > steps) {
				p->f = f;
				p->steps = steps;
				return p;
			}
			return NULL;
		}
	}

	node *q = (node*)malloc(sizeof(node));
	q->data = data;
	q->steps = steps;
	q->f = f;
	q->next = NULL;
	p->next = q;
	return q;
}

int Mdistance(unsigned long long data) {
	int distance[16];
	for (int j = 1; j < 16; j++) {
		distance[j] = 0;
		for (int i = 0; i < 16; i++) {
			unsigned int a;
			a = data >> (60 - 4 * i);
			a = a % 16;

			if ((int)a == j) {
				distance[j] = 0;
				distance[j] += std::abs(i % 4 - (j - 1) % 4);
				distance[j] += std::abs(i / 4 - (j - 1) / 4);
			}
		}
	}

	int sum = 0;
	for (int j = 1; j < 16; j++) {
		sum += distance[j];
	}
	return sum;
}

void heap_add(node ** heap, int * n, node * a) {
	*n += 1;
	while (*n != 1) {
		if (heap[*n / 2]->f > a->f) {
			heap[*n] = heap[*n / 2];
			*n = *n / 2;
		}
		else {
			break;
		}
	}
	heap[*n] = a;
}

void heap_delete(node ** heap, int * n, int i) {
	if (i == 1) {
		heap[1] = heap[*n];
		*n -= 1;
	}

	if (heap[2 * i] == NULL) {
		return;
	}
	if (heap[2 * i + 1] == NULL) {
		if (heap[i]->f > heap[2 * i]->f) {
			node * tmp = heap[i];
			heap[i] = heap[2 * i];
			heap[2 * i] = tmp;
		}
		return;
	}

	if (heap[i]->f < heap[2 * i]->f && heap[i]->f < heap[2 * i + 1]->f) {
		return;
	}

	if (heap[2 * i]->f < heap[2 * i + 1]->f) {
		node * tmp = heap[i];
		heap[i] = heap[2 * i];
		heap[2 * i] = tmp;
		heap_delete(heap, n, 2 * i);
	}
	else {
		node * tmp = heap[i];
		heap[i] = heap[2 * i + 1];
		heap[2 * i + 1] = tmp;
		heap_delete(heap, n, 2 * i + 1);
	}
}

/*
	At the end, return 1; otherwise, return 0;
*/
int create(unsigned long long data, int steps, int f,
	node ** hash_table, node ** heap, int * n)
{
	node * p = hash_unique_create(hash_table, data, steps, f);
	if (p == NULL) {
		return 0;
	}

	if (data == 0x123456789abcdef0) {
		return 1;
	}

	heap_add(heap, n, p);
	if (*n > HEAP_SIZE) {
		printf("heap is full\n");
		throw 1;
	}

	return 0;
}

int find_zero(unsigned long long data) {
	for (int i = 0; i < 16; i++) {
		unsigned int a;
		a = data >> (60 - 4 * i);
		a = a % 16;

		if (a == 0) {
			return i;
		}
	}
	return 0;
}

// zero does move.

unsigned long long move_right(unsigned long long data, int zero_index, int * dif_f) {
	unsigned long long a;
	a = (data >> (60 - 4 * (zero_index + 1))) % 16;
	if (((int)a - 1) % 4 < (zero_index + 1) % 4) {
		*dif_f = 0;
	}
	else {
		*dif_f = 2;
	}
	a = a << (60 - 4 * (zero_index + 1));
	unsigned long long b;
	b = data - a;
	a = a << 4;
	b += a;

	return b;
}

unsigned long long move_left(unsigned long long data, int zero_index, int * dif_f) {
	unsigned long long a;
	a = (data >> (60 - 4 * (zero_index - 1))) % 16;
	if (((int)a - 1) % 4 > (zero_index - 1) % 4) {
		*dif_f = 0;
	}
	else {
		*dif_f = 2;
	}
	a = a << (60 - 4 * (zero_index - 1));
	unsigned long long b;
	b = data - a;
	a = a >> 4;
	b += a;

	return b;
}

unsigned long long move_down(unsigned long long data, int zero_index,int * dif_f) {
	unsigned long long a;
	a = (data >> (60 - 4 * (zero_index + 4))) % 16;
	if (((int)a - 1) / 4 < (zero_index + 4) / 4) {
		*dif_f = 0;
	}
	else {
		*dif_f = 2;
	}
	a = a << (60 - 4 * (zero_index + 4));
	unsigned long long b;
	b = data - a;
	a = a << 16;
	b += a;

	return b;
}

unsigned long long move_up(unsigned long long data, int zero_index, int * dif_f) {
	unsigned long long a;
	a = (data >> (60 - 4 * (zero_index - 4))) % 16;
	if (((int)a - 1) / 4 > (zero_index - 4) / 4) {
		*dif_f = 0;
	}
	else {
		*dif_f = 2;
	}
	a = a << (60 - 4 * (zero_index - 4));
	unsigned long long b;
	b = data - a;
	a = a >> 16;
	b += a;

	return b;
}

int main() {
	unsigned long long data;
	//unsigned long long goal = 0x123456789abcdef0;
	node ** heap;
	int n;
	node ** hash_table;
	int finished;
	int result;

	heap = (node **)malloc(sizeof(node *) * HEAP_SIZE + 1);
	for (int i = 1; i <= HEAP_SIZE; i++) {
		heap[i] = NULL;
	}
	n = 0;
	hash_table = (node **)malloc(sizeof(node *) * HASH_SIZE);
	for (int i = 0; i < HASH_SIZE; i++) {
		hash_table[i] = NULL;
	}

	data = 0;
	for (int i = 0; i < 16; i++) {
		unsigned int a;
		std::cin >> a;
		data = (data << 4) + a;
	}

	try
	{
		finished = create(data, 0, Mdistance(data), hash_table, heap, &n);
	}
	catch (...)
	{
		return 1;
	}
	if (finished) {
		std::printf("0\n");
		return 0;
	}

	while (1) {
		if (n == 0) {
				std::printf("empty heap\n");
				return 1;
		}

		node * p = heap[1];
		heap_delete(heap, &n, 1);

		unsigned long long new_data;
		int zero_index = find_zero(p->data);
		int dif_f = 0;

		if (zero_index % 4 != 3) {
			new_data = move_right(p->data, zero_index, &dif_f);
			try
			{
				finished += create(new_data, p->steps + 1, p->f + dif_f, hash_table, heap, &n);
			}
			catch (...)
			{
				return 1;
			}
		}
		if (zero_index % 4 != 0) {
			new_data = move_left(p->data, zero_index, &dif_f);
			try
			{
				finished += create(new_data, p->steps + 1, p->f + dif_f, hash_table, heap, &n);
			}
			catch (...)
			{
				return 1;
			}
		}
		if (zero_index < 12) {
			new_data = move_down(p->data, zero_index, &dif_f);
			try
			{
				finished += create(new_data, p->steps + 1, p->f + dif_f, hash_table, heap, &n);
			}
			catch (...)
			{
				return 1;
			}
		}
		if (zero_index > 3) {
			new_data = move_up(p->data, zero_index, &dif_f);
			try
			{
				finished += create(new_data, p->steps + 1, p->f + dif_f, hash_table, heap, &n);
			}
			catch (...)
			{
				return 1;
			}
		}

		if (finished) {
			result = p->steps + 1;
			break;
		}
	}

	std::printf("%d\n", result);

	return 0;
}