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:
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.
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 puzzle
is described by this list:
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.
Sample Input
2 3 4 1 5 x 7 6 8
Sample Output
ullddrurdllurdruldr
题目大意:自己看啦
提交情况:T T T TT T T T T T T T T T T TT T AC...
目前只是用了BFS做了,有空要把A*补上
该说的都在注释里面
这里说一句,如果循环很多次的话,一个微小的声明的区别都会导致250ms的差别,
只能说姿势很重要,如果姿势不过关只能考研自己的人品看看能不能卡着数据过了
下面是BFS的AC代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#define aim 0
using namespace std;
bool cantor[400000];
int fac[10]={1,1,2,6,24,120,720,5040,40320,362880};//0!1!2!3!4! 5! 6! 7! 8! 9!
int xd[4]={0,0,-1,1},yd[4]={-1,1,0,0}; // 移动方向上下左右,与下面的dir相意义对应
char dir[4]={'u','d','l','r'}; //上下左右
struct node
{
int s[10];
int pos,a,b;
int state;
string str;
node(){
memset(s,0,sizeof(s));
}
}start,e;
int hashint(int C[])
{
int i,j;
int sum=0;
for(i=0;i<9;i++){
int res=0;
for(j=i+1;j<9;j++){
if(C[j]<C[i]){
res++;
}
}
sum+=res*fac[8-i];
}
return sum;
}
int main()
{
int x;
char ch;
for(int k=0;k<9;){
scanf("%c",&ch);
if(ch=='x'){
start.s[k]=9;
start.pos=k++;
}
else if(ch<='9'&&ch>='1'){
start.s[k++]=ch-'0';
}
}
start.a=start.pos%3;
start.b=start.pos/3;
memset(cantor,false,sizeof(cantor));
start.state=hashint(start.s);
cantor[start.state]=1;
queue<node> q;
q.push(start);
while(!q.empty()){
node cur=q.front();
/*这道题果真还是应该用A*来解,
单纯bfs的时间卡得非常的紧,
牛神拿了我原来T的代码看了之后就按照我的思路敲了一遍结果AC了,
经过我无数遍的比对,发现问题就在这一句,如果使用这一句来申明队列,
那么提交时间是750ms,如果使用下面那两句,则是T,
这个真的是看人品...只能说出题人是想要卡bfs的只是卡得还不够紧。
看来还是姿势第一啊。没有姿势就只能赌人品了
*/
/*node cur;
cur=q.front();*/
q.pop();
for(int i=0;i<4;i+=1){
int x,y;
x=cur.a+xd[i],y=cur.b+yd[i];//数组不越界
if(x<3&&y<3&&x>=0&&y>=0){
node tempt=cur;
tempt.s[y*3+x]=cur.s[cur.b*3+cur.a];
tempt.s[cur.b*3+cur.a]=cur.s[y*3+x];
tempt.state=hashint(tempt.s);
if(!cantor[tempt.state]){
cantor[tempt.state]=true;
tempt.str=cur.str+dir[i];
tempt.a=x;
tempt.b=y;
if(tempt.state==aim){
e=tempt;
cout<<e.str<<endl;
return 0;
}
q.push(tempt);
}
}
}
}
cout<<"unsolvable"<<endl;
return 0;
}