博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1043 八数码 Eight A*算法
阅读量:6833 次
发布时间:2019-06-26

本文共 6994 字,大约阅读时间需要 23 分钟。

Eight

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 10101    Accepted Submission(s): 2684
Special Judge

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
 

 

 

 

 

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define Max(a,b) ((a)>(b)?(a):(b))#pragma comment(linker, "/STACK:16777216")using namespace std ;typedef long long LL ;const int fac[9] = { 1,1,2,6,24,120,720,5040,40320} ; const int d[4][2] = { { 1,0},{-1,0},{ 0,1},{ 0,-1}} ; //四个方向const char direction[4] = { 'd','u','r','l'} ; //输出记录const int end_pos[10][2] = { { 0,0},{ 0,0},{ 0,1},{ 0,2}, { 1,0},{ 1,1},{ 1,2}, { 2,0},{ 2,1},{ 2,2}} ; //最后状态 123 // 456 // 78Xclass Node{ public : char mat[3][3] ; int x ; int y ; int h ; //h(x)=递归层数 int g ; //g(x)=曼哈顿距离和 int f ; //估价函数 f(x) = g(x)+ h(x) int id ; //hash值,用于判重,利用排列序列的康拓展开 int G() ;//求g(x) int cango() ; //剪枝,为什么逆序数必须是偶数,网上很多人问这个问题。 //我的解答: 与X(空白)交换的Y ,编号(index)相差为偶数,即在[ index[X] + 1 .index[Y]-1 ] 区间的数为偶数 ; // 交换前 ,设 [ index[X] + 1 .index[Y]-1 ](zh) ,〉Y有a个,
) ; friend bool operator < (const Node &A ,const Node &B){ if(A.f != B.f) return A.f > B.f ; else return A.g > B.g ; } //建堆,使用C++ STL 优先队列 ,f(x)=g(x)+h(x) , f(x)小的优先级别大,也就是说与最后的状态差异小的优先考虑};Node::Node(vector
s){ int i , j ; for(int k = 0 ;k < s.size() ;k++){ i = k/3 ; j = k%3 ; if(s[k] == 'x'){ this->x = i ; this->y = j ; } mat[i][j] = s[k] ; }}int Node::G(){ int sum = 0 ; for(int i = 0 ;i <= 2 ;i++){ for(int j = 0 ;j <= 2 ;j++){ if(mat[i][j]=='x') continue ; int n = mat[i][j] - '0' ; sum = sum + abs(i-end_pos[n][0]) + abs(j - end_pos[n][1]) ; } } return sum ;}int Node::cango(){ char num[10] ; int n = 0 ,sum = 0; for(int i = 0 ;i <=2 ;i++){ for(int j = 0 ;j <= 2 ;j++){ if(mat[i][j] != 'x') num[++n] = mat[i][j] ; } } for(int i = 1 ;i <= n ;i++){ for(int j =1 ;j < i ;j++){ if(num[j] > num[i]) sum++ ; } } return (sum&1) == 0 ;}int Node::Hash(){ char num[10] ; int n = 0 ,sum ,index = 0 ; for(int i = 0 ;i <=2 ;i++){ for(int j = 0 ;j <= 2 ;j++) num[n++] = mat[i][j] ; } for(int i = 0 ;i < n ;i++){ sum = 0 ; for(int j =0 ;j < i ;j++){ if(num[j] > num[i]) sum++ ; } index += fac[i]*sum ; } return index ;}void Node::out(){ for(int i = 0 ;i <= 2 ;i++){ for(int j = 0 ;j <= 2 ;j++) putchar(mat[i][j]) ; puts("") ; }}int father[363000] ;class App{ private : bool visited[363000] ; Node start ; char opet[363000] ; public : App(){} App(class Node) ; int cango(int ,int) ; int A_star() ; void out() ;};App::App(class Node s){ start = s ; start.h = 0 ; start.g = start.G() ; start.f = start.g + start.h ; start.id = start.Hash() ; memset(visited,0,sizeof(visited)) ; visited[start.id] = 1 ;}int App::cango(int x ,int y){ return 0<=x&&x<=2&&0<=y&&y<=2 ;}int App::A_star(){ if(start.id == 0) return 1 ; if(!start.cango()) return 0; priority_queue
que ; que.push(start) ; while(!que.empty()){ Node now =que.top() ; que.pop() ; if(now.id == 0) return 1 ; for(int i = 0 ;i < 4 ;i++){ int x = now.x + d[i][0] ; int y = now.y + d[i][1] ; if(!cango(x,y)) continue ; Node next = now ; next.x = x ; next.y = y ; swap(next.mat[now.x][now.y],next.mat[next.x][next.y]) ; next.id = next.Hash() ; if(visited[next.id]) continue ; visited[next.id] = 1 ; if(!next.cango()) continue ; next.g = next.G() ; next.h = now.h +1 ; next.f = next.g + next.h ; father[next.id] = now.id ; opet[next.id] = direction[i] ; que.push(next) ; } } return 0 ;}void App::out(){ if(A_star()==0) puts("unsolvable") ; else{ vector
ans ; ans.clear() ; int i = 0 ; while(start.id != i){ ans.push_back(opet[i]) ; i = father[i] ; } for(int i = ans.size()-1 ;i >= 0 ;i--) putchar(ans[i]) ; puts("") ; }}int main(){ char str[20] ; vector
s ; while(gets(str)){ s.clear() ; for(int i = 0 ;i < strlen(str) ;i++){ if(str[i] != ' ') s.push_back(str[i]) ; } Node start ; start = Node(s) ; App app = App(start) ; app.out() ; } return 0 ;}

 

相关知识点:  

A*算法

公式表示为: f(n)=g(n)+h(n),

其中 f(n) 是从初始点经由节点n到目标点的估价函数,
g(n) 是在 中从初始节点到n节点的实际代价,
h(n) 是从n到目标节点最佳路径的估计代价。
保证找到 (最优解的)条件,关键在于估价函数h(n)的选取:
估价值h(n)<= n到目标 的距离实际值,这种情况下,搜索的点数多,搜索范围大,效率低。但能得到最优解。
如果 估价值>实际值, 的点数少,搜索范围小,效率高,但不能保证得到最优解。

 

康托展开

 

{1,2,3,4,...,n}表示1,2,3,...,n的排列如 {1,2,3} 按从小到大排列一共6个 123 132 213 231 312 321

代表的数字 1 2 3 4 5 6 也就是把10进制数与一个排列对应起来。他们间的对应关系可由康托展开来找到。 如我想知道321是{1,2,3}中第几个大的数可以这样考虑 第一位是3,当第一位的数小于3时,那排列数小于321 如 123 213 小于3的数有1,2 所以有2*2!个 再看小于第二位2的 小于2的数只有一个就是1 所以有1*1!=1 所以小于321的{1,2,3}排列数有2*2!+1*1!=5个所以321是第6个大的数。 2*2!+1*1!是康托展开 再举个例子 1324是{1,2,3,4}排列数中第几个大的数 第一位是1小于1的数没有,是0个 0*3! 第二位是3小于3的数有1,2但1已经在第一位了所以只有一个数2 1*2! 第三位是2小于2的数是1,但1在第一位所以有0个数 0*1! 所以比1324小的排列有0*3!+1*2!+0*1!=2个 1324是第三个大数。

转载于:https://www.cnblogs.com/liyangtianmen/p/3448016.html

你可能感兴趣的文章
GNU make manual 翻译(四十七)
查看>>
makefile中变量覆盖的小例子
查看>>
所有类型都从Object类型派生
查看>>
关于MFC和android开发上的一些相近地方
查看>>
Linux下rsync的用法
查看>>
c# DataGridView控件的使用
查看>>
TChart的用法
查看>>
DTP语义组分析
查看>>
(老孙随笔)燃烧青春和诗意的IT人生
查看>>
在PowerDesigner中设计概念模型
查看>>
SQL语句,查询数据库里是否存在某个表
查看>>
CSS常用属性
查看>>
搜索引擎开始「实体搜索」新领域竞争,Google、百度分别发力实体搜索产品
查看>>
让敏捷落地-软件研发管理最佳实践(上海站)
查看>>
【R】大型机Linux系统安装R及bsub提交R任务
查看>>
演练:创建并运行托管代码的单元测试 VS2012
查看>>
C#在托盘显示图标
查看>>
把老赵的页面缓存片断改一下,呵呵
查看>>
没有使用全局变量的必要时,就尽量不要使用全局变量。
查看>>
Extjs- Ext.extend函数的使用
查看>>