hdu-1043 bfs+康拓展开hash
因为是计算还原成一种局面的最短步骤,应该想到从最终局面开始做bfs,把所有能到达的情况遍历一遍,把值存下来。
bfs过程中,访问过的局面的记录是此题的关键,9*9的方格在计算过程中直接存储非常占内存。而这个显然是12345678x的不同排列,考虑康拓展开来记录,每个局面hash成一个整数。步骤我先算了一下,最多有31步,我用4进制位hash成了两个整数来保存
#include <iostream>
#include <iomanip>
#include <set>
#include <cmath>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#define LL long long
using namespace std;
class cantor
{
public:
#define siz 9
char c[siz]={'','','','','','','','','x'};
LL w[siz];
bool vis[siz];
cantor()
{
w[]=;
for(int i=;i<siz;i++)
w[i]=w[i-]*i;
}
void init()
{
for(int i=;i<siz;i++)
vis[i]=false;
}
LL makeCanto(string s)
{
init();
LL rec=;
for(int i=;i<siz;i++)
{
int d=;
for(int j=;j<siz;j++)
{
if(vis[j])
continue;
if(c[j]!=s[i])d++;
else
{
vis[j]=true;
break;
}
}
rec+=w[siz-i-]*d;
}
return rec;
}
string recover(LL val)
{
init();
string s="";
for(int i=siz-;i>=;i--)
{
LL te=val/w[i];
val-=e*w[i];
for(int j=,cnt=-;j<siz;j++)
{
if(vis[j])continue;
else cnt++;
if(cnt==te&&!vis[j])
{
s+=c[j];
vis[j]=true;
break;
}
}
}
return s;
}
};
LL n,m;
char mp[][];
set<LL> s;
string ss="";
const string las="12345678x";
bool f;
int dir[][]={-,,,,,,,-};
char dfuck[]={'d','u','l','r'};
struct node
{
LL s;
int c;
int x,y;
LL a,b;
node(LL ss,int aa,int X,int Y,LL A,LL B){s=ss;c=aa;x=X;y=Y;a=A;b=B;}
};
struct ax
{
LL a,b,c;
ax(LL A,LL B,LL C){a=A;b=B;c=C;}
ax(){}
};
map<LL,ax> ans;
LL bit[];
int main()
{
cin.sync_with_stdio(false);
cantor fx;
bit[]=;
for(int i=;i<;i++)
bit[i]=bit[i-]*;
queue<node> q;
node ini=node(fx.makeCanto(las),,,,,);
q.push(ini);
s.clear();
ans.clear();
int mx=;
s.insert(fx.makeCanto(las));
while(!q.empty())
{
node now=q.front();
ans[now.s]=ax(now.a,now.b,now.c);
q.pop();
mx=max(now.c,mx);
for(int i=;i<;i++)
{
int yy=now.y+dir[i][];
int xx=now.x+dir[i][];
if(xx<||yy<||xx>=||yy>=)continue;
string nx=fx.recover(now.s);
swap(nx[now.y*+now.x],nx[yy*+xx]);
LL nxv=fx.makeCanto(nx);
if(s.find(nxv)!=s.end())continue;
s.insert(nxv);
LL na=now.a,nb=now.b;
if(now.c<)
{
na+=i*bit[now.c];
}
else
{
nb+=i*bit[now.c-];
}
q.push(node(nxv,now.c+,xx,yy,na,nb));
}
}
while(cin>>mp[][]>>mp[][]>>mp[][]>>mp[][]>>mp[][]>>mp[][]>>mp[][]>>mp[][]>>mp[][])
{
int x,y;
f=false;
s.clear();
ss="";
for(int i=;i<;i++)
for(int j=;j<;j++)
{
ss+=mp[i][j];
if(mp[i][j]=='x')
y=i,x=j;
}
string o="";
if(ss=="12345678x")
{
cout<<"lr"<<endl;
continue;
}
if(ans.find(fx.makeCanto(ss))==ans.end())
{
cout<<"unsolvable"<<endl;
continue;
}
ax fuck=ans[fx.makeCanto(ss)];
for(int i=;i<fuck.c;i++)
{
if(i<)
{
o+=dfuck[fuck.a%];
fuck.a/=;
}
else
{
o+=dfuck[fuck.b%];
fuck.b/=;
}
}
reverse(o.begin(),o.end());
cout<<o<<endl;
}
}
。
hdu-1043 bfs+康拓展开hash的更多相关文章
- HDU 4531  bfs/康拓展开
		题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4531 吉哥系列故事——乾坤大挪移 Time Limit: 2000/1000 MS (Java/Othe ... 
- Eight (HDU - 1043|POJ - 1077)(A* | 双向bfs+康拓展开)
		The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ... 
- 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 ... 
- 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]*( ... 
- 【HDOJ3567】【预处理bfs+映射+康拓展开hash】
		http://acm.hdu.edu.cn/showproblem.php?pid=3567 Eight II Time Limit: 4000/2000 MS (Java/Others) Me ... 
- bnuoj 1071 拼图++(BFS+康拓展开)
		http://www.bnuoj.com/bnuoj/problem_show.php?pid=1071 [题意]:经过四个点的顺逆时针旋转,得到最终拼图 [题解]:康拓展开+BFS,注意先预处理,得 ... 
- 九宫重拍(bfs + 康拓展开)
		问题描述 如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着.与空格子相邻的格子中的卡片可以移动到空格中.经过若干次移动,可以形成第二个图所示的局面. 我们把第一个图的局面记为:12 ... 
- 8数码,欺我太甚!<bfs+康拓展开>
		不多述,直接上代码,至于康拓展开,以前的文章里有 #include<iostream> #include<cstdio> #include<queue> using ... 
- hdu 1430(BFS+康托展开+映射+输出路径)
		魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ... 
随机推荐
- [本体论][UML][统一建模语言][软件建模][OWL]从本体论到UML到OWL
			以下内容,是关于软件建模的方法与思路. UML与OWL都是基于本体论的建模语言. 本体论(哲学) 本体论(信息科学) UML(统一建模语言) more info 参考:[设计语言][统一建模语言][软 ... 
- Android Studio--》Gradle Scripts配置说明
			什么是Gradle? Gradle是一种依赖管理工具,基于Groovy语言,面向Java应用为主,它抛弃了基于XML的各种繁琐配置,取而代之的是一种基于Groovy的内部领域特定(DSL)语言. 安装 ... 
- 构造方法 this super
			1 构造方法 1.1 构造方法Constructor概述创建对象要明确属性值,此时需要用到构造方法,即对象创建时要执行的方法,用来给对象的属性进行初始化.在new对象时,知道其执行的构造方法是什么,就 ... 
- property 和 魔法方法
			property和魔法方法 一.property 二.model,class,bases,mro 三.__doc__, __dict__,__call__,__item__,__len__,__str ... 
- mongodb删除重复数据
			注:mongodb当前版本是3.4.3 插入六条数据: 查询存在重复的数据: 查询并循环删除重复数据: 删除语句解析: db.userInfo.aggregate([ { ... 
- etree导入问题
			原因:主要是lxml没有这个包的问题,需要安装下: 1.需要在https://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml 下选择你和你对应的pycharm对应的版 ... 
- Oracle时间日期函数
			ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year: yy two digits 两位年 ... 
- 细说flask数据库迁移
			什么情况下要用数据库迁移? 在开发过程中,需要修改数据库模型,而且还要在修改之后更新数据库.最直接的方式就是删除旧表,但这样会丢失数据. 更好的解决办法是使用数据库迁移框架,它可以追踪数据库模式的变化 ... 
- Install rapyuta Robot Cloud Engine on Ubuntu12.04
			Prepare on ubuntu12.04 sudo apt-get install vim Install fuerte ROS sudo sh -c 'echo "deb http:/ ... 
- vue store存储commit和dispatch
			vue store存储commit和dispatch this.$store.commit('toShowLoginDialog', true);this.$store.dispatch('toSho ... 
