这个题扔到A*可也还行。。。


定义估价函数h():为每个数或空格的位置 到 最终状态中所在位置 的 曼哈顿距离 的 总和。

把状态压成一个九进制数,便于存储和判重。

然后记录方案可以记录一下此次的操作和上一次的状态,具体见代码。

安利一篇博文:http://www.cnblogs.com/goodness/archive/2010/05/04/1727141.html 。。。被震惊。。。我只会map,懒得哈希QWQ

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<algorithm>
#define R register int
const int dx[]={-,,,},dy[]={,,,-},end=;
const int py[]={,,,,,,,},px[]={,,,,,,,};
const char op[]={'u','r','d','l'};
using namespace std;
inline int g() {
R ret=; register char ch; while(!isdigit(ch=getchar()));
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret;
}
struct node{ int sta,g,h; node() {}
node(int ss,int gg,int hh) {sta=ss,g=gg,h=hh;}
bool operator <(const node& y) const {return g+h>y.g+y.h;}
}; int a[][];
map<int,int> d,pre,dir;
priority_queue<node> q;
inline bool ckpos(int x,int y) {return x<||x>||y<||y>;}
inline int calc(int p[][]) { R ret=;
for(R i=;i<;++i) for(R j=;j<;++j) ret=ret*+p[i][j]; return ret;
}
inline pair<int,int> recalc(int vl,int p[][]) { R x,y;
for(R i=;i>=;--i) for(R j=;j>=;--j) {
p[i][j]=vl%,vl/=; if(p[i][j]==) x=i,y=j;
} return make_pair(x,y);
}
inline int h(int p[][]) { R ret=;
for(R i=;i<;++i) for(R j=;j<;++j) {
if(p[i][j]==) continue;
ret+=abs(i-px[p[i][j]-])+abs(j-py[p[i][j]-]);
} return ret;
}
inline int Astar() {
d.clear(),pre.clear(),dir.clear();
while(q.size()) q.pop(); R st=calc(a); d[st]=;
q.push(node(st,,h(a)));
while(q.size()) {
node u=q.top(); R crt=q.top().sta; q.pop();
if(crt==end) return u.g;
R a[][]; register pair<int,int> blk=recalc(crt,a); R x=blk.first,y=blk.second;
for(R i=;i<;++i) { R xx=x+dx[i],yy=y+dy[i];
if(ckpos(xx,yy)) continue; swap(a[x][y],a[xx][yy]);
R nxt=calc(a); if(d.find(nxt)==d.end()||d[nxt]>d[crt]+)
d[nxt]=d[crt]+,pre[nxt]=crt,dir[nxt]=i,q.push(node(nxt,d[nxt],h(a)));//此处存了nxt的上一个状态和如何操作
swap(a[xx][yy],a[x][y]);
}
} return -;
}
inline void print(int s) {
if(pre.find(s)==pre.end()) return;
print(pre[s]); putchar(op[dir[s]]);
}
signed main() {
for(R i=;i<;++i) for(R j=;j<;++j) { register char ch;
while(!isdigit(ch=getchar())&&ch!='x');
if(ch=='x') a[i][j]=;
else a[i][j]=ch^;
} R ans=Astar(); if(ans==-) printf("unsolvable"),putchar('\n'); else print(end);
}

2019.04.27

POJ1077 Eight A*的更多相关文章

  1. ACM/ICPC 之 BFS-广搜进阶-八数码(经典)(POJ1077+HDU1043)

    八数码问题也称为九宫问题.(本想查查历史,结果发现居然没有词条= =,所谓的历史也就不了了之了) 在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的某一数字,不同棋子上标的数字不相同.棋盘上还有一个 ...

  2. POJ-1077 HDU 1043 HDU 3567 Eight (BFS预处理+康拓展开)

    思路: 这三个题是一个比一个令人纠结呀. POJ-1077 爆搜可以过,94ms,注意不能用map就是了. #include<iostream> #include<stack> ...

  3. POJ1077 Eight —— 经典的搜索问题

    题目链接:http://poj.org/problem?id=1077 Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submission ...

  4. poj1077 Eight【爆搜+Hash(脸题-_-b)】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298840.html   ---by 墨染之樱花 题目链接:http://poj.org/pr ...

  5. POJ1077&&HDU1043(八数码,IDA*+曼哈顿距离)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30127   Accepted: 13108   Special ...

  6. poj1077(康托展开+bfs+记忆路径)

    题意:就是说,给出一个三行三列的数组,其中元素为1--8和x,例如: 1 2 3 现在,需要你把它变成:1 2 3 要的最少步数的移动方案.可以右移r,左移l,上移u,下移dx 4 6 4 5 67 ...

  7. poj1077

    题意:给出一个八数码问题,求解法,不可解则输出unsolvable. 分析:可以用ida*算法,估价函数可以使用每个数码到其最终位置的最短距离之和.对于不可解的判断,我这里用迭代深度大于100时判定为 ...

  8. POJ1077 八数码 BFS

    BFS 几天的超时... A*算法不会,哪天再看去了. /* 倒搜超时, 改成顺序搜超时 然后把记录路径改成只记录当前点的操作,把上次的位置记录下AC..不完整的人生啊 */ #include < ...

  9. POJ1077 Eight —— IDA*算法

    主页面:http://www.cnblogs.com/DOLFAMINGO/p/7538588.html 代码一:像BFS那样,把棋盘数组放在结构体中. #include <iostream&g ...

随机推荐

  1. C++——STL容器

    序列式容器:vector,list,deque:stack,queue(容器适配器),heap,priority_queue,slist 关联式容器:(底层都是红黑树)set,map,multiset ...

  2. Django框架 之 跨域请求伪造

    Django框架 之 跨域请求伪造 浏览目录 同源策略与Jsonp 同源策略 Jsonp jQuery对JSONP的实现 CORS 简介 两种请求 同源策略与Jsonp 同源策略 同源策略(Same ...

  3. python,itertools模块

    itertools模块的使用: # coding=utf-8 """ itertools模块 """ import itertools im ...

  4. Sharepoint 文档知识管理系统--Word在试图打开文件时遇到错误

    在系统开发中,遇到问题:SharePoint 2010与Office 2010安装在一台服务器上,当用Office打开SharePoint文档库中的文档时,遇到“Word在试图打开文件时遇到错误,请尝 ...

  5. poj3080 Blue Jeans(暴枚+kmp)

    Description The Genographic Project is a research partnership between IBM and The National Geographi ...

  6. Castle Windsor 注册组件

    1.逐个注册组件即对每个接口通过代码指定其实现类,代码: container.Register( Component.For<IMyService>() //接口 .Implemented ...

  7. IO流-File,字节流,缓冲流

    1.1 IO概述 回想之前写过的程序,数据都是在内存中,一旦程序运行结束,这些数据都没有了,等下次再想使用这些数据,可是已经没有了.那怎么办呢?能不能把运算完的数据都保存下来,下次程序启动的时候,再把 ...

  8. 757. Set Intersection Size At Least Two

    An integer interval [a, b] (for integers a < b) is a set of all consecutive integers from a to b, ...

  9. selenium面试题

    selenium中如何判断元素是否存在? selenium中没有提供原生的方法判断元素是否存在,一般我们可以通过定位元素+异常捕获的方式判断. # 判断元素是否存在 try: dr.find_elem ...

  10. 洛谷P4557 [JSOI2018]战争(闵可夫斯基和+凸包)

    题面 传送门 题解 看出这是个闵可夫斯基和了然而我当初因为见到这词汇是在\(shadowice\)巨巨的\(Ynoi\)题解里所以压根没敢学-- 首先您需要知道这个 首先如果有一个向量\(w\)使得\ ...