【HDU 1043】Eight(A*启发式搜索算法)
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 45 6 7 89 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 45 6 7 8 5 6 7 8 5 6 7 8 5 6 7 89 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 xr-> 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
题目大意
这题其实和我们经常玩的一个游戏非常的像,看下图:
不过这边所要拼成的目标是这样的;
1 2 3
4 5 6
7 8 x
题目就是给你任意一个中间的转态,问你怎样移动x(x就相当于图中的白块,可以和相邻块交换。)能到达目标转态。其实这就是一个八数码的经典问题。
思路
八数码问题以及各种实现方法和讲解可以见:http://blog.csdn.net/huangxy10/article/details/8034285
对于这题我把大部分思路都写在了代码注释里,因为A*算法在一定程度上比较难以理解,所以我觉得结合代码大家应该更看的懂,我自己也是看了3天的A*算法后才勉强可以做这题的。在这里我先简要讲一讲这题:
首先这题必须要用hash和康托展开来压缩空间,康托展开的原理我用一个例子来讲解:
对于这样一个排列:14032,他在排列中排第几呢?公式是:
1*4!+3*3!+0*2!+1*1!+0*0!
求法是这样的,首先看第一个数字1,在数字1前面(不是这个排列的前面,而是这个排列所出现的数字中,如14032中只有一个0比它小)小于它的数只有一个0,对于它后面的数可以有4!种(4的全排列),所以对于1就是1*4!,同理对于4,0~4比它小的有0,1,2,3,但是1在前面已经用过了,所以只有3个,对于后面就是3!,所以对于4就是3*3!,同理往后推。
但是为什么要用hash呢,其实就是为了判重,因为如果你用普通的方法去判重,那么对于3*3方块,每一个数字都有9种可能0~8,况且你用普通bfs和dfs的vis数组根本无法判这题状态是否重复。但是hash可以很好的解决这个问题,因为hash可以把3*3这些数字全部排列的个数控制在0~9!上,对于每一个转态就有一个唯一的hash值,此时只要开一个一维的vis[hash最大值]数组就可以判重。
其次这题所用的A*算法见:http://blog.csdn.net/huangxy10/article/details/8034285
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>using namespace std;
const int maxn=4e5+10;int ha[9]={1,1,2,6,24,120,720,5040,40320};
/*这个ha[9]数组是用来计算hash值的,这个数组中的值分别是0!,1!,2!,3!,4!,5!,6!,7!,8!*/
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
char d[10]="udlr";
int vis[maxn];//判重 struct proc
{int f[3][3];//当前这个转态每个位置上的值 int x,y;int g,h;int hash_num;bool operator < (const proc a)const{return h+g>a.h+a.g;//优先队列保证 出队的是估值函数值较小的。 }
};struct path
{int pre;char ch;
}p[maxn];//用于记录路径 //评估函数,获得评估值
//计算1~8的数字回到原点需要的步数作为评估值,必定小于实际操作数
int get_h(proc a)
{int i,j,ans=0;for(int i=0;i<3;i++){for(int j=0;j<3;j++){if(a.f[i][j]){ans+=abs(i-(a.f[i][j]-1)/3)+abs(j-(a.f[i][j]-1)%3);//曼哈顿距离}}}return ans;
}//康托展开
int get_hash(proc e)
{int a[9],i,j,k=0,ans=0; for(i=0;i<3;i++) //将数据排成一排,便于计算 { for(j=0;j<3;j++) a[k++]=e.f[i][j]; } for(i=0;i<9;i++)//计算hash值 { k=0; for(j=0;j<i;j++) if(a[j]>a[i])k++; ans+=ha[i]*k; } return ans;
}void print(int x)
{if(p[x].pre==-1) return;print(p[x].pre);printf("%c",p[x].ch);
}void A_star(proc a)
{memset(vis,0,sizeof(vis));int end_ans,xx,yy,k;proc vw,vn;for(int i=0;i<9;i++){vw.f[i/3][i%3]=(i+1)%9;//目标转态时各个位置的值}end_ans=get_hash(vw);//目标转态时的hash值a.hash_num=get_hash(a);a.g=0;a.h=get_h(a);vis[a.hash_num]=1;p[a.hash_num].pre=-1;if(a.hash_num==end_ans){printf("\n");return;}priority_queue<proc> q;q.push(a);while(!q.empty()){a=q.top();q.pop();for(int i=0;i<4;i++){xx=a.x+dir[i][0];yy=a.y+dir[i][1];if(xx<0||yy<0||xx>=3||yy>=3) continue;vw=a;swap(vw.f[a.x][a.y],vw.f[xx][yy]);//交换双方的值k=get_hash(vw);if(vis[k]) continue;vis[k]=1;vw.hash_num=k;vw.x=xx;vw.y=yy;vw.g++;vw.h=get_h(vw);p[k].pre=a.hash_num;p[k].ch=d[i];if(k==end_ans)//如果当前状态的hash值等于目标状态的hash值,表示已经到达目标状态{print(k);printf("\n");return;}q.push(vw);}}
}int main()
{char a[30]; while(gets(a)) { int i,j,k,n; proc e; n=strlen(a); for(i=0,j=0;i<n;i++) { if(a[i]==' ')continue; if(a[i]=='x'){e.f[j/3][j%3]=0;e.x=j/3;e.y=j%3;} else e.f[j/3][j%3]=a[i]-'0'; j++; } for(i=0,k=0;i<9;i++) //这边涉及到一个逆序数问题,如果开始状态的逆序数为奇数,那么这个状态只能到达逆序数为奇数的转态,同理偶数逆序数的状态也只能到达逆序数为偶数的状态{ if(e.f[i/3][i%3]==0)continue; for(j=0;j<i;j++) { if(e.f[j/3][j%3]==0)continue; if(e.f[j/3][j%3]>e.f[i/3][i%3])k++; } } if(k&1)printf("unsolvable\n"); else A_star(e); } return 0;
}
【HDU 1043】Eight(A*启发式搜索算法)相关推荐
- 启发式搜索算法(A*算法)
A算发:在bfs算法中,若对每个状态n都设定估价函数f(n)=g(n)+h(n),并且每次从开启列表中选节点 进行扩展时,都选取f值最小的节点,则该搜索算法为启发式搜索算法,又称A算法. g(n):从 ...
- 你必须会的启发式搜索算法--A*算法
一.算法原理 A* 算法,就是解决在一个平面 grid地图中寻找起点到终点的最短路径问题的算法,类似于Dijkstra算法和BFS算法一样,属于广度优先搜索.实际上它还是一个启发式搜索算法,什么叫启发 ...
- HDU 1043 Eight(八数码)
HDU 1043 Eight(八数码) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- 经典算法研究系列:八、再谈启发式搜索算法
经典算法研究系列:八.再谈启发式搜索算法 作者:July 二零一一年二月十日 本文参考: I. 维基百科. II. 人工智能-09 启发式搜索. III.本BLOG内,经典算法研究系列:一.A ...
- 【人工智能】1.问题求解:启发式搜索算法
A* 算法是启发式搜索算法中的经典,经常应用于图搜索.路径搜索和规划中.这里以八数码问题状态空间图的搜索为例,初步介绍以A*算法为代表的启发式搜索. 搜索算法可分为两大类:无信息的搜索算法和有信息的搜 ...
- hdu 1043 Eight 经典八数码问题
hdu 1043 Eight 经典八数码问题 题意描述:给出一个3×3的矩阵(包含1-8数字和一个字母x),经过一些移动格子上的数后得到连续的1-8,最后一格是x,要求最小移动步数. 算法分析:经典的 ...
- Eight HDU - 1043
Eight HDU - 1043 题意:给定一个3*3的方阵,要求通过交换x方格与其相邻方格位置的方式,使方阵上的数字由小到大排列,且x在右下角.输出具体交换步骤
- 转载:改进的双向启发式搜索算法及其在车载导航仪中的应用
非常感谢作者. 摘要:介绍单车辆路径规划的有关算法,针对车载导航仪的应用,对双向启动式搜索算法进行了改进和优化,提出了可靠有效的搜索终止条件和搜索切换标准,给出了改进算法的流程.最后给出了四种算法的实 ...
- 启发式搜索算法解决数独问题sudoku,附python实现
定义 数独是源自18世纪瑞士的一种数学游戏.是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个粗线宫(3*3)内的数字均含1 ...
- A*启发式搜索算法详解 人工智能
A*启发式搜索算法详解 人工智能 我们尝试解决的问题是把一个游戏对象(game object)从出发点移动到目的地.路径搜索(Pathfinding)的目标是找到一条好的路径--避免障碍物.敌人,并把 ...
最新文章
- pythonclass全局变量_Python-多处理全局变量更新未返回给父级
- BugkuCTF-reverse:入门逆向
- 听说”双11”是这么解决线上bug的
- 具名元祖--namedtuple
- 作者:高丰(1986-),英国南安普敦大学计算机博士,现为开放数据与创新独立咨询顾问,兼复旦大学数字与移动治理实验室特邀研究员。...
- Python之路---函数进阶
- 茂名2021高考成绩查询入口,茂名高考成绩查询入口
- 【Shell教程】三----运算符,条件判断
- [Swift]LeetCode288. 唯一单词缩写 $ Unique Word Abbreviation
- 类型约束的本质:泛型是不完备类型,只有合乎要求的构造才能正确使用和访问。...
- StringBulider StringBuffer
- NYOJ题目1045看美女
- 人工雨量计_自动站与人工站遥测雨量计降水量对比分析
- 详解手机注册验证码操作思路与流程
- Python 结巴分词(jieba)Tokenize和ChineseAnalyzer的使用及示例代码
- 巧用二重积分的积分中值定理
- 微信屏蔽网址解决办法 微信网址被屏蔽了红了照样打开
- 笔记本电脑怎样重装系统
- 如何放大图片不模糊?教你一招
- SSH框架相关准备与入门学习
热门文章
- 关于对《三只松鼠》网站的诊断报告
- 微信小程序判断iphonex xs xr 样式
- oracle有dba角色用户,ORACLE管理-查看拥有DBA角色的用户
- “1万起投,年化达8%”?天安金交中心卖力“吆喝”的产品,是“香”还是“坑”?
- 如何用MCU来控制21489调音?
- Android 流量分析API
- 浙江大学计算机研究生2020年录取分数线,2020年浙江大学考研分数线公布
- DaoCloud Rest API 体验
- Python 电影评分分析
- 案例分析-电影评分分析