Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 30632 | Accepted: 13332 | Special Judge |
Description
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.
Input
You will receive a description of a configuration of the 8 puzzle. The 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 puzzle1 2 3
x 4 6
7 5 8
is described by this list:
1 2 3 x 4 6 7 5 8
Output
You will print to standard output either the word ``unsolvable'', if the puzzle has no solution, or a string consisting entirely of the letters 'r', 'l', 'u' and 'd' that describes a series of moves that produce a solution. The string should include no spaces and start at the beginning of the line.Sample Input
2 3 4 1 5 x 7 6 8
Sample Output
ullddrurdllurdruldr
题解:
经典的八数码问题,这里先用第四境界的算法做一下。
双向广搜就不再赘述了,说说hash函数,用的是康托展开,逆推hash的话用逆康托展开
康托展开函数:X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中,a[i]为整数,并且X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0!。
逆康托展开的话反推就行了,很简单。
#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
#include <map>
#include <stack>
using namespace std;
struct Node {
int status;
int pos;
Node(int s = 0, int p = 0) {
status = s;
pos = p;
}
};
const int MAXN = 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 + 10;
int jie[10] = {1, 1};
int dirx[] = {-1, 1, 0, 0};
int diry[] = {0, 0, -1, 1};
int father[2][MAXN];
bool vis[2][MAXN];
queue<Node> q[2];
bool InRange(int x, int y) {
return x >= 0 && x < 3 && y >= 0 && y < 3;
}
int Hash(string s) {
int ans = 0;
for (int i = 0; i < 9; ++i) {
int rev = 0;
for (int j = i + 1; j < 9; ++j) {
if (s[i] > s[j]) ++rev;
}
ans += rev * jie[8 - i];
}
return ans;
}
string RevHash(int val) {
string ans = "";
bool tag[10] = {};
for (int i = 0; i < 9; ++i) {
int tNum = val / jie[8 - i] + 1;
for (int j = 1; j <= tNum; ++j) {
if (tag[j]) ++tNum;
}
val %= jie[8 - i];
ans += tNum + '0';
tag[tNum] = true;
}
return ans;
}
void PutPath(int mid) {
stack<char> path;
int a = mid;
while (father[0][a] != a) {
string s = RevHash(a);
string fs = RevHash(father[0][a]);
int pos, fpos;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '9') pos = i;
if (fs[i] == '9') fpos = i;
}
if (pos / 3 == fpos / 3) {
if (pos % 3 < fpos % 3) {
path.push('l');
} else {
path.push('r');
}
} else {
if (pos / 3 < fpos / 3) {
path.push('u');
} else {
path.push('d');
}
}
a = father[0][a];
}
while (!path.empty()) {
putchar(path.top());
path.pop();
}
int b = mid;
while (father[1][b] != b) {
string s = RevHash(b);
string ss = RevHash(father[1][b]);
int pos, spos;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '9') pos = i;
if (ss[i] == '9') spos = i;
}
if (pos / 3 == spos / 3) {
if (pos % 3 < spos % 3) {
putchar('r');
} else {
putchar('l');
}
} else {
if (pos / 3 < spos / 3) {
putchar('d');
} else {
putchar('u');
}
}
b = father[1][b];
}
putchar('\n');
}
bool Expand(int id) {
Node h = q[id].front(); q[id].pop();
int x = h.pos / 3, y = h.pos % 3;
for (int i = 0; i < 4; ++i) {
int nx = x + dirx[i];
int ny = y + diry[i];
int npos = nx * 3 + ny;
if (!InRange(nx, ny)) continue;
string ns = RevHash(h.status);
swap(ns[h.pos], ns[npos]);
int hashVal = Hash(ns);
if (!vis[id][hashVal]) {
father[id][hashVal] = h.status;
q[id].push(Node(hashVal, npos));
vis[id][hashVal] = true;
if (vis[1 - id][hashVal]) {
PutPath(hashVal);
return true;
}
}
}
return false;
}
bool DBFS(int st, int ed, int pos) {
memset(vis, 0, sizeof vis);
memset(father, 0, sizeof father);
for (int i = 0; i < 2; ++i)
while (!q[i].empty()) q[i].pop();
father[0][st] = st;
father[1][ed] = ed;
vis[0][st] = true;
vis[1][ed] = true;
q[0].push(Node(st, pos));
q[1].push(Node(ed, 8));
while (!q[0].empty() && !q[1].empty()) {
if (q[0].size() <= q[1].size()) {
if (Expand(0)) return true;
} else {
if (Expand(1)) return true;
}
}
for (int i = 0; i < 2; ++i)
while (!q[i].empty())
if (Expand(i)) return true;
return false;
}
int main() {
#ifdef NIGHT_13
freopen("in.txt", "r", stdin);
#endif
for (int i = 2; i < 10; ++i) {
jie[i] = jie[i - 1] * i;
}
char s[100];
while (gets_s(s) != NULL) {
int len = strlen(s), pos = 0;
string ma = "";
for (int i = 0; i < len; ++i) {
if (s[i] == 'x') {
pos = ma.size();
s[i] = '9';
}
if (s[i] != ' ') {
ma += s[i];
}
}
int rev = 0;
for (int i = 0; i < 9; ++i) {
if (ma[i] == '9') continue;
for (int j = i + 1; j < 9; ++j) {
if (ma[i] > ma[j]) ++rev;
}
}
if (rev & 1) puts("unsolvable");
else DBFS(Hash(ma), 0, pos);
}
return 0;
}