Eight
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17589 Accepted Submission(s): 4795
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 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
Sample Output
从来没有接触过这种搜索题,是在看了别人的题解的提示以后知道怎
么做的。看了别人的题解,觉得高明之处在于使用康托展开判重。按照我最初的想法,把每个数字的摆放看作是一种状态嘛,我直接去搜索就可以了,但是还是有很多麻烦,而使用康托展开直接把当前状态hash省去了很多不必要的麻烦
。
值得一提的是这个题目是特殊判断,之前没有注意,算出结果了以为自己是错的。这个题我并不是直接去搜索,而是反向搜索去遍历每一种状态,并记录路径,然后根据输入查表解决,按照康托+BFS直TLE。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <ctype.h>
#include <queue>
using namespace std;
const int N=400000;//最多的状态也就是9!
struct node{
int s[9];
int loc;
int status;//康托哈希
string path;
};
int vis[N];//存储状态的访问情况
node initcur;//初始状态
//康托判重
//阶乘0! 1! 2! 3! 4! 5! 6! 7! 8! 9!
int fact[]={1,1,2,6,24,120,720,5040,40320,362880};
int Cantor(int s[],int n){
int i,j,sum=0,num;
for(i=0;i<n;i++){
num=0;
for(j=i+1;j<n;j++){
if(s[i]>s[j])
num++;
}
sum+=num*fact[n-i-1];
}
return sum;
}
//方向 上 右 下 左
int dic[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
char index[5]="udlr";
string path;
string allpath[N];
int solve(){
queue<node> Q;
int aim=initcur.status;//结束状态的hash
while(!Q.empty()){
Q.pop();
}
node next;
node cur={{1,2,3,4,5,6,7,8,0},8,46233,""};
Q.push(cur);
while(!Q.empty()){
cur=Q.front();
Q.pop();
int x,y;
x=cur.loc/3;
y=cur.loc%3;
for(int i=0;i<4;i++){
int dx=x+dic[i][0];
int dy=y+dic[i][1];
if(0<=dx&&dx<3&&0<=dy&&dy<3){
next=cur;
next.loc=dx*3+dy;
next.s[cur.loc]=next.s[next.loc];
next.s[next.loc]=0;
next.status=Cantor(next.s,9);
if(!vis[next.status]){
vis[next.status]=1;
next.path=next.path+index[i];
allpath[next.status]=next.path;
Q.push(next);
}
}
}
}
return 0;
}
string solvepath(string path){
string realpath="";
for(int i=path.length()-1;i>=0;i--){
if(path[i]=='u')
realpath+='d';
else if(path[i]=='d'){
realpath+='u';
}
else if(path[i]=='l'){
realpath+='r';
}
else if(path[i]=='r'){
realpath+='l';
}
}
return realpath;
}
int main(){
char tile[30];
memset(vis,0,sizeof(vis));
solve();
while(gets(tile)!=NULL){
int i;
int num=0;
for(i=0;tile[i];i++){
if(isspace(tile[i])){
continue;
}
else{
if(tile[i]=='x'){
initcur.s[num]=0;
initcur.loc=num;
num++;
}
else{
initcur.s[num]=tile[i]-'0';
num++;
}
}
}
initcur.status=Cantor(initcur.s,9);
if(allpath[initcur.status]!=""){
cout<<solvepath(allpath[initcur.status])<<endl;
}
else{
cout<<"unsolvable"<<endl;
}
}
return 0;
}