日期 2021 / 11 / 3
[题目链接]https://acm.dingbacode.com/showproblem.php?pid=1043
Eight
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 45011 Accepted Submission(s): 11275 Special Judge
Problem 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 xwhere 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, 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 8is 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. Do not print a blank line between cases.
Sample Input
2 3 4 1 5 x 7 6 8
Sample Output
ullddrurdllurdruldr
这个题目是一个八数码问题,搞了我三天时间,八数码问题类似于九宫格,也是排列组合问题中的一种,这个题目要求把一个状态的九宫格还原到初始状态下的九宫格所经历 的路径,这里我用的两个结构体,一个记录结点数和路径,另一个就是记录每一次的九宫格地图已经0在九宫格中的位置,还有此状态下的康拓展开值,康拓展开类似一种哈希 函数,空间换时间,把每一种排列对应于一个值,这里思路还是bfs把每一次向四周遍历后的值进行康拓展开计算看是否遍历过,如果没有则设此结点的父节点为遍历前的状态 最后根据给定状态下的结点一步步还原到最初状态.
#include <bits/stdc++.h>
#define INF 0x7fffffff
#define rep(x, y, z) for (int x = y; x <= z; x++)
#define dec(x, y, z) for (int x = y; x >= z; x--)
#define format(a) memset (a, 0, sizeof(a))
#define swap(a, b) (a ^= b ^= a ^= b)
#define ll long long int
#define ull unsigned long long int
#define uint unsigned int
const int maxn = 1e6 + 10;
using namespace std;
typedef struct Mix {
char way; // 记录路径
int fath; // 记录父节点
} node1;
typedef struct Axl {
int s[10]; // 记录此时的排列
int n; // 表示0在九宫格中的位置
int son; // 表示此排列在字典序中的位置
} node2;
int dir[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} }; // 方向数组
int fac[10];
node1 Node[maxn];
void Fibonacci() { // 计算斐波拉契数组
fac[0] = 1;
for (int i = 1; i <= 8; i++) {
fac[i] = fac[i - 1] * i;
}
}
int cantor(int s[]) { // 康拓展开
int ans, k;
ans = k = 0;
for (int i = 0; i < 9; i++) {
k = 0;
for (int j = i + 1; j < 9; j++) {
if (s[i] > s[j]) {
k++;
}
}
ans += k * fac[8 - i];
}
return ans;
}
void bfs(int a[]) {
queue<node2> q;
node2 x, y;
for (int i = 0; i < 9; i++) {
x.s[i] = a[i]; // 赋值初始路径
}
x.n = 8; // 0在九宫格中的位置
x.son = 0; // 子节点刚开始是初始值排列为0
Node[x.son].fath = 0; // 子节点的父节点为0初始值
q.push(x);
while (q.size()) {
x = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
y = x;
int tx = x.n % 3 + dir[i][0];
int ty = x.n / 3 + dir[i][1];
if (tx >= 0 && tx < 3 && ty >= 0 && ty < 3) {
y.n = ty * 3 + tx;
int temp = y.s[y.n];
y.s[y.n] = y.s[x.n];
y.s[x.n] = temp;
y.son = cantor(y.s);
if (Node[y.son].fath == -1) {
Node[y.son].fath = x.son;
if (i == 0) {
Node[y.son].way = 'l';
}
if (i == 1) {
Node[y.son].way = 'r';
}
if (i == 2) {
Node[y.son].way = 'u';
}
if (i == 3) {
Node[y.son].way = 'd';
}
q.push(y);
}
}
}
}
}
int main(int argc, char *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int str[10], a[10];
int x;
char ch[50];
for (int i = 0; i < 9; i++) {
a[i] = i + 1;
}
for (int i = 0; i < maxn; i++) {
Node[i].fath = -1;
}
Fibonacci();
bfs(a);
while (gets(ch) != NULL) {
for (int i = 0, j = 0; ch[i] != '\0'; i++) {
if (ch[i] == 'x') {
str[j++] = 9;
}
else if (ch[i] >= '0' && ch[i] <= '8') {
str[j++] = ch[i] - '0';
}
}
x = cantor(str); // 求出初态康拓值
if (Node[x].fath == -1) {
cout << "unsolvable" << endl;
continue;
}
while (x != 0) {
cout << Node[x].way;
x = Node[x].fath;
}
cout << endl;
}
return 0;
}