/*
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)的更多相关文章

  1. poj1184 聪明的打字员(BFS剪枝)

    http://poj.org/problem?id=1184 用字符串s存下数字,并把光标位置做一个字符加到s末尾,用map做标记状态是否出现过,然后bfs即可. 不剪枝是过不了的,考虑的两种交换操作 ...

  2. 聪明的打字员---poj1184(bfs)

    题目链接:http://poj.org/problem?id=1184 分析:首先可以发现有6*10^6种状态,比较多,不过搜索的时候可以去除一些无用的状态, 可以发现一个点的值(2-5)如果想要改变 ...

  3. codevs 3290 华容道(SPFA+bfs)

    codevs 3290华容道 3290 华容道 2013年NOIP全国联赛提高组 时间限制: 1 s  空间限制: 128000 KB 题目描述 Description 小 B 最近迷上了华容道,可是 ...

  4. Codevs 1138 聪明的质监员 2011年NOIP全国联赛提高组

    1138 聪明的质监员 2011年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 小 T 是一名质量监督员, ...

  5. POJ 1184 聪明的打字员

    简直难到没朋友. 双向bfs + 剪枝. 剪枝策略: 对于2--5位置上的数,仅仅有当光标在相应位置时通过swap ,up.down来改变.那么当当前位置没有达到目标状态时,left和right无意义 ...

  6. (广搜)聪明的打字员 -- POJ --1184

    链接: http://poj.org/problem?id=1184 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#probl ...

  7. codevs 1138 聪明的质监员

    二分+前缀和. #include<iostream> #include<cstdio> #include<cstring> #include<cmath> ...

  8. poj练习题的方法

    poj1010--邮票问题 DFSpoj1011--Sticks dfs + 剪枝poj1020--拼蛋糕poj1054--The Troublesome Frogpoj1062--昂贵的聘礼poj1 ...

  9. 06day2

    蠕虫游戏 模拟 [问题描述] 蠕虫是一个古老的电脑游戏,它有许多版本.但所有版本都有一个共同规则:操纵一条蠕虫在屏幕上转圈,并试着去避免撞到自己或障碍物. 这里我们将模拟一个简单的版本.游戏将在 50 ...

随机推荐

  1. Java学习-----单例模式

    一.问题引入 偶然想想到的如果把Java的构造方法弄成private,那里面的成员属性是不是只有通过static来访问呢:如果构造方法是private的话,那么有什么好处呢:如果构造方法是privat ...

  2. 《JavaScript dom 编程艺术》 placeholder占位符IE8兼容办法。

    在<JavaScript dom 编程艺术>第11章学来的. 相对于用JavaScript替换文本框的提示语句 <!DOCTYPE html> <html lang=&q ...

  3. WINFORM的DataGridView使用点滴

    1.如果有超过一屏的数据,想通过关键字查找到相关记录,然后再定位之,做法如下:遍历所有行,把某单元格的值和关键字对比,找到后清除所有选择行,然后把当前行设为选择,然后把grid的CurrentCell ...

  4. JQuery在光标位置插入内容

    (function($) { $.fn.extend({ insertAtCaret: function(myValue) { var $t = $(this)[0]; //IE if (docume ...

  5. UITextFiled自动补全输入,选中补全内容。NSRange和UITextRange的相互转换。-b

    有个需求就是 需要用户输入几位以后账号,可以根据本地存储的登录成功的账号,进行自动补全,并且补全内容为选中状态,不影响用户的新输入. 研究了一下,下面是完整的实现的方法. 补充个下载地址http:// ...

  6. The working copy xxxx needs to be upgraded to Subversion 1.7.

    原因是我在svn是低版本时候checkout的项目 而对方用的版本比我高 然后 我运行这个项目  就要求我  更新 如果我选择   对项目进行upgrade. 当 upgrade后 我的项目就在 现有 ...

  7. PIL Image 转成 wx.Image、wx.Bitmap

    import wx from PIL import Image def ConvertToWxImage(): pilImage = Image.open('1.png') image = wx.Em ...

  8. Vim记录

    Command Mode下: . 代表当前行 % 代表所有行 $ 代表结束行 :1,$normal i#     全部行前加#,同下 :%normal i# :read ! cd /usr/bin/; ...

  9. CLOUDSTACK HA功能,测试成功

    要注意VM HA和HOST HA两个级别的区别.并且要整合.

  10. 关于LEA指令(单周期就可以做简单的算术计算)

    堆栈种分配的局部变量所谓的“标号”,你以为是什么?(都是那些该死的宏惹的祸,大家要都是老老实实写代码,就不会有这些疑问了).      比如你用local在栈上定义了一个局部变量LocalVar,你知 ...