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*算法是一种启发式搜索,具体实现要用到优先队列/堆,不同 ...
随机推荐
- linux虚拟机安装
1.真实机第一次安装必须先搞f2进入boot从光盘启动,虚拟机不用 进入的时候五个选项Install or upgrade an existing system:安装或升级现有系统Install sy ...
- 2014 Multi-University Training Contest 5
hdu4911 max(逆序数-k,0) #include <iostream> #include<stdio.h> #include<vector> #inclu ...
- (转)Web自动化测试中的接口测试
1.背景 1.1 Web程序中的接口 1.1.1 典型的Web设计架构 web是实现了基于网络通信的浏览器客户端与远程服务器进行交互的应用,通常包括两部分:web服务器和web客户端.web客户端的应 ...
- pkg-config问题:
pkg-config是一个工具,可以用于检测相应的依赖环境. pkg-config用来检索系统中安装库文件的信息,典型的是用作库的编译和连接.一般来说,如果库的头文件不在/usr/include目录中 ...
- golang 资源
1.Learning Go <学习Go语言> http://www.miek.nl/projects/learninggo/中文版http://mikespook.com/learning ...
- JVM系列三:JVM参数设置、分析
不管是YGC还是Full GC,GC过程中都会对导致程序运行中中断,正确的选择不同的GC策略,调整JVM.GC的参数,可以极大的减少由于GC工作,而导致的程序运行中断方面的问题,进而适当的提高Java ...
- Oracle查看表结构的几种方法
1,DESCRIBE 命令 使用方法如下: SQL> describe nchar_tst(nchar_tst为表名) 显示的结果如下: 名称 ...
- spring-boot资料
spring-boot-admin的相关资料 This application provides a simple UI to administrate Spring Boot application ...
- Axure_元件库
1.百度“推荐”,看到一篇文章“用Axure制作Material Design的APP原型(附元件库下载)” 想到 可以搜索 类似“axure 元件库”的关键字,来看看有哪些现成的元件库 2.
- mac10.12的Cocopods安装使用
Cocopods的安装 CocoaPods应该是iOS最常用最有名的类库管理当我们开发iOS应用时,会经常使用到很多第三方开源类库,比如AFNetWorking等等,可能某个类库又用到其他的库,手动一 ...