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*算法是一种启发式搜索,具体实现要用到优先队列/堆,不同 ...
随机推荐
- HDU5829 NTT
以下这份代码并没有过.但感觉没有问题.不是蜜汁WA就是蜜汁T. #include <cstdio> #include <iostream> #include <cstri ...
- Binary Tree Paths
Description: Given a binary tree, return all root-to-leaf paths. For example, given the following bi ...
- android 主线程和子线程之间的消息传递
从主线程发送消息到子线程(准确地说应该是非UI线程) package com.zhuozhuo; import android.app.Activity; import android.os.Bun ...
- jQuery DOM基础
jQuery DOM基础 1.对元素内容的获取和修改: 表单用value(),普通元素用html()和text(). html() html(value)设置和获取html内容,有html标签会自动 ...
- supervisord
[简介] supervisord的官网:http://supervisord.org.看懂英文的可以不用看我的博客,直接看文档就行了,文档写得非常好.点个赞!! Supervisor是一个客户/服务器 ...
- Spring3 表达式语言(SpEL)介绍
转载自:http://iyiguo.net/blog/2011/06/19/spring-expression-language/ 下一版本 项目需要使用到SpEL ,做一个保存.
- lftp
linux安装FTP工具 lftp及使用教程 来源:网络 发布时间:2013-05-24 15:21 字体:[大 中 小] 点击2510次 linux下可以直接通过FTP命令进行ftp上传下载,不 ...
- Linux chmod命令修改文件与文件夹权限的命令附实例
Linux chmod命令修改文件与文件夹权限的命令附实例 作者:佚名 字体:[增加 减小] 来源:互联网 时间:05-01 20:46:07我要评论 在linux中要修改一个文件夹或文件的权限我们需 ...
- paramiko-客户端和服务器认证工具
required: python+pycrypto 1.安装pycrypto 726 cd /opt/ 727 wget http://ftp.dlitz.net/pub/dlitz/crypto/p ...
- android 开发 常用工具类
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38965311,本文出自[张鸿洋的博客] 打开大家手上的项目,基本都会有一大批的辅 ...