Eight
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 26254 Accepted Submission(s): 6980
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:
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, 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
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
Source
Recommend
题意
经典八数码问题。多组数据,给你一串由1~8和x组成的串,问你能否变到最终目标1 2 3 4 5 6 7 8 x。可以则输出方法,否则输出unsolvable。
思路
我用的是最基础的大力BFS预处理之后O(答案长度)回答。由于HDU上Memory Limit只有32MB,用STL的queue怕会MLE,所以我就手写了一遍,但后来写完后发现其实STL的queue也能过,对于可到达1 2 3 4 5 6 7 8 x的方案,我搜了一遍,发现只有18W种左右。对于每种状态大力map一下就行了
Code
#pragma GCC optimize(3)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline void readInt(int &x) {
x=0;int f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
x*=f;
}
inline void readLong(ll &x) {
x=0;int f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
x*=f;
}
/*================Header Template==============*/
vector<int>statu;
map<int,string>way;
int pos,x,pw[10];
char s[2];
struct que {
pair<int,int>array[190010];
int head,tail;
inline void init(){head=tail=0;}
inline bool empty(){return head==tail;}
inline void push(pair<int,int> element){array[tail]=element;tail++;}
inline pair<int,int> front(){return array[head];}
inline void pop(){head++;}
};
que q;
//queue<pair<int,int> >q;
inline int change(vector<int> statu) {
int tmp=0;
for(int i=0;i<9;i++)
tmp=tmp*10+statu[i];
return tmp;
}
double mx=0;
inline void init() {
pw[8]=1;
for(int i=7;i>=0;i--)
pw[i]=pw[i+1]*10;
q.push(make_pair(123456789,8));
way[123456789]="";
while(!q.empty()) {
x=q.front().first;
pos=q.front().second;
q.pop();
// cout<<x<<" "<<pos<<endl;
// system("pause");
string xxx;
int Anoxx;
if(pos>2) {
xxx=way[x];
Anoxx=x+(-pw[pos-3]+pw[pos])*(x/pw[pos-3]%10)+(-pw[pos]+pw[pos-3])*(x/pw[pos]%10);
// cout<<(-pw[pos-3]+pw[pos])*(x/pw[pos-3]%10)<<" "<<(-pw[pos]+pw[pos-3])*(x/pw[pos]%10)<<" "<<x<<" "<<Anoxx<<endl;
// system("pause");
if(way.find(Anoxx)==way.end()) {
xxx+="u";
way[Anoxx]=xxx;
q.push(make_pair(Anoxx,pos-3));
}
}
if(pos<6) {
xxx=way[x];
Anoxx=x+(-pw[pos+3]+pw[pos])*(x/pw[pos+3]%10)+(-pw[pos]+pw[pos+3])*(x/pw[pos]%10);
if(way.find(Anoxx)==way.end()) {
xxx+="d";
way[Anoxx]=xxx;
q.push(make_pair(Anoxx,pos+3));
}
}
if((pos%3)>0) {
xxx=way[x];
Anoxx=x+(-pw[pos-1]+pw[pos])*(x/pw[pos-1]%10)+(-pw[pos]+pw[pos-1])*(x/pw[pos]%10);
if(way.find(Anoxx)==way.end()) {
xxx+="l";
way[Anoxx]=xxx;
q.push(make_pair(Anoxx,pos-1));
}
}
if((pos%3)<2) {
xxx=way[x];
Anoxx=x+(-pw[pos+1]+pw[pos])*(x/pw[pos+1]%10)+(-pw[pos]+pw[pos+1])*(x/pw[pos]%10);
if(way.find(Anoxx)==way.end()) {
xxx+="r";
way[Anoxx]=xxx;
q.push(make_pair(Anoxx,pos+1));
}
}
}
}
int main() {
init();
// cout<<mx;
// cout<<clock()<<"ms"<<endl;
while(scanf("%s",s+1)==1) {
int x=0;
if(s[1]=='x')
x=9;
else
x=s[1]-'0';
for(int i=1;i<9;i++) {
scanf("%s",s+1);
if(s[1]>='1'&&s[1]<='8')
x=x*10+s[1]-'0';
else
x=x*10+9;
}
if(x==123456789) {
puts("lr");
continue;
}
if(!way.count(x)) {
puts("unsolvable");
continue;
}
string ans=way[x];
for(int i=ans.length()-1;i>=0;i--) {
if(ans[i]=='l')
putchar('r');
else if(ans[i]=='r')
putchar('l');
else if(ans[i]=='u')
putchar('d');
else
putchar('u');
}
putchar('\n');
}
return 0;
}