Problem

The 15-puzzle has been around for over 100 years; even if you don’t know it by that name, you’ve seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile missing. Let’s call the missing tile ‘x’; the object of the puzzle is to arrange the tiles so that they are ordered as:

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

where the only legal operation is to exchange ‘x’ with one of the tiles with which it shares an edge. As an example, the following sequence of moves solves a slightly scrambled puzzle:

1  2  3  4     1  2  3  4     1  2  3  4     1  2  3  4
5  6  7  8     5  6  7  8     5  6  7  8     5  6  7  8
9  x 10 12     9 10  x 12     9 10 11 12     9 10 11 12
13 14 11 15    13 14 11 15    13 14  x 15    13 14 15  x
r->            d->            r->

The letters in the previous row indicate which neighbor of the ‘x’ tile is swapped with the ‘x’ tile at each step; legal values are ‘r’,’l’,’u’ and ‘d’, for right, left, up, and down, respectively.

Not all puzzles can be solved; in 1870, a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle, and
frustrating many people. In fact, all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing ‘x’ tile, of course).

In this problem, you will write a program for solving the less well-known 8-puzzle, composed of tiles on a three by three
arrangement.

You will receive, several descriptions of configuration of the 8 puzzle. One description is just a list of the tiles in their initial positions, with the rows listed from top to bottom, and the tiles listed from left to right within a row, where the tiles are represented by numbers 1 to 8, plus ‘x’. For example, this puzzle

1 2 3
x 4 6
7 5 8

is described by this list:

1 2 3 x 4 6 7 5 8

Code

#include <bits/stdc++.h>
using namespace std;

struct Node {
int code;
int a;
};
const int maxn = 370000;
const int dir = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
const char chg = {'d', 'u', 'l', 'r'};

Node st;
int vis[maxn], fact;
int precode[maxn];
char oper[maxn];

void initLookTable() {
fact = 1;
for(int i = 1; i < 9; ++i)
fact[i] = fact[i-1] * i;
}

bool tryInsert(Node &u, bool ins) {
int code = 0;
for(int i = 0; i < 9; ++i) {
int cnt = 0;
for(int j = i + 1; j < 9; ++j)
if(u.a[j] < u.a[i]) ++cnt;
code += fact[8-i] * cnt;
}
u.code = code;
if(!ins) return true;
if(vis) return false;
return vis = true;
}

void bfs() {
for(int i = 0; i < 9; ++i) {
if(i == 8) st.a[i] = 0;
else st.a[i] = i + 1;
}
initLookTable();
tryInsert(st, true);

queue<Node> qu;
qu.push(st);
while(!qu.empty()) {
Node u = qu.front(); qu.pop();
int z;
for(z = 0; z < 9; ++z)
if(!u.a[z]) break;
int x = z / 3, y = z % 3;
for(int i = 0; i < 4; ++i) {
int xx = x + dir[i];
int yy = y + dir[i];
int zz = xx * 3 + yy;
if(xx >= 0 && xx < 3 && yy >= 0 && yy < 3) {
Node v = u;
v.a[zz] = u.a[z];
v.a[z] = u.a[zz];
if(tryInsert(v, true)) {
precode[v.code] = u.code;
oper[v.code] = chg[i];
qu.push(v);
}
}
}
}
}

int main() {
bfs();
char buf; Node cur;
while(~scanf("%s", buf)) {
for(int i = 0; i < 9; ++i) {
if(i > 0) scanf("%s", buf);
if(isalpha(buf)) buf = '0';
cur.a[i] = buf - '0';
}
tryInsert(cur, false);
if(vis[cur.code]) {
int pos = cur.code;
while(pos != st.code) {
putchar(oper[pos]);
pos = precode[pos];
}
puts("");
} else {
puts("unsolvable");
}
}
return 0;
}