Eight

传送门HDU1043

描述

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
5
 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.

输入

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

输出

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.

样例

  • Input
    2 3 4 1 5 x 7 6 8

  • Output
    ullddrurdllurdruldr

题解

  • 题意:3*3的表格,x可以和上下左右交换,最后通过移动x使整个表格有序且x在最后一格。求移动方案,输出路径,没有则输出-1
  • 法一:从输入字符串和目标串开始进行双向bfs(3000+ms)
  • 法二:bfs预处理出目标串到其他可达串的路径,然后O(t)查询,t为答案长度(300+ms)
  • 因为内存有限,把x视为9,用康托展开存储答案。

Code(法一)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//双向bfs+康托
#include
#define INIT(a,b) memset(a,b,sizeof(a))
#define LL long long
using namespace std;
const int inf=0x3f3f3f3f;
const int N=362885;
const int mod=1e9+7;
string title="12345678x";
string mp="111111111";
string ops[2]={"udlr","durl"};
map<int,string> op[2];
int rec[4]={-3,3,-1,1};
int vis[2][N];
int fac[20];
struct node{
string sta;
int xsit;
};
queue que[2];
bool judge(int x,int to){
if(to<0||to>=9)return false;
if((x==2&&to==3)||(x==3&&to==2))return false;
if((x==5&&to==6)||(x==6&&to==5))return false;
return true;
}
int cantor(string a){
int ans=0,c=0;
for(int i=0;i<9;i++){
c=0;
for(int j=i+1;j<9;j++)
if(a[j]
ans+=c*fac[9-i-1];
}
return ans;
}
int solve(int t){
int sum=que[t].size();
node q;
while(sum--){
q=que[t].front();que[t].pop();
int can=cantor(q.sta);
if(vis[t^1][can]) return can;
for(int i=0;i<4;i++){
int to=q.xsit+rec[i];
if(judge(q.xsit,to)){
swap(q.sta[q.xsit],q.sta[to]);
int _can=cantor(q.sta);
if(!vis[t][_can]){
vis[t][_can]=1;
que[t].push((node){q.sta,to});
op[t][_can]=op[t][can]+ops[t][i];
}
swap(q.sta[q.xsit],q.sta[to]);
}
}
}
return -1;
}
void bfs(int t){
INIT(vis,0);
while(!que[0].empty())que[0].pop();
while(!que[1].empty())que[1].pop();
que[0].push((node){mp,t});
que[1].push((node){title,8});
op[0].clear(),op[1].clear();
op[0][cantor(mp)]="";
op[1][0]="";
int ans;
while(que[0].size()||que[1].size()){
int ans1=solve(0);
int ans2=solve(1);
if(ans1!=-1||ans2!=-1){
ans=(ans1==-1)?ans2:ans1;
break;
}
}
reverse(op[1][ans].begin(),op[1][ans].end());
cout<0][ans]<1
][ans]<<endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
fac[0]=1;
for(int i=1;i<10;i++){
fac[i]=i*fac[i-1];
}
while(cin>>mp[0]){
int t=0;
for(int i=1;i<9;i++){
cin>>mp[i];
if(mp[i]=='x') t=i;
}
int sum=0;
for(int i=1;i<9;i++){
if(mp[i]=='x') continue ;
for(int j=i-1;j>=0;j--){
if(mp[j]=='x') continue;
if(mp[j]>mp[i]) sum++;
}
}
if(sum & 1) cout<<"unsolvable"<<endl;//逆序数为奇数则无解
else bfs(t);
}
return 0;
}

Code(法二)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//预处理答案
#include
#define INIT(a,b) memset(a,b,sizeof(a))
#define LL long long
using namespace std;
const int inf=0x3f3f3f3f;
const int N=1e6+7;
const int mod=1e9+7;
string title="12345678x";
string mp="111111111";
string ops="durl";
int rec[4]={-3,3,-1,1};
int pre[N],ans[N],vis[N],fac[10];
struct node{
string sta;
int xsit;
};
queue que[2];
bool judge(int x,int to){
if(to<0||to>=9)return false;
if((x==2&&to==3)||(x==3&&to==2))return false;
if((x==5&&to==6)||(x==6&&to==5))return false;
return true;
}
int cantor(string a){
int ans=0,c=0;
for(int i=0;i<9;i++){
c=0;
for(int j=i+1;j<9;j++)
if(a[j]
ans+=c*fac[9-i-1];
}
return ans;
}
void bfs(){
INIT(vis,0);
INIT(pre,-1);
queueque;
que.push((node){title,8});
int can=cantor(title);
vis[can]=1;
while(!que.empty()){
node q=que.front();que.pop();
for(int i=0;i<4;i++){
int to=q.xsit+rec[i];
can=cantor(q.sta);
if(judge(q.xsit,to)){
swap(q.sta[q.xsit],q.sta[to]);
int _can=cantor(q.sta);
if(!vis[_can]){
vis[_can]=1;
que.push((node){q.sta,to});
ans[_can]=i;
pre[_can]=can;
}
swap(q.sta[q.xsit],q.sta[to]);
}
}
}
}
void print(int c){
if(pre[c]==-1){
cout<<endl;
return;
}
cout<
print(pre[c]);
}
int main(){
fac[0]=1;
for(int i=1;i<10;i++){
fac[i]=i*fac[i-1];
}
bfs();
while(cin>>mp[0]){
int t=0;
for(int i=1;i<9;i++){
cin>>mp[i];
if(mp[i]=='x') t=i;
}
int can=cantor(mp);
if(!vis[can]) cout<<"unsolvable"<<endl;
else print(can);

}
return 0;
}

你可能感兴趣的:(Eight)