NC51032 八数码
题目
题目描述
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 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 puzzle
1 2 3
x 4 6
7 5 8
is described by this list:
1 2 3 x 4 6 7 5 8
输出描述
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.
示例1
输入
2 3 4 1 5 x 7 6 8
输出
ullddrurdllurdruldr
题解
知识点:BFS。
很显然用bfs搜索,但状态保存是个问题,可以用c++自带的map进行状态保存,也可以用康托展开对局面字符串转化为整型保存(我居然还不会qwq)。
要注意的是,数据再复杂点可以卡普通的bfs,这时候需要优化搜索,可以用双向bfs或者A*,可以节省大量时间。这里我用了双向bfs(因为不会A*2333)。
最后注意无解情况可能超时,建议计数跳出(也有逆序对的方法)。
时间复杂度 \(O(?)\)
空间复杂度 \(O(1)\)
代码
#include <bits/stdc++.h>
using namespace std;
struct node {
string s;
int x, y;
};
const int dir[4][2] = { {-1,0},{0,-1},{0,1},{1,0} };
char dirs[4] = { 'u','l','r','d' };
string bfs(node init, node ans) {
map<string, string> vis1, vis2;
vis1[init.s] = "";
vis2[ans.s] = "";
queue<node> q1, q2;
q1.push(init);
q2.push(ans);
int cnt = 0;
while (!q1.empty() && !q2.empty()) {
if (cnt >= 10000) break;
cnt++;
node a = q1.front();
node b = q2.front();
q1.pop();
q2.pop();
if (vis2.count(a.s)) return vis1[a.s] + vis2[a.s];
else if (vis1.count(b.s)) return vis1[b.s] + vis2[b.s];
for (int i = 0;i < 4;i++) {
node aa;
aa.x = a.x + dir[i][0];
aa.y = a.y + dir[i][1];
if (aa.x >= 0 && aa.x < 3 && aa.y >= 0 && aa.y < 3) {
aa.s = a.s;
swap(aa.s[a.x * 3 + a.y], aa.s[aa.x * 3 + aa.y]);
if (!vis1.count(aa.s)) vis1[aa.s] = vis1[a.s] + dirs[i], q1.push(aa);
}
node bb;
bb.x = b.x + dir[i][0];
bb.y = b.y + dir[i][1];
if (bb.x >= 0 && bb.x < 3 && bb.y >= 0 && bb.y < 3) {
bb.s = b.s;
swap(bb.s[b.x * 3 + b.y], bb.s[bb.x * 3 + bb.y]);
if (!vis2.count(bb.s)) vis2[bb.s] = dirs[3 - i] + vis2[b.s], q2.push(bb);
}
}
}
return "unsolvable";
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
node init, ans;
for (int i = 0;i < 9;i++) {
char tmp;
cin >> tmp;
init.s += tmp;
if (tmp == 'x') {
init.x = i / 3;
init.y = i % 3;
}
}
ans.s = "12345678x";
ans.x = 2;
ans.y = 2;
cout << bfs(init, ans) << '\n';
return 0;
}
本文来自博客园,作者:空白菌,转载请注明原文链接:https://www.cnblogs.com/BlankYang/p/16484692.html