HDU 1043
2024-08-25 19:06:00
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 ;
}
最新文章
- 【codevs1515】 跳
- Get a List of Keys From a Dictionary in Both Python 2 and Python 3
- C# 使用 Abot 实现 爬虫 抓取网页信息 源码下载
- NOIP201105铺地毯
- android 更新uI主线程
- Quartz2D使用
- PHP Sessions
- QT小记之在VS2005中使用(设置QMAKESPEC环境变量,以及编译QT Lib)
- JDK安装、java环境配置
- Vijos 1111 小胖的水果 LCS
- rocketMQ No route info of this topic 错误
- Spring Data Redis 让 NoSQL 快如闪电(2)
- http: server gave HTTP response to HTTPS client &; Get https://192.168.2.119/v2/: dial tcp 192.168.2.119:443: getsockopt: connection refused
- 恢复误删的DB table数据
- hadoop执行 报错
- Oracle PL/SQL异常、存储过程和触发器
- centos 7 安装svn客户端
- blktrace分析IO
- Web知识简易介绍及HTTP知识总结
- WCF RestFull提交数据超出限额解决方法
热门文章
- cogs 341:[NOI2005] 聪聪与可可
- MySQL日常维护
- Math.Round 四舍五入问题 解惑 !
- 关于http请求ContentType:application/x-www-form-urlencoded
- 【转】Java运行时数据区简介及堆与栈的区别
- 微信小程序-----安装,编写第一个小程序和运行到手机端
- 51nod-1103-抽屉原理
- HDU 4665 Mutiples on a circle (圆环DP)
- day19 Models补充+缓存+信号+序列化+分析抽屉页面
- day5-os、sys模块