codevs 1733 聪明的打字员 (Bfs)
/*
Bfs+Hash 跑的有点慢 但是codevs上时间限制10s 也ok
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define maxn 10000010
using namespace std;
int len;
bool f[maxn];
string ls,rs;
struct node
{
int step,place;
string s;
};
queue<node>q;
int Hash(node x)
{
int re=;
len=x.s.length();
for(int i=;i<=len-;i++)
re=re*+x.s[i]-'';
re=re*+x.place;
return re;
}
int main()
{
cin>>ls>>rs;
ls=' '+ls;rs=' '+rs;
node st;st.s=ls;
st.step=;st.place=;
q.push(st);f[Hash(st)]=;
while(!q.empty())
{
node k=q.front();q.pop();
string si=k.s;
int p=k.place,t=k.step;
if(si==rs)
{
cout<<t;
return ;
}
for(int i=;i<=;i++)
{
int pi,ki;node x;
string ss=si;
if(i==&&p<)
{
pi=p;pi++;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
else if(i==&&ss[p]<'')
{
ss[p]++;pi=p;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
else if(i==&&ss[p]>'')
{
ss[p]--;pi=p;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
else if(i==)
{
char tmps=ss[p];ss[p]=ss[];ss[]=tmps;pi=p;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
else if(i==)
{
char tmps=ss[p];ss[p]=ss[];ss[]=tmps;pi=p;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
else if(i==&&p>)
{
pi=p;pi--;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
}
}
return ;
}
/*
加上剪枝的话就ok了 200ms
对于2 3 4 5这几个点 左移右移对答案是没有贡献的 只有1 6 左移右移再加上swap0 1才有贡献
所以2 3 4 5这几个只有已经和目标相同了才左右移
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define maxn 10000010
using namespace std;
int len;
bool f[maxn];
string ls,rs;
struct node
{
int step,place;
string s;
};
queue<node>q;
int Hash(node x)
{
int re=;
len=x.s.length();
for(int i=;i<=len-;i++)
re=re*+x.s[i]-'';
re=re*+x.place;
return re;
}
int main()
{
//freopen("clever.in","r",stdin);
//freopen("clever.out","w",stdout);
cin>>ls>>rs;
ls=' '+ls;rs=' '+rs;
node st;st.s=ls;
st.step=;st.place=;
q.push(st);f[Hash(st)]=;
while(!q.empty())
{
node k=q.front();q.pop();
string si=k.s;
int p=k.place,t=k.step;
if(si==rs)
{
cout<<t;
return ;
}
for(int i=;i<=;i++)
{
int pi,ki;node x;
string ss=si;
if(i==&&p<)
{
if((p==||p==||p==)&&ss[p]!=rs[p])continue;
pi=p;pi++;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
else if(i==&&ss[p]<'')
{
ss[p]++;pi=p;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
else if(i==&&ss[p]>'')
{
ss[p]--;pi=p;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
else if(i==)
{
char tmps=ss[p];ss[p]=ss[];ss[]=tmps;pi=p;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
else if(i==)
{
char tmps=ss[p];ss[p]=ss[];ss[]=tmps;pi=p;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
else if(i==&&p>)
{
if((p==||p==||p==)&&ss[p]!=rs[p])continue;
pi=p;pi--;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
}
}
return ;
}
codevs 1733 聪明的打字员 (Bfs)的更多相关文章
- poj1184 聪明的打字员(BFS剪枝)
http://poj.org/problem?id=1184 用字符串s存下数字,并把光标位置做一个字符加到s末尾,用map做标记状态是否出现过,然后bfs即可. 不剪枝是过不了的,考虑的两种交换操作 ...
- 聪明的打字员---poj1184(bfs)
题目链接:http://poj.org/problem?id=1184 分析:首先可以发现有6*10^6种状态,比较多,不过搜索的时候可以去除一些无用的状态, 可以发现一个点的值(2-5)如果想要改变 ...
- codevs 3290 华容道(SPFA+bfs)
codevs 3290华容道 3290 华容道 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description 小 B 最近迷上了华容道,可是 ...
- Codevs 1138 聪明的质监员 2011年NOIP全国联赛提高组
1138 聪明的质监员 2011年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 小 T 是一名质量监督员, ...
- POJ 1184 聪明的打字员
简直难到没朋友. 双向bfs + 剪枝. 剪枝策略: 对于2--5位置上的数,仅仅有当光标在相应位置时通过swap ,up.down来改变.那么当当前位置没有达到目标状态时,left和right无意义 ...
- (广搜)聪明的打字员 -- POJ --1184
链接: http://poj.org/problem?id=1184 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#probl ...
- codevs 1138 聪明的质监员
二分+前缀和. #include<iostream> #include<cstdio> #include<cstring> #include<cmath> ...
- poj练习题的方法
poj1010--邮票问题 DFSpoj1011--Sticks dfs + 剪枝poj1020--拼蛋糕poj1054--The Troublesome Frogpoj1062--昂贵的聘礼poj1 ...
- 06day2
蠕虫游戏 模拟 [问题描述] 蠕虫是一个古老的电脑游戏,它有许多版本.但所有版本都有一个共同规则:操纵一条蠕虫在屏幕上转圈,并试着去避免撞到自己或障碍物. 这里我们将模拟一个简单的版本.游戏将在 50 ...
随机推荐
- 使用EF实现数据库的增删改查
EF的使用步骤:(1)将EF添加到项目:在Model右击添加新建项找到ADO.NET实体数据模型,接着…(2)实现数据库的增删改查查询(因为在Model中已经添加EF实体了,所以就可以在Control ...
- JSON基础知识总结
JSON基础 一.JSON简介 JSON,全称“JavaScript Object Notation(JavaScript对象表示法)”,起源于JavaScript的对象和数组.JSON,说白了就是J ...
- winform批量查询单号剔除重复
//查询分单函数 private string GetQueryInSubbillNo() { string strSubbillNO = " ...
- C# ,asp.net 获取当前,相对,绝对路径(转)
C# ,asp.net 获取当前,相对,绝对路径 一.C#获取当前路径的方法: . System.Diagnostics.Process.GetCurrentProcess().MainModule. ...
- GFStableList Adapter
STL中,list的优点是插入.删除性能极佳(时间复杂度只需O(1)即可),而且非常重要的在删除节点后,其迭代器不失效,但list查找却不擅长.map由于其实现的数据结构为rb-tree,因此,其插入 ...
- IOS中扩展机制Category和associative
在ios开发中,有时候会遇到以下的问题,需要在一个类中添加自己的一些属性和方法.一般的做法是重写一个类来继承它,但是有时候就只是需要添加一些简单的属性和方法,那么这样做就显得过于麻烦,其实在IOS中还 ...
- Memcached源码分析——hash
以下为memcached中关于使用的hash算法的一点记录 memcached中默认使用的是Bob Jenkins的jenkins_hash算法 以下4段代码均在memcached-1.4.22/ha ...
- RUBY,玩玩~~~
我觉得吧,这东瀛的红宝石,也得玩玩,毕竟,RUBY ON RAILS,PUPPET等也是一股力量. 作为混IT圈的,知道总没坏处. 就是感觉和C,C++,JAVA,C#,PHP,甚至PYTHON的感觉 ...
- 玩了一下SDN:MININET+FLOODLIGHT,感觉这确实是一个趋势啊
功能用增加中间层的方案来解决. 仿佛回到用交换机和路由器模拟器的感觉. 遇到执行命令小问题,狗哥搞定: mininet>mininet> dpctl dump-flows tcp:127. ...
- 萬用表檢測MOS管好壞的簡便方法
在開發LED驅動電源時難免不會接觸到MOS管,它又是一個相當脆弱的器件.往往有時故障就是因為它罷工了.以下的一點經驗希望對大家有所幫助. 1.用黑表筆接在D極上 ,紅表筆接在S極上 , 一般有一個5 ...