现在的位置: 首页 > 综合 > 正文

HDU1043:Eight(A*+康托)

2018年04月02日 ⁄ 综合 ⁄ 共 5913字 ⁄ 字号 评论关闭

From:

http://blog.csdn.net/libin56842/article/details/23185751

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

 

 

了解了一下A*的算法,并用A*做了一下这道题,A*的关键在于F=H+G这个式子,有一定基础的人在网上随便找篇文章看下很容易便能理解其核心思想了

对于八数码这么经典的题目也不多说了,上代码

 

  1. #include <stdio.h>   
  2. #include <string.h>   
  3. #include <math.h>
      
  4. #include <queue>   
  5. #include <stack>   
  6. #include <algorithm>   
  7. using namespace std;  
  8.   
  9. struct node  
  10. {  
  11.     int f,h,g;  
  12.     int x,y;  
  13.     char map[5][5];  
  14.     friend bool operator< (const node &a,const node &b)  
  15.     {  
  16.         if(a.f==b.f)  
  17.             return a.g<b.g;  
  18.         return a.f>b.f;  
  19.     }  
  20. };  
  21.   
  22. char str[100];  
  23. node start,next;  
  24. int hash[15];  
  25. int pos[][2]= {{0,0},{0,1},{0,2},{1,0},{1,1},{1,2},{2,0},{2,1},{2,2}};  
  26. bool vis[500000];  
  27. char ans[500000];  
  28. int pre[500000];  
  29. int to[4][2] = {1,0,0,1,-1,0,0,-1};  
  30. char to_c[10] = "drul";  
  31.   
  32. int check()//判断不可能的状况
      
  33. {  
  34.     int i,j,k;  
  35.     int s[20];  
  36.     int cnt = 0;  
  37.     for(i = 0; i<3; i++)  
  38.     {  
  39.         for(j = 0; j<3; j++)  
  40.         {  
  41.             s[3*i+j] = start.map[i][j];  
  42.             if(s[3*i+j] == 'x')  
  43.                 continue;  
  44.             for(k = 3*i+j-1; k>=0; k--)  
  45.             {  
  46.                 if(s[k] == 'x')  
  47.                     continue;  
  48.                 if(s[k]>s[3*i+j])  
  49.                     cnt++;  
  50.             }  
  51.         }  
  52.     }  
  53.     if(cnt%2)  
  54.         return 0;  
  55.     return 1;  
  56. }  
  57.   
  58. int solve(node a)//康托
      
  59. {  
  60.     int i,j,k;  
  61.     int s[20];  
  62.     int ans = 0;  
  63.     for(i = 0; i<3; i++)  
  64.     {  
  65.         for(j = 0; j<3; j++)  
  66.         {  
  67.             s[3*i+j] = a.map[i][j];  
  68.             int cnt = 0;  
  69.             for(k = 3*i+j-1; k>=0; k--)  
  70.             {  
  71.                 if(s[k]>s[3*i+j])  
  72.                     cnt++;  
  73.             }  
  74.             ans = ans+hash[i*3+j]*cnt;  
  75.         }  
  76.     }  
  77.     return ans;  
  78. }  
  79.   
  80. int get_h(node a)//得到H值
      
  81. {  
  82.     int i,j;  
  83.     int ans = 0;  
  84.     for(i = 0; i<3; i++)  
  85.     {  
  86.         for(j = 0; j<3; j++)  
  87.         {  
  88.             if(a.map[i][j] == 'x')  
  89.                 continue;  
  90.             int k = a.map[i][j]-'1';  
  91.             ans+=abs(pos[k][0]-i)+abs(pos[k][1]-j);  
  92.         }  
  93.     }  
  94.     return ans;  
  95. }  
  96.   
  97. void bfs()  
  98. {  
  99.     memset(vis,0,sizeof(vis));  
  100.     priority_queue<node> Q;  
  101.     start.g = 0;  
  102.     start.h = get_h(start);  
  103.     start.f = start.h;  
  104.     Q.push(start);  
  105.     while(!Q.empty())  
  106.     {  
  107.         node a = Q.top();  
  108.         Q.pop();  
  109.         int k_s = solve(a);  
  110.         for(int i = 0; i<4; i++)  
  111.         {  
  112.             next = a;  
  113.             next.x+=to[i][0];  
  114.             next.y+=to[i][1];  
  115.             if(next.x < 0 || next.y < 0 || next.x>2 || next.y > 2)  
  116.             continue;  
  117.             next.map[a.x][a.y] = a.map[next.x][next.y];  
  118.             next.map[next.x][next.y] = 'x';  
  119.             next.g+=1;  
  120.             next.h = get_h(next);  
  121.             next.f = next.g+next.h;  
  122.             int k_n = solve(next);  
  123.             if(vis[k_n])  
  124.                 continue;  
  125.             vis[k_n] = true;  
  126.             Q.push(next);  
  127.             pre[k_n] = k_s;  
  128.             ans[k_n] = to_c[i];  
  129.             if(k_n == 0)  
  130.                 return ;  
  131.         }  
  132.     }  
  133. }  
  134.   
  135. int main()  
  136. {  
  137.     int i,j,len,x,y;  
  138.     hash[0] = 1;  
  139.     for(i = 1; i<=9; i++)  
  140.         hash[i] = hash[i-1]*i;  
  141.     while(gets(str))  
  142.     {  
  143.         x = y = 0;  
  144.         len = strlen(str);  
  145.         for(i = 0; i<len; i++)  
  146.         {  
  147.             if(str[i]>='0' && str[i]<='9' || str[i]=='x')  
  148.             {  
  149.                 start.map[x][y] = str[i];  
  150.                 if(start.map[x][y] == 'x')  
  151.                 {  
  152.                     start.x = x;  
  153.                     start.y = y;  
  154.                 }  
  155.                 y++;  
  156.                 if(y==3)  
  157.                 {  
  158.                     y = 0;  
  159.                     x++;  
  160.                 }  
  161.             }  
  162.         }  
  163.         if(!check())  
  164.         {  
  165.             printf("unsolvable\n");  
  166.             continue;  
  167.         }  
  168.         int sa = solve(start);  
  169.         bfs();  
  170.         stack<int> s;  
  171.         int now = 0;  
  172.         while(sa!=now)  
  173.         {  
  174.             s.push(ans[now]);  
  175.             now = pre[now];  
  176.         }  
  177.         while(!s.empty())  
  178.         {  
  179.             putchar(s.top());  
  180.             s.pop();  
  181.         }  
  182.         printf("\n");  
  183.     }  
  184.   
  185.     return 0;  
  186. }  

 

抱歉!评论已关闭.