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的更多相关文章

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

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

  2. HDU 1043 Eight(八数码)

    HDU 1043 Eight(八数码) 00 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   Problem Descr ...

  3. Eight POJ - 1077 HDU - 1043 八数码

    Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...

  4. HDU - 1043 - Eight / POJ - 1077 - Eight

    先上题目: Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  5. HDU 1043 Eight 八数码问题 A*算法(经典问题)

    HDU 1043 Eight 八数码问题(经典问题) 题意 经典问题,就不再进行解释了. 这里主要是给你一个状态,然后要你求其到达\(1,2,3,4,5,6,7,8,x\)的转移路径. 解题思路 这里 ...

  6. 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]*( ...

  7. hdu 1043 Eight 经典八数码问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 The 15-puzzle has been around for over 100 years ...

  8. 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 ...

  9. HDU 1043 Eight(双向BFS+康托展开)

    http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...

  10. HDU 1043 Eight(反向BFS+打表+康托展开)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各 ...

随机推荐

  1. @component的注解

    1.@controller 控制器(注入服务) 2.@service 服务(注入dao) 3.@repository dao(实现dao访问) 4.@component (把普通pojo实例化到spr ...

  2. LeetCode—— Partition Equal Subset Sum

    Question Given a non-empty array containing only positive integers, find if the array can be partiti ...

  3. c#的逆向工程-IL指令集

    一些 IL 语言解释:  跳转指令集合 Public field Static     Beq     如果两个值相等,则将控制转移到目标指令. Public field Static     Beq ...

  4. MongoDB 副本集和C#交互,简单测试

    MongoDB 副本集和C#交互,简单测试 primary节点宕机: 模拟primary节点宕机的情况,这时查看监控: 可以看到37018已经成了primary节点.主界面宕机导致了整个集群发生一次e ...

  5. Java数字证书操作

    为服务器生成证书 keytool -genkey -v -alias tomcat -keyalg RSA -keystore D:\tomcat.keystore -validity 36500 为 ...

  6. Ubuntu 安装 networkx

    参考:ubuntu 下NetworkX的安装和使用 Dependences pip setuptools Commands 1.install networkx sudo pip install ne ...

  7. redis 与session

    springboot:session集中存储到redis https://www.cnblogs.com/huiy/p/6907164.html springboot处理session生命周期 htt ...

  8. 51nod 1187 寻找分数

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  9. 可以,得驾证了\(^o^)/

    说实在话,我自认为我不太适合开车. 首先我有点晕车,在车上坐久了就头晕脑胀. 心里素质也不行,凡是遇到什么事了,就慌慌张张,手忙脚乱. 然后就是练车一点都不稳,每次练车都会有新问题出现. 先说一下我科 ...

  10. 1-16-2 LVM管理和ssm存储管理器使用&磁盘配额

    ssm存储管理器使用&磁盘配额 ssm存储管理器使用 系统存储管理器的使用 系统存储管理器(又称ssm,即system-storage-manager),是RHEL7/CentOS7新增的功能 ...