POJ 1077 && HDU 1043 Eight A*算法,bfs,康托展开,hash 难度:3
http://poj.org/problem?id=1077
http://acm.hdu.edu.cn/showproblem.php?pid=1043
X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中,a[i]为整数,并且X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0!。这就是康托展开。康拓展开可以用来表示排列状态,对于本题的9个数字的所有排列只需要9位,所有状态总共362880个.对每个状态,我们都能得到一个不重复的状态编号,用这个状态编号可以查重
Astar算法,就是一个给所有状态一个评估函数,优先选取较优状态向下搜的最好优先直接搜索算法,对于本题,我们设F(n)=g(n)+h(n)为估价函数,设g(n)为到达该状态已经走过的步数,h(n)为到最终答案的曼哈顿距离
因为该拼图不能改变除了x以外的数字的逆序数奇偶性,所以若x以外的数字逆序数为奇数,则无法得到答案,直接输出
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cctype>
using namespace std;
const int base[9]={1,1,2,6,24,120,720,5040,40320};
const int dx[4]={1,-1,0,0};
const int dy[4]={0,0,1,-1};
const int maxh=362880;
const int des=0;
struct pnt{
int maz[3][3];
int h,g;
int x,y;
int hashCode;
bool operator < (pnt p)const {
return h+g!=p.h+p.g?h+g>p.h+p.g:g>p.g;
}
int gethashcode(){
int ans=0;
for(int i=0;i<9;i++){
int cnt=0;
for(int j=0;j<i;j++){
if(maz[j/3][j%3]>maz[i/3][i%3]){
cnt++;
}
}
ans+=base[i]*cnt;
}
return hashCode=ans;
}
int geth(){
int ans=0;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
int t=maz[i][j]-1;
if(t<9) ans+=abs(i-t/3)+abs(j-t%3);
}
}
return ans;
}
bool judge(){
int cnt=0;
for(int i=0;i<9;i++){
for(int j=0;j<i;j++){
if(maz[i/3][i%3]<9&&maz[j/3][j%3]<9&&maz[i/3][i%3]<maz[j/3][j%3])cnt++;
}
}
return (cnt&1)==0;
}
};
bool in(int tx,int ty){
return tx>=0&&tx<3&&ty>=0&&ty<3;
} int vis[maxh+1];
int pre[maxh+1]; void astar(pnt s){
priority_queue <pnt>que;
que.push(s);
while(!que.empty()){
pnt f=que.top();que.pop();
for(int i=0;i<4;i++){
pnt t=f;
t.x+=dx[i];
t.y+=dy[i];
if(in(t.x,t.y)){
swap(t.maz[t.x][t.y],t.maz[f.x][f.y]);
t.hashCode=t.gethashcode();
if(vis[t.hashCode]==-1){
vis[t.hashCode]=i;
t.g++;
pre[t.hashCode]=f.hashCode;
t.h=t.geth();
que.push(t);
}
if(t.hashCode==des)return ;
}
}
}
}
char ans[maxh+1];
void print(){
int nxt=des;
int len=0;
while(pre[nxt]!=-1){
switch(vis[nxt]){
case 0:
ans[len++]='d';
break;
case 1:
ans[len++]='u';
break;
case 2:
ans[len++]='r';
break;
case 3:
ans[len++]='l';
break;
}
nxt=pre[nxt];
}
for(int i=len-1;i>=0;i--){
putchar(ans[i]);
}
puts("");
}
char buff[300];
pnt s;
bool input(){
if(gets(buff)==NULL)return false;
int j=0;
for(int i=0;i<9;i++){
while(!isalnum(buff[j])){j++;}
if(buff[j]>='0'&&buff[j]<='9'){
s.maz[i/3][i%3]=buff[j]-'0';
}
else{
s.maz[i/3][i%3]=9;
s.x=i/3;
s.y=i%3;
}
j++;
}
return true;
}
int main(){
while(input()){
memset(vis,-1,sizeof(vis));
memset(pre,-1,sizeof(pre));
if(!s.judge()){
puts("unsolvable");
continue;
}
s.hashCode=s.gethashcode();
if(s.hashCode==des){
puts("");
continue;
}
vis[s.hashCode]=-2;
s.g=0;s.h=s.geth();
astar(s);
print();
}
return 0;
}
POJ 1077 && HDU 1043 Eight A*算法,bfs,康托展开,hash 难度:3的更多相关文章
- HDU 1043 Eight(双向BFS+康托展开)
http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...
- Eight POJ - 1077 HDU - 1043 八数码
Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...
- HDU - 1430 魔板 【BFS + 康托展开 + 哈希】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路 我刚开始 想到的 就是 康托展开 但是这个题目是 多组输入 即使用 康托展开 也是会T的 ...
- POJ 1077 HDU 1043 Eight (IDA*)
题意就不用再说明了吧......如此经典 之前想用双向广搜.a*来写,但总觉得无力,现在用IDA*感觉其他的解法都弱爆了..............想法活跃,时间,空间消耗很小,给它跪了 启发式搜索关 ...
- HDU_1043 Eight 【逆向BFS + 康托展开 】【A* + 康托展开 】
一.题目 http://acm.hdu.edu.cn/showproblem.php?pid=1043 二.两种方法 该题很明显,是一个八数码的问题,就是9宫格,里面有一个空格,外加1~8的数字,任意 ...
- 洛谷 P2578 [ZJOI2005]九数码游戏【bfs+康托展开】
只有9!=362880个状态,用康托展开hash一下直接bfs即可 #include<iostream> #include<cstdio> #include<cstrin ...
- [算法总结]康托展开Cantor Expansion
目录 一.关于康托展开 1.什么是康托展开 2.康托展开实现原理 二.具体实施 1.模板 一.关于康托展开 1.什么是康托展开 求出给定一个由1n个整数组成的任意排列在1n的全排列中的位置. 解决这样 ...
- BFS(八数码) POJ 1077 || HDOJ 1043 Eight
题目传送门1 2 题意:从无序到有序移动的方案,即最后成1 2 3 4 5 6 7 8 0 分析:八数码经典问题.POJ是一次,HDOJ是多次.因为康托展开还不会,也写不了什么,HDOJ需要从最后的状 ...
- HDU 1043 Eight (A*算法)
题目大意:裸的八数码问题,让你输出空格的一条合法移动路径 首先利用康托展开对排列编号,可以预处理出排列,就不必逆展开了 然后利用A*算法求解 A*算法是一种启发式搜索,具体实现要用到优先队列/堆,不同 ...
随机推荐
- ERROR 1201 (HY000) Could not initialize master info structure
1.错误原因:Slave线程启动出现问题.2.解决办法一:stop slave; reset slave; start slave; show slave status\G3.如果解决方法一,还是失败 ...
- Ubuntu Server14.04 32位安装odoo8.0简单方法
一.wget -O - https://nightly.odoo.com/odoo.key | apt-key add - 二.echo "deb http://nightly.odoo.c ...
- strcpy, memcpy, memset函数
一. strcpy函数 原型声明:char *strcpy(char* dest, const char *src); 头文件:#include <string.h> 和 #inclu ...
- ps前端切图常用快捷键
一.新建文件:预设-自定:宽度1920,单位像素,高度自定义:分辨率:72像素/英寸:颜色模式:RGB,8位:背景色:透明:可存储预设,下次新建的时候,直接在预设中,选择那个名称,点确定.视图:显示- ...
- golang json
1.Go语言的JSON 库 Go语言自带的JSON转换库为 encoding/json 1.1)其中把对象转换为JSON的方法(函数)为 json.Marshal(),其函数原型如下 func Mar ...
- [js] 跨域
原文链接:http://www.cnblogs.com/scottckt/archive/2011/11/12/2246531.html 什么是跨域? 首先什么是跨域,简单地理解就是因为JavaScr ...
- 转:浅谈C/C++中的指针和数组(一)
再次读的时候实践了一下代码,结果和原文不一致 error C2372: 'p' : redefinition; different types of indirection 不同类型的间接寻址 /// ...
- Hbase之测试数据
info ship user name age height phone addr email dept salary create 'user','info','ship'; put 'user', ...
- 【图像处理Matlab】2 灰度变换 imadjust stretchlim
f=imread('123.tif'); % 读入一幅图片 g=imadjust(f,[ ],[ ]); % 负片显示 [imadjust] imadjust(f, [low_in high_in], ...
- commonJS — 对象操作(for Object)
for Object github: https://github.com/laixiangran/commonJS/blob/master/src/forObject.js 代码 /** * Cre ...