Eight

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.

输入描述:

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.
示例1
输入
2 3 4 1 5 x 7 6 8
输出
ullddrurdllurdruldr

Problem solving

这道题有许多种解法。这里我用的是康拓展开。
康拓展开,将一个全排列跟一个整数对应起来。也就是哈希的思想。我们把这个3*3的图当成一个全排列看待即可。
因为最后我们要到达的状态是恒定的(12345678x),所以我们可以通过逆向bfs打一个表得到每种状态到达这个状态的路径。省了时间也省了空间。把x当成0看,就是0-8的全排列了。(其实没看题解之前想到了hash,但是不知道怎么来回转换,康拓展开nb

康拓展开的模板

int cantor(int *a){//获得hash值
   int x=0;
   for(int i=0;i<9;i++){
      int mi=0;
      for(int j=i+1;j<9;j++){
         if(a[j]<a[i])  mi++;
      }
      x+=FAC[9-i-1]*mi;
   }
   return x+1;
}
void decantor(int x,int *a){//通过hash值倒推回全排列
   vector<int> v;
   for(int i=0;i<9;i++) v.pb(i);
   for(int i=0;i<9;i++){
      int r=x%FAC[9-i-1];
      int t=x/FAC[9-i-1];
      x=r;
      sort(v.begin(),v.end());
      a[i]=v[t];
      v.erase(v.begin()+t);
   }
}

Code

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
const int maxn = 4e5+10;
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;
int miday[9],dt[4][2]= {1,0,-1,0,0,1,0,-1},result=46234,vis[maxn];
string path[maxn];
char op[5]="udlr";
int FAC[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880}; 
struct node{
   string path;
   int val,pos;
};
node now,net;
int cantor(int *a){
   int x=0;
   for(int i=0;i<9;i++){
      int mi=0;
      for(int j=i+1;j<9;j++){
         if(a[j]<a[i])  mi++;
      }
      x+=FAC[9-i-1]*mi;
   }
   return x+1;
}
void decantor(int x,int *a){
   vector<int> v;
   for(int i=0;i<9;i++) v.pb(i);
   for(int i=0;i<9;i++){
      int r=x%FAC[9-i-1];
      int t=x/FAC[9-i-1];
      x=r;
      sort(v.begin(),v.end());
      a[i]=v[t];
      v.erase(v.begin()+t);
   }
}
void bfs(){
   queue<node> q;
   for(int i=0;i<8;i++) miday[i]=i+1;
   miday[8]=0;
   now.pos=8;now.val=result;now.path="";vis[result]=1;path[result]="";
   q.push(now);
   while(!q.empty()){
      now=q.front();
      q.pop();
      for(int i=0;i<4;i++){
         int dx=(now.pos)/3+dt[i][0];
         int dy=(now.pos)%3+dt[i][1];
         if(dx<0||dy<0||dx>2||dy>2) continue;
         net=now;net.pos=dx*3+dy;
         decantor(now.val-1,miday);
         swap(miday[now.pos],miday[net.pos]);
         net.val=cantor(miday);
         if(!vis[net.val]){
            vis[net.val]=1;
            net.path=op[i]+net.path;
            q.push(net);
            path[net.val]=net.path;
         }
      }
   }
   return ;
}
int main() {
   ios::sync_with_stdio(0);
   #ifdef Uncle_drew
   freopen("in.txt","r",stdin);
   freopen("out.txt","w",stdout);
   #else
   #endif
   bfs();
   char x;
   while(cin>>x){
      if(x=='x'){
         now.pos=0;
         miday[0]=0;
      }else{
         miday[0]=x-'0';
      }
      for(int i=1;i<9;i++){
         cin>>x;
         if(x=='x'){
            now.pos=i;
            miday[i]=0;
         }else{
            miday[i]=x-'0';
         }
      }
      now.val=cantor(miday);
      if(!vis[now.val]){
         cout<<"unsolvable\n";
      }else{
         cout<<path[now.val]<<endl;
      }
   }
   return 0;
}