Problem DescriptionThe 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: 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: 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. InputYou 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 OutputYou 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. Do not print a blank line between cases. Sample Input
2 3 4 1 5 x 7 6 8 Sample Output
ullddrurdllurdruldr |
思路分析:
本题的题目叙述与POJ1077完全一样, 不过本题存在多组测试数据, 适合先使用先进先出队列BFS并结合康托展开计算所有从目标状态逆向可达的最短路径(空格使用数字9表示), 具体实现见如下AC代码:
//HDOJ1043_Eight
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <functional>
#include <cmath>
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
const int MAX = 4, INF = 0x3f3f3f3f, LEN = 1e6;
const int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
const char dir[4] = {'u', 'd', 'l', 'r'};
const int TARGET[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
const int POS[10][2] = {{0, 0}, {1, 1}, {1, 2}, {1, 3}
, {2, 1}, {2, 2}, {2, 3}
, {3, 1}, {3, 2}, {3, 3}};
int ph[10];//ph[i]: i!的值
int ndir[200];//ndir[i]: 方向i的反方向
int dis[LEN];//dis[i]: 起始状态变换至排列i所需的最少变换次数
char last[LEN];//last[i]: 起始状态变换至排列i最少步数对应的上一步操作
//将一位数组one转换为对应的二维数组two
void toTwo(const int one[10], int two[4][4]){
for(int i = 1, k = 1; i <= 3; ++i)
for(int j = 1; j <= 3; ++j) two[i][j] = one[k++];
}
//将二维数组two转换为对应的一位数组one
void toOne(const int two[4][4], int one[10]){
for(int i = 1, k = 1; i <= 3; ++i)
for(int j = 1; j <= 3; ++j) one[k++] = two[i][j];
}
//返回全排列arr康拓展开的值
int ct(const int arr[10]){
int res = 0;
for(int i = 1, j = 8; i <= 9; ++i, --j){
int t = arr[i] - 1;
for(int k = i - 1; k >= 1; --k) if(arr[k] < arr[i]) --t;
res += t * ph[j];
}
return res;
}
//还原val对应的全排列值arr
void rect(int val, int arr[10]){
vector<int> ok; for(int i = 1; i <= 9; ++i) ok.push_back(i);
for(int i = 1, j = 8; i <= 9; ++i, --j){
int t = val / ph[j]; val %= ph[j];
arr[i] = ok[t], ok.erase(ok.begin() + t);
}
}
//返回arr中空格(9)的位置
void getSpace(const int arr[4][4], int &x, int &y){
for(int i = 1; i <= 3; ++i)
for(int j = 1; j <= 3; ++j)
if(arr[i][j] == 9){
x = i, y = j; return;
}
}
//计算所有从tar逆向可达状态的最少步数
void bfs(){
memset(dis, 0x3f, sizeof(dis)), memset(last, 0, sizeof(last));
int beg = ct(TARGET);
queue<int> qu; qu.push(beg), dis[beg] = 0;
while(qu.size()){
int z = qu.front(); qu.pop();
int one[10], two[4][4]; rect(z, one), toTwo(one, two);
int x, y; getSpace(two, x, y);
for(int i = 0; i <= 3; ++i){
int a = x + dx[i], b = y + dy[i];
if(a >= 1 && a <= 3 && b >= 1 && b <= 3){
swap(two[a][b], two[x][y]), toOne(two, one);
int t = ct(one);
if(dis[t] != INF){
swap(two[a][b], two[x][y]); continue;
}
dis[t] = dis[z] + 1, last[t] = dir[i], qu.push(t);
swap(two[a][b], two[x][y]);
}
}
}
}
int main(){
ndir['u'] = 1, ndir['d'] = 0, ndir['l'] = 3, ndir['r'] = 2;
ph[0] = 1; for(int i = 1; i <= 9; ++i) ph[i] = ph[i - 1] * i;
bfs();
char s[MAX * MAX];
while(gets(s)){
int begarr[10];
for(int i = 1, k = 0; i <= 9; ++i){
while(s[k] == ' ') ++k;
begarr[i] = s[k] == 'x'? 9: s[k] - '0', ++k;
}
int beg = ct(begarr);//初始状态对应的康拓展开值
if(dis[beg] == INF){
cout << "unsolvable" << endl; continue;
}
vector<char> path;
int two[4][4], one[10]; toTwo(begarr, two);
for(int i = last[beg], sta = beg; i; ){
path.push_back(dir[ndir[i]]);
int x, y; getSpace(two, x, y);
int k = ndir[i];
swap(two[x][y], two[x + dx[k]][y + dy[k]]);
toOne(two, one), sta = ct(one), i = last[sta];
}
for(int i = 0; i < path.size(); ++i) cout << path[i];
cout << endl;
}
return 0;
}