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
第一种IDA*代码:
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#include<vector>
#include<iomanip>
#include<queue>
using namespace std;
struct S {
char maze[3][3];
int x,y;
int g,h,f;
S() {}
S(const S& ts) {
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
maze[i][j]=ts.maze[i][j];
}
}
x=ts.x;
y=ts.y;
}
};
const int pos[][2]= {{0,0},{0,1},{0,2},{1,0},{1,1},{1,2},{2,0},{2,1},{2,2}};
inline int ABS(int x) {
if(x<0) return-x;
else return x;
}
inline int h(S ts) {
int val=0;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if(ts.maze[i][j]=='x') continue;
int c=ts.maze[i][j]-'1';
val+=ABS(pos[c][0]-i)+ABS(pos[c][1]-j);
}
}
return val;
}
int fac[]= {1,1,2,6,24,120,720,5040,40320};
inline int inv_hash(S ts) {
char str[10];
int ans=0;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
str[i*3+j]=ts.maze[i][j];
int cnt=0;
for(int k=i*3+j-1; k>=0; k--) {
if(str[k]>str[i*3+j]) cnt++;
}
ans+=fac[i*3+j]*cnt;
}
}
return ans;
}
int dir[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
int ans[50],deep;
bool vis[363000];
S ts;
bool dfs(int d) {
if(h(ts)==0) return true;
if(h(ts)+d>deep) return false;
int x=ts.x;
int y=ts.y;
for(int i=0; i<4; ++i) {
int tx=x+dir[i][0];
int ty=y+dir[i][1];
if(tx<0||tx>=3||ty<0||ty>=3)
continue;
ts.maze[x][y]=ts.maze[tx][ty];
ts.maze[tx][ty]='x';
int tmp=inv_hash(ts);
if(vis[tmp]) {
ts.maze[tx][ty]=ts.maze[x][y];
ts.maze[x][y]='x';
continue;
}
vis[tmp]=true;
ts.x=tx;ts.y=ty;
ans[d]=i;
if(dfs(d+1)) return true;
vis[tmp]=false;
ts.x=x,ts.y=y;
ts.maze[tx][ty]=ts.maze[x][y];
ts.maze[x][y]='x';
}
return false;
}
S s;
bool inv_check() {
char str[10];
int cnt=0;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
str[i*3+j]=s.maze[i][j];
if(str[i*3+j]=='x') continue;
for(int k=i*3+j-1; k>=0; k--) {
if(str[k]=='x') continue;
if(str[k]>str[i*3+j]) cnt++;
}
}
}
return!(cnt&1);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
char in[100];
while(gets(in)) {
for(int i=0,x=0,y=0; in[i]; i++) {
if((in[i]<='9'&&in[i]>='0')||in[i]=='x') {
s.maze[x][y]=in[i];
if(in[i]=='x') {
s.x=x;
s.y=y;
}
y++;
if(y==3) y=0,x++;
}
}
if(!inv_check()) {
puts("unsolvable");
continue;
}
memset(vis,false,sizeof(vis));
vis[inv_hash(s)]=true;
ts=s;
deep=0;
while(true) {
if(dfs(0)) break;
deep++;
}
for(int i=0; i<deep; i++) {
if(ans[i]==0) putchar('d');
else if(ans[i]==1) putchar('r');
else if(ans[i]==2) putchar('u');
else if(ans[i]==3) putchar('l');
}
puts("");
}
return 0;
}
第二中A*代码:
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#include<vector>
#include<iomanip>
#include<queue>
using namespace std;
struct S {
char maze[3][3];
int x,y;
int g,h,f;
S() {}
S(const S& ts) {
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
maze[i][j]=ts.maze[i][j];
}
}
x=ts.x;
y=ts.y;
g=ts.g;
h=ts.h;
f=ts.f;
}
friend bool operator< (const S& a,const S& b) {
if(a.f==b.f) return a.g<b.g;
return a.f>b.f;
}
} s;
const int fac[]= {1,1,2,6,24,120,720,5040,40320};
bool vis[363000];
int pre[363000];
char op[363000];
inline int inv_hash(S ts) {
char str[10];
int ans=0;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
str[i*3+j]=ts.maze[i][j];
int cnt=0;
for(int k=i*3+j-1; k>=0; k--) {
if(str[k]>str[i*3+j]) cnt++;
}
ans+=fac[i*3+j]*cnt;
}
}
return ans;
}
const int pos[][2]= {{0,0},{0,1},{0,2},{1,0},{1,1},{1,2},{2,0},{2,1},{2,2}};
inline int ABS(int x) {
return x<0?-x:x;
}
inline int h(S ts) {
int val=0;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if(ts.maze[i][j]=='x') continue;
int c=ts.maze[i][j]-'1';
val+=ABS(pos[c][0]-i)+ABS(pos[c][1]-j);
}
}
return val;
}
const int dir[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
bool bfs() {
memset(vis,false,sizeof(vis));
priority_queue<S> que;
que.push(s);
while(!que.empty()) {
S u=que.top();
que.pop();
int ihu=inv_hash(u);
for(int i=0; i<4; i++) {
S v=u;
v.x+=dir[i][0];
v.y+=dir[i][1];
if(v.x<0||v.y<0||v.x>=3||v.y>=3) continue;
v.maze[u.x][u.y]=u.maze[v.x][v.y];
v.maze[v.x][v.y]='x';
v.g+=1;
v.h=h(v);
v.f=v.g+v.h;
int ihv=inv_hash(v);
if(vis[ihv]) continue;
vis[ihv]=true;
pre[ihv]=ihu;
if(i==0) op[ihv]='d';
else if(i==1) op[ihv]='r';
else if(i==2) op[ihv]='u';
else if(i==3) op[ihv]='l';
if(ihv==0) return true;
que.push(v);
}
}
return false;
}
inline bool inv_check() {
char str[10];
int cnt=0;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
str[i*3+j]=s.maze[i][j];
if(str[i*3+j]=='x') continue;
for(int k=i*3+j-1; k>=0; k--) {
if(str[k]=='x') continue;
if(str[k]>str[i*3+j]) cnt++;
}
}
}
return!(cnt&1);
}
char in[100];
char stk[100];
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
while(gets(in)) {
for(int i=0,x=0,y=0; in[i]; i++) {
if((in[i]<='9'&&in[i]>='0')||in[i]=='x') {
s.maze[x][y]=in[i];
if(in[i]=='x') {
s.x=x;
s.y=y;
}
y++;
if(y==3) y=0,x++;
}
}
if(!inv_check()) {
puts("unsolvable");
continue;
}
s.g=0;
s.h=h(s);
s.f=s.h;
int shash=inv_hash(s);
if(shash==0) {
puts("");
continue;
}
bfs();
int top=-1,thash=0;
while(thash!=shash) {
stk[++top]=op[thash];
thash=pre[thash];
}
for(int i=top; i>=0; i--) {
putchar(stk[i]);
}
puts("");
}
}