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的位置的计算和每一步的状态..然而这个题最重要的一点在于判重,实际上可以康托展开用全排列的个数进行判重, ...
随机推荐
- 转载 :Linux有问必答:如何在Debian或Ubuntu上安装完整的内核源码
http://linux.cn/article-5015-1.html 问题:我需要为我的Debian或Ubuntu下载并安装完整树结构的内核源码以供编译一个定制的内核.那么在Debian或Ubunt ...
- docker操作大全
docker 常用操作方法 查看docker版本docker version 搜索镜像docker serach 镜像名称 拉去镜像docker pull 镜像名称 查看本地镜像仓库信息docker ...
- 今日SGU 5.26
#include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl ...
- HNU 12876 Quite Good Numbers 完美数变形
筛法是一种很快的方法,贴代码纪念一下. 做法很像筛法 #include <iostream> #include <cstdio> #include <cstring> ...
- windows server 打开 FTP 服务器上的文件夹时发生错误。请检查是否有权限访问该文件夹。
解决方案1: 打开高级安全windows防火墙,设置出入站规则. 然后,再打开windows防火墙界面,点击左上角“允许程序或功能通过windows防火墙”,勾选上设置的出入站名称和FTP服务器. 如 ...
- 洛谷——P1019 单词接龙(NOIP2000 T3)
https://www.luogu.org/problem/show?pid=1019#sub 题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母, ...
- iOS开发系列之四 - UITextView 使用方法小结
// 初始化输入框并设置位置和大小 UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 300, 1 ...
- javascript 提交弹窗
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Flask Flash闪现
Flash介绍以及工作方式 flash中添加消息 取出flash中的消息 Flash介绍以及工作方式 - 介绍: flash :闪现 一个好的应用和用户界面都需要良好的反馈. 如果用户得不到足够的反馈 ...
- python学习二,字符串常用操作
字符串可以说是在日常开发中应用最广泛的了,现在来总结下有关python中有关字符串一些常用操作 首先我们声明一个字符串变量 str = "hello world" 下面我们来依次介 ...