HDU 1043 八数码(A*搜索)
在学习八数码A*搜索问题的时候须要知道下面几个点:
Hash:利用康托展开进行hash
康托展开主要就是依据一个序列求这个序列是第几大的序列。
A*搜索:这里的启示函数就用两点之间的曼哈顿距离进行计算就能够。
减枝:在八数码里。随意交换一个空行和一个位置的数字,这个八数码的逆序数是不变的,这样就能够依据眼下状态推断是否可达终点状态了。
第一次做这个题用的map进行哈希,结果果断超时。之后又写了LRJ书上的hash方法也超时了,最后仅仅能用康托展开了
具体请參考:【八数码的八重境地】 http://www.cnblogs.com/goodness/archive/2010/05/04/1727141.html
/*
康托展开
A* 算法
八数码逆序数性质
*/
#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 370015;
//322560
struct State{
int mat[3][3];
int h,g,cvalue;
int posx,posy;
friend bool operator < (State p,State q){
if(p.h != q.h)
return p.h > q.h;
else
return p.g > q.g;
}
}start;
int vis[maxn],fa[maxn],cnt;
//-------------------init----------------------------------
void init(){
memset(vis,-1,sizeof(vis));
memset(fa,-1,sizeof(fa));
cnt = 0;
}
bool isok(State &state){
int temp[9];
for(int i = 0,k = 0; i < 3; i ++)
for(int j = 0; j < 3; j++,k++)
temp[k] = state.mat[i][j];
int ret = 0;
for(int i = 0; i < 9; i++)
for(int j = 0; j < i; j++){
if(temp[i] && temp[j] && temp[j] > temp[i])
ret ++;
}
return (ret & 1) ? 0 : 1;
}
//---------------------------------------------------------
const int Hash[] = {1,1,2,6,24,120,720,5040,40320};
int Cantor(State &stemp){
int temp[9];
for(int i = 0,k = 0; i < 3; i++)
for(int j = 0; j < 3; j++, k++)
temp[k] = stemp.mat[i][j];
int ret = 0;
for(int i = 0; i < 9; i++){
int val = 0;
for(int j = 0; j < i; j++)
if(temp[j] > temp[i]) val ++;
ret += Hash[i] * val;
}
return ret;
}
//----------------------------------------------------------
int get_h(State &temp){
int ret = 0;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++){
ret +=
abs(i - (temp.mat[i][j] - 1)/3) + abs(j - (temp.mat[i][j] - 1) % 3);
}
return ret;
}
//----------------------------------------------------------
//ulldrdrulldrruldlurrd
const char cdir[] = "dlru";
const int dir[4][2] = {{1,0},{0,-1},{0,1},{-1,0}}; //d l r u
void dfs_print(int u){
if(vis[u] < 0)
return;
dfs_print(fa[u]);
printf("%c",cdir[vis[u]]);
}
bool bfs(){
priority_queue<State>q;
start.cvalue = Cantor(start);
start.h = get_h(start);
start.g = 0;
q.push(start);
vis[start.cvalue] = - 2;
State temp;
while(!q.empty()){
State now = q.top(); q.pop();
if(now.cvalue == 322560){
dfs_print(now.cvalue);
puts("");
return true;
}
for(int i = 0; i < 4; i++){
temp = now;
int x = now.posx + dir[i][0];
int y = now.posy + dir[i][1];
temp.posx = x;
temp.posy = y;
if(x >= 0 && x < 3 && y >= 0 && y < 3){
swap(temp.mat[x][y],temp.mat[now.posx][now.posy]);
int cvalue = Cantor(temp);
if(vis[cvalue] == -1 && isok(temp)){
vis[cvalue] = i;
fa[cvalue] = now.cvalue;
temp.h = get_h(temp);
temp.g = now.g + 1;
temp.cvalue = cvalue;
q.push(temp);
if(temp.cvalue == 322560){
dfs_print(cvalue);
puts("");
return true;
}
cnt ++;
}
}
}
}
return false;
}
int main(){
char _in[10][2];
while(scanf("%s",_in[0]) != EOF){
init();
for(int i = 1; i < 9; i++)
scanf("%s",_in[i]);
for(int k = 0,i = 0; i < 3; i++)
for(int j = 0; j < 3; j++,k ++){
if(_in[k][0] == 'x'){
_in[k][0] = '0';
start.posx = i;
start.posy = j;
}
start.mat[i][j] = _in[k][0] - '0';
}
if(!bfs())
printf("unsolvable\n");
}
return 0;
}
/*
1
2
6
24
120
720
5040
40320
*/
HDU 1043 八数码(A*搜索)的更多相关文章
- Eight POJ - 1077 HDU - 1043 八数码
Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...
- HDU 1043 八数码(八境界)
看了这篇博客的讲解,挺不错的.http://www.cnblogs.com/goodness/archive/2010/05/04/1727141.html 判断无解的情况(写完七种境界才发现有直接判 ...
- HDU 1043 八数码问题的多种解法
一.思路很简单,搜索.对于每一种状态,利用康托展开编码成一个整数.于是,状态就可以记忆了. 二.在搜索之前,可以先做个优化,对于逆序数为奇数的序列,一定无解. 三.搜索方法有很多. 1.最普通的:深搜 ...
- HDU 1043 八数码 Eight A*算法
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- Eight hdu 1043 八数码问题 双搜
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- hdu 1043 八数码问题
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- hdu3567 八数码(搜索)--预处理
题意:为你两个状态,求a到b 的最小路径,要求字典序最小. 思路: 最开始想的是目标状态是变化的,所以打表应该不行,然后直接上A*,但是TLE了- -(瞬间无语) 然后看了下别人的思路,预处理出9个状 ...
- (中等) HDU 1043 Eight,经典搜索问题。
Problem Description The 15-puzzle has been around for over 100 years; even if you don't know it by t ...
- 【P1379】八数码难题(搜索+暴力)
这个题真是... 不想说什么了,及其复杂和烦人的一道题.基础思路就是bfs,用两个队列分别进行0的位置的计算和每一步的状态..然而这个题最重要的一点在于判重,实际上可以康托展开用全排列的个数进行判重, ...
随机推荐
- Python安装selenium启动浏览器
1:在Python运行火狐或谷歌的浏览器是需要下载相对应的驱动 例如:你想在Python中使用代码命令打开firefox的网页 如果没有安装驱动,直接运行的话会出下面的错误 所以我们要安装相对应的浏览 ...
- 深入理解void以及void指针的含义
void的含义 void即“无类型”,void *则为“无类型指针”,可以指向任何数据类型. void指针使用规范 ①void指针可以指向任意类型的数据,亦即可用任意数据类型的指针对void指针赋值. ...
- 【LeetCode-面试算法经典-Java实现】【057-Insert Interval(插入区间)】
[057-Insert Interval(插入区间)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a set of non-overlapping in ...
- iOS多线程与网络开发之多线程GCD
郝萌主倾心贡献,尊重作者的劳动成果.请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠.支持郝萌主.捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 游戏官方下 ...
- cocos2d-x 3.1.1学习笔记[23]寻找主循环 mainloop
文章出自于 http://blog.csdn.net/zhouyunxuan cocos2d到底是怎样把场景展示给我们的,我一直非常好奇. 凭个人猜想,引擎内部的结构类似于这样 while(true ...
- Anatomy of an IIS7 configuration path
The concept of configuration paths is fundamental to managing and operating an IIS server, so I want ...
- 根据on,获取选中的元素
打钩的元素有个on的class. // 礼物发送 function send_gift() { var type,diamond,name,id; var list = $('.live .on'); ...
- ECharts简单入门
图1和图2是手机上显示的效果, 图3是电脑浏览器显示的效果. 如何使用ECharts? 1.下载echarts.js 2.引入echarts.js <script type="text ...
- 请求由tomcat转到servlet的临界点
>>>>>>>>>>>>>>>>>>>>>>>>> ...
- Linux 下实现虚拟光驱功能,查看iso文件内容
1,创建挂载点(也可以不创建,直接用现有的目录) openSUSE:~ # mkdir /mnt/iso 2,挂载ISO文件至创建的挂载点 openSUSE:~ # mount -t iso9660 ...