/*
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. jQuery实现checkbox全选反选及删除等操作

    1.list.html 说明:用checkbox数组Check[]存放每一行的ID值 <div id="con"> <table width="100% ...

  2. w3wp异常

    相信做ASP.NET中大型Web应用的人都碰到过OutOfMemoryException这个异常,对于这个问题我研究了很久,在微软的技术文档上也了解过此问题出现的原因,说实话,到目前我仍然没有完美的解 ...

  3. windows批处理命令之ren

    1.批处理批量修改文件后缀名(假设我需要把一个文件夹中的很多txt文件改为sql文件): 1)在需要被处理的文件的文件夹里先新建一个txt文本,然后在文本中写入: ren *.txt *.sql 2) ...

  4. 【学习笔记】【oc】指针

    1.取一个变量的值,可通过直接与间接的方式 直接:访问变量 间接:通过变量的内存地址来获取变量的值. 存放内存地址的变量就是指针变量. 2.定义指针变量的语法:类型* 变量名 &:取地址运算符 ...

  5. 10 - 删除vtkDataObject中的Update Extent方法 VTK 6.0 迁移

    VTK6 引入了许多不兼容的变.其中之一是删除vtkDataObject中所有有关管道的方法.下面列出来的就是其中的一些方法: SetUpdateExtent(int piece, int numPi ...

  6. 转:7个鲜为人知却超实用的PHP函数

    PHP have lots of built-in functions, and most developers know many of them. But a few functions are ...

  7. keil中使用_at_绝对地址定位

    使用_at_关键字对存储器进行绝对地址定位程序如下: #include<reg51.h> ] _at_ 0x8000; main() { LED_Data[] = 0x23; } 在kei ...

  8. iPhone之为UIView设置阴影(CALayer的shadowColor,shadowOffset,shadowOpacity,shadowRadius,shadowPath属性)

    效果图: 以下代码实现: 第一个图片的代码 //加阴影--任海丽编辑 _imageView.layer.shadowColor = [UIColor blackColor].CGColor;//sha ...

  9. COJ 0501 取数游戏(TPM)

    取数游戏(TPM) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给你一个n*n的格子的棋盘,每个格子里面有一个非负数.从中取 ...

  10. STL_iterator迭代器(2)——几种迭代器对象的用法

    要学会使用迭代器和容器以及算法,需要学习下面的新技术. 一.流和迭代器 本书的很多例子程序使用I/O流语句来读写数据.例如: int value; cout << "Enter ...