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 ;
}

最新文章

  1. 【codevs1515】 跳
  2. Get a List of Keys From a Dictionary in Both Python 2 and Python 3
  3. C# 使用 Abot 实现 爬虫 抓取网页信息 源码下载
  4. NOIP201105铺地毯
  5. android 更新uI主线程
  6. Quartz2D使用
  7. PHP Sessions
  8. QT小记之在VS2005中使用(设置QMAKESPEC环境变量,以及编译QT Lib)
  9. JDK安装、java环境配置
  10. Vijos 1111 小胖的水果 LCS
  11. rocketMQ No route info of this topic 错误
  12. Spring Data Redis 让 NoSQL 快如闪电(2)
  13. http: server gave HTTP response to HTTPS client &amp; Get https://192.168.2.119/v2/: dial tcp 192.168.2.119:443: getsockopt: connection refused
  14. 恢复误删的DB table数据
  15. hadoop执行 报错
  16. Oracle PL/SQL异常、存储过程和触发器
  17. centos 7 安装svn客户端
  18. blktrace分析IO
  19. Web知识简易介绍及HTTP知识总结
  20. WCF RestFull提交数据超出限额解决方法

热门文章

  1. cogs 341:[NOI2005] 聪聪与可可
  2. MySQL日常维护
  3. Math.Round 四舍五入问题 解惑 !
  4. 关于http请求ContentType:application/x-www-form-urlencoded
  5. 【转】Java运行时数据区简介及堆与栈的区别
  6. 微信小程序-----安装,编写第一个小程序和运行到手机端
  7. 51nod-1103-抽屉原理
  8. HDU 4665 Mutiples on a circle (圆环DP)
  9. day19 Models补充+缓存+信号+序列化+分析抽屉页面
  10. day5-os、sys模块