hdu1043Eight (经典的八数码)(康托展开+BFS)

分类: 广搜
2013-08-13 21:20 
249人阅读 
评论(0) 
收藏 
举报
树状
BFS
建议先学会用康托展开:http://blog.csdn.net/u010372095/article/details/9904497

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

2 3 4 1 5 x 7 6 8

Sample Output

ullddrurdllurdruldr
[cpp]
view plain
copy
print
?

  1. #include<stdio.h>  
  2. #include<iostream>  
  3. #include<queue>  
  4. using namespace std;  
  5. typedef struct nn  
  6. {  
  7.     char way;//记录路径  
  8.     int fath;//记录父节点  
  9. }node1;  
  10. typedef struct nod  
  11. {  
  12.     int aa[10];  
  13.     int n,son;//n为9在aa中的位置  
  14. }node2;  
  15. int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}},fac[10];  
  16. node1 Node[370000];//节点  
  17. void set_fac()//计算0到8的阶层  
  18. {  
  19.     fac[0]=1;  
  20.     for(int i=1;i<=8;i++)  
  21.     fac[i]=fac[i-1]*i;//printf(“%d”,fac[8]);  
  22. }  
  23. int cantor(int aa[])//康托展开  
  24. {  
  25.     int i,j,ans=0,k;  
  26.     for(i=0;i<9;i++)  
  27.     {  
  28.         k=0;  
  29.         for(j=i+1;j<9;j++)  
  30.         if(aa[i]>aa[j])  
  31.         k++;  
  32.         ans+=k*fac[8-i];  
  33.     }  
  34.     return ans;  
  35. }  
  36. void bfs(int a[])  
  37. {  
  38.     queue<node2>Q;  
  39.     node2 q,p;  
  40.     int e,tx,ty,tem,t=0;  
  41.     for(e=0;e<9;e++) q.aa[e]=a[e];  
  42.     q.n=8;q.son=0;  
  43.     Node[q.son].fath=0;//把最终父节点记为0,也就是本身  
  44.     Q.push(q);  
  45.     while(!Q.empty())  
  46.     {  
  47.         q=Q.front(); Q.pop();  
  48.         for(e=0;e<4;e++)  
  49.         {  
  50.             p=q;  
  51.             tx=q.n%3+dir[e][0];ty=q.n/3+dir[e][1];  
  52.             if(tx>=0&&ty>=0&&tx<3&&ty<3)  
  53.             {  
  54.                 p.n=ty*3+tx;  
  55.                 tem=p.aa[p.n];p.aa[p.n]=p.aa[q.n];p.aa[q.n]=tem;  
  56.                 p.son=cantor(p.aa);  
  57.                 if(Node[p.son].fath==-1)//为-1时表示这个点没有访问过,那么放入队列  
  58.                 {  
  59.                     Node[p.son].fath=q.son;//当前节点的父节点就是上一个节点  
  60.                     if(e==0)Node[p.son].way=‘l’;//一定要注意了,e=0是向右走,但我们是要往回搜,所以为了在输出时不用再进行转换,直接记录相反的方向  
  61.                     if(e==1)Node[p.son].way=‘r’;  
  62.                     if(e==2)Node[p.son].way=‘u’;  
  63.                     if(e==3)Node[p.son].way=‘d’;  
  64.                     Q.push(p);  
  65.                 }  
  66.             }  
  67.         }  
  68.     }  
  69. }  
  70. int main()  
  71. {  
  72.     int i,j,s,ss[10],a[10];  
  73.     char ch[50] ;  
  74.     for(i=0;i<9;i++)//目标  
  75.         a[i]=i+1;  
  76.     for(i=0;i<370000;i++)  
  77.     Node[i].fath=-1;  
  78.     set_fac();//计算阶层  
  79.         bfs(a);//开始从目标建立一树  
  80.   
  81.     while(gets(ch)>0)  
  82.     {  
  83.         for(i=0,j=0;ch[i]!=‘\0’;i++)//把字符串变成数子  
  84.         {  
  85.              if(ch[i]==‘x’)  
  86.             ss[j++]=9;  //把x变为数子9  
  87.             else if(ch[i]>=‘0’&&ch[i]<=‘8’)  
  88.             ss[j++]=ch[i]-‘0’;  
  89.         }  
  90.         s=cantor(ss);//算出初态康托值  
  91.        if(Node[s].fath==-1) {printf(“unsolvable\n”);continue;}//不能变成目标  
  92.          
  93.         while(s!=0)  
  94.         {  
  95.             printf(“%c”,Node[s].way);  
  96.             s=Node[s].fath;  
  97.         }  
  98.         printf(“\n”);  
  99.     }  
  100. }  
  101. /* 
  102. 1 2 3 4 5 6 7 8 x 
  103.  
  104. 2 1  4 3 5 x 6 8 7 
  105. unsolvable 
  106. 2 1  4 3 5 x 6 8 7 
  107. drdlurdruldruuldlurrdd 
  108. 8 5 6 4 x 3 4 1 2 
  109. rulddruulddluurddrulldrurd 
  110. 8 5 6 4 x 3 4 1 2 
  111. urdluldrurdldruulddluurddr 
  112.  
  113. */  
    原文作者:BFS
    原文地址: https://blog.csdn.net/pi9nc/article/details/27580893
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞