在学习八数码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*搜索)的更多相关文章

  1. Eight POJ - 1077 HDU - 1043 八数码

    Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...

  2. HDU 1043 八数码(八境界)

    看了这篇博客的讲解,挺不错的.http://www.cnblogs.com/goodness/archive/2010/05/04/1727141.html 判断无解的情况(写完七种境界才发现有直接判 ...

  3. HDU 1043 八数码问题的多种解法

    一.思路很简单,搜索.对于每一种状态,利用康托展开编码成一个整数.于是,状态就可以记忆了. 二.在搜索之前,可以先做个优化,对于逆序数为奇数的序列,一定无解. 三.搜索方法有很多. 1.最普通的:深搜 ...

  4. HDU 1043 八数码 Eight A*算法

    Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. Eight hdu 1043 八数码问题 双搜

    Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  6. hdu 1043 八数码问题

    Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  7. hdu3567 八数码(搜索)--预处理

    题意:为你两个状态,求a到b 的最小路径,要求字典序最小. 思路: 最开始想的是目标状态是变化的,所以打表应该不行,然后直接上A*,但是TLE了- -(瞬间无语) 然后看了下别人的思路,预处理出9个状 ...

  8. (中等) HDU 1043 Eight,经典搜索问题。

    Problem Description The 15-puzzle has been around for over 100 years; even if you don't know it by t ...

  9. 【P1379】八数码难题(搜索+暴力)

    这个题真是... 不想说什么了,及其复杂和烦人的一道题.基础思路就是bfs,用两个队列分别进行0的位置的计算和每一步的状态..然而这个题最重要的一点在于判重,实际上可以康托展开用全排列的个数进行判重, ...

随机推荐

  1. java源码学习

    Collection List ArrayList LinkedList Vector Stack Set HashSet TreeSet Map HashMap TreeMap LinkedHash ...

  2. 开源映射平台Mapzen加入了Linux基金会的项目

    2019年1月29日,Linux基金会宣布,开源映射平台Mapzen现在是Linux基金会项目的一部分. Mapzen专注于地图显示的核心组件,如搜索和导航.它为开发人员提供了易于访问的开放软件和数据 ...

  3. Python爬虫基础示例

    使用pip安装相关依赖: pip install requests pip install bs4 安装成功提示:Successfully installed *... 爬取中国天气网数据示例代码: ...

  4. 05002_Linux的安装

    1.虚拟机VMware workstation的安装 (1)下载链接:VMware workstation下载 密码:52wt: (2)双击VMware-workstation-full-10.0.2 ...

  5. 二 MapReduce 各阶段流程分析

    如果想要将问题变得清晰.精准和优雅, 需要关注 MapReduce 作业所需要的系统资源,尤其是集群内部网络资源使用情况. MR 可以运行在共享集群上处理 TB 级 甚至 PB 级的数据.同时,改作业 ...

  6. 怎样用第三方开源免费软件portecle从https站点上导出SSL的CA证书?

    在我这篇文章中.我提到了怎样用OpenSSL从https站点上导出SSL的CA证书?  这样的方式不太直观,且须要用户自己手工拷贝.然后另存为文件,那么有没有更好更方便的工具呢? 幸运的是,有热心于开 ...

  7. wxWidgets笔记_1_linux环境下wxwidgets的安装与配置

    linux下wxwidgets环境的安装与配置 一.建立目标文件夹(自己定义) mkdir /opt/SCOTT 二.安装wxWidgets 1.wxWidgets版本号能够选用wxGTK-2.8.1 ...

  8. Codefroces B. T-primes

    http://codeforces.com/problemset/problem/230/B B. T-primes time limit per test 2 seconds memory limi ...

  9. 概率编程:《贝叶斯方法概率编程与贝叶斯推断》中文PDF+英文PDF+代码

    贝叶斯推理的方法非常自然和极其强大.然而,大多数图书讨论贝叶斯推理,依赖于非常复杂的数学分析和人工的例子,使没有强大数学背景的人无法接触.<贝叶斯方法概率编程与贝叶斯推断>从编程.计算的角 ...

  10. C# Arcgis Engine 捕捉功能实现

    namespace 捕捉 { public partial class Form1 : Form { private bool bCreateElement=true; ; ; private IEl ...