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,将各 ...
随机推荐
- Ansible VMware模块使用示例
vmware_vm_facts模块使用示例 执行条件: 安装Pyvmimo: pip install pyvmomi 方法一,直接编写单个yaml文件: - hosts: localhost # 注 ...
- The Startup Manager FAQ
Main Features: 1. Login Items: Manageable list of applications that are launched automatically ever ...
- ASP.NET OAuth Authorization - Difference between using ClientId and Secret and Username and Password
What I don't fully understand is the use of ClientId and Secret vs Username and Password. The code ...
- centos mysql忘记密码
1.停止mysql 服务:service mysqld stop; 2.vim命令打开mysql配置文件my.cnf(位置一般为:/etc/my.cnf) 3.在mysqld进程配置文件中添加skip ...
- linux ps 命令参数详解
-a 显示所有终端机下执行的进程,除了阶段作业领导者之外. a 显示现行终端机下的所有进程,包括其他用户的进程. -A 显示所有进程. -c 显示CLS和PRI栏位. c 列出进程时,显示每个进程真正 ...
- jQuery全局冲突案例,解决$.noConflict()
如图:犹豫$在js中可以作为一个变量去定义,所以在引入jQuery包之前定义了$对象,那么,在引入jQuery包之后就不能使用$对象了 解决:使用$.noConflict()她可以返回一个对象,这个对 ...
- Angular 4.x 修仙之路
参考:https://segmentfault.com/a/1190000008754631 一个Angular4的博客教程目录
- import sys sys.path.append(...)
模块搜索路径: 当我们试图加载一个模块时,Python会在指定的路径下搜索对应的.py文件,如果找不到,就会报错 默认情况下,Python解释器会搜索当前目录.所有已安装的内置模块和第三方模块,搜索路 ...
- CentOS7 LVM添加硬盘及扩容
一.LVM简介 LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制.LVM将一个或多个磁盘分区(PV)虚拟为一个卷组(VG), ...
- 【Mongodb】数据库操作--备份、还原、导出和导入
文章转载自点这里 mongodb数据备份和还原主要分为二种,一种是针对于库的mongodump和mongorestore,一种是针对库中表的mongoexport和mongoimport. mongo ...