题意:
就是还原八数码。输出操作。
题目:
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, 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
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
分析:
用广搜打表,根据广搜的性质,第一次出现某种状态,即是最短路
注意,方向问题。最终结果倒着输出
(因为此题用搜索即可做出,网上有康拓展开(利用康拓展开判重。通过康拓展开知一共有362879种状态。用逆序数判断可行解见八数码可行解。然后宽搜。)和A*(A*是一种启发式搜索,g为已花代价,h为估计的剩余代价,而A*是根据f=g+h作为估价函数进行排列,也就是优先选择可能最优的节点进行扩展。)做法,自行去看,我不会QAQ)orz
康拓展开:https://www.xuebuyuan.com/3261380.html
A*:https://blog.csdn.net/acm_cxlove/article/details/7745323
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<map>
#include<string>
using namespace std;
map<int,int>book;
map<int,string>ans;
int c[4][2]= {0,1,1,0,0,-1,-1,0};
struct node
{
int x,y;
string s;
int k[3][3];
};
void dfs()
{
queue<node>q;
node u,v;
u.x=2,u.y=2;
int xx=1;
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
u.k[i][j]=xx++;
u.k[2][2]=0;
book[123456780]=1;
q.push(u);
while(!q.empty())
{
v=q.front();
q.pop();
for(int i=0; i<4; i++)
{
int yy=1;
int dx=v.x+c[i][0];
int dy=v.y+c[i][1];
if(dx<0||dy<0||dx>=3||dy>=3)
continue;
for(int o=0; o<3; o++)
for(int j=0; j<3; j++)
{
u.k[o][j]=v.k[o][j];
}
swap(u.k[dx][dy],u.k[v.x][v.y]);
for(int o=0; o<3; o++)
for(int j=0; j<3; j++)
{
yy=yy*10+u.k[o][j];
}
if(book[yy])
continue;
book[yy]=1;
u.x=dx;
u.y=dy;
if(i==0)
u.s=v.s+'l';
else if(i==1)
u.s=v.s+'u';
else if(i==2)
u.s=v.s+'r';
else
u.s=v.s+'d';
q.push(u);
ans[yy]=u.s;
}
}
}
int main()
{
char w[50];
dfs();
while(gets(w))
{
int ant=1;
int len=strlen(w);
for(int i=0; i<len; i++)
{
if(w[i]>='1'&&w[i]<='8')
ant=ant*10+w[i]-'0';
else if(w[i]=='x')
ant*=10;
}
if(book[ant])
{
string m=ans[ant];
int l=m.size();
for(int i=l-1; i>=0; i--)
cout<<m[i];
cout<<endl;
}
else
cout<<"unsolvable"<<endl;
}
return 0;
}