HDU 1043
http://acm.hdu.edu.cn/showproblem.php?pid=1043
http://www.cnblogs.com/goodness/archive/2010/05/04/1727141.html
根据这篇文章说的八数码八境界,我这种做法大概是在境界三。。
我选择的是康托展开作为哈希函数,在全排列问题中这个哈希函数可以很好的处理冲突
http://zh.wikipedia.org/zh/%E5%BA%B7%E6%89%98%E5%B1%95%E5%BC%80
当然白书上的那种哈希也是极好的。。
#include <iostream>
#include <queue>
#include <cstring>
using namespace std ;
int vis[] ;
int f[]={,,,,,,,,,} ;
int ct(int *a)
{
int ans= ;
for(int i= ;i< ;i++)
{
int cnt= ;
for(int j=i+ ;j< ;j++)
if(a[j]<a[i])
cnt++ ;
ans+=cnt*f[-i] ;
}
return ans ;
}
typedef struct L{
int a[] ;
int p,pos ;
string step ;
}L ;
int dx[]={,-,,} ;
int dy[]={,,,-} ;
string d="udlr" ;
string ans[] ;
void bfs()
{
queue <L> q ;
L now,next ;
for(int i= ;i< ;i++)
now.a[i]=i+ ;
now.a[]= ;
now.p=ct(now.a) ;
now.pos= ;
now.step="" ;
ans[now.p]="" ;
q.push(now) ;
vis[now.p]= ;
while(!q.empty())
{
now=q.front() ;
q.pop() ;
for(int i= ;i< ;i++)
{
int xx=now.pos/+dx[i] ;
int yy=now.pos%+dy[i] ;
if(xx< || xx> || yy< || yy>)continue ;
next=now ;
next.pos=xx*+yy ;
next.a[now.pos]=next.a[next.pos] ;
next.a[next.pos]= ;
next.p=ct(next.a) ;
if(!vis[next.p])
{
next.step+=d[i] ;
vis[next.p]= ;
ans[next.p]=next.step ;
q.push(next) ;
}
}
}
}
void pt(int *a)
{
int tt=ct(a) ;
if(!vis[tt])
{
puts("unsolvable") ;
return ;
}
for(int i=ans[tt].length()- ;i>= ;i--)
cout << ans[tt][i] ;
putchar('\n') ;
}
int main()
{
bfs() ;
char s[] ;
while(gets(s))
{
int st= ;
int t[] ;
for(int i= ;i<strlen(s) ;i++)
{
if(s[i]>='' && s[i]<='')
t[st++]=s[i]-'' ;
if(s[i]=='x')
t[st++]= ;
}
pt(t) ;
}
return ;
}
HDU 1043的更多相关文章
- POJ-1077 HDU 1043 HDU 3567 Eight (BFS预处理+康拓展开)
思路: 这三个题是一个比一个令人纠结呀. POJ-1077 爆搜可以过,94ms,注意不能用map就是了. #include<iostream> #include<stack> ...
- HDU 1043 Eight(八数码)
HDU 1043 Eight(八数码) 00 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Descr ...
- Eight POJ - 1077 HDU - 1043 八数码
Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...
- HDU - 1043 - Eight / POJ - 1077 - Eight
先上题目: Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- HDU 1043 Eight 八数码问题 A*算法(经典问题)
HDU 1043 Eight 八数码问题(经典问题) 题意 经典问题,就不再进行解释了. 这里主要是给你一个状态,然后要你求其到达\(1,2,3,4,5,6,7,8,x\)的转移路径. 解题思路 这里 ...
- 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]*( ...
- hdu 1043 Eight 经典八数码问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 The 15-puzzle has been around for over 100 years ...
- hdu 1043 pku poj 1077 Eight (BFS + 康拓展开)
http://acm.hdu.edu.cn/showproblem.php?pid=1043 http://poj.org/problem?id=1077 Eight Time Limit: 1000 ...
- HDU 1043 Eight(双向BFS+康托展开)
http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...
- HDU 1043 Eight(反向BFS+打表+康托展开)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各 ...
随机推荐
- shiro对事务的影响
记一个 No transaction aspect-managed TransactionStatus in scope 错误的解决方法 昨天出现一个BUG,事务没有加回滚成功,修改管理员密码事务没有 ...
- Spring MVC web.xml+servlet.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" " ...
- Docker 学习记录
docker logs 查看日志 docker logs 容器id docker logs -f 容器id 这次命令后面添加了一个新的标识 -f. 和 tail -f 类似, docker logs ...
- JAXB和XStream比较
转自:https://www.cnblogs.com/tang9139/p/4825610.html http://www.cnblogs.com/wlsblog/p/7452882.html 这两东 ...
- hiho#1080 更为复杂的买卖房屋姿势 线段树+区间更新
#1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们 ...
- tp5---auth权限搭建2
1.auth权限 composer auth库 下载完成之后 根据auth.php中所提及怎样创建表,就怎样建表 2.安利一个简单的建表操作 根据composer下载里的文件注释的代码,将其直接放到s ...
- 在Jupyter notebook中使用特定虚拟环境中的python的kernel
在虚拟环境tf中安装完tensorflow后,在虚拟环境tf打开的jupyter里发现只有一个kernel-python3,新建一个文件, import tensorflow as tf ,发 ...
- Java线程池可用的队列
Java线程池ThreadPoolExecutor的构造器: public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long ...
- Java 基于javaMail的邮件发送(支持附件)
基于JavaMail的Java邮件发送Author xiuhong.chen@hand-china.com Desc 简单邮件发送 Date 2017/12/8 项目中需要根据物料资质的状况实时给用户 ...
- [转]通过rsync+inotify-tools+ssh实现触发式远程实时同步
文件的同步镜像在很多地方都需要用到,因此rsync这款免费软件得到了广泛的应用,包括在Windows平台上,都已经有了支持rsync的“cwRsyncServer”. 但是,我们一般都是通过结合cro ...