#include<cstdio>
#include<map>
#include<vector>
#include<stack>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdlib>
#define PI acos(-1.0)
using namespace std;
typedef long long ll;
const ll mood=1e9+;
const double eps=1e-;
const int N=1e4+;
const int MAXN=;
struct node{
string password;
int cnt;
}now,tmp;
string beg,end;
map<string,int>back_vis;
map<string,int>vis;
queue<struct node>q;
queue<struct node>back_q;
int back_bfs(int n)//反向BFS,每次只搜一层,即第n层
{
while(back_q.front().cnt<=n)
{
now=back_q.front();
back_q.pop();
for(int i=;i<;i++)
{ //各个位-1\
tmp=now;
if(tmp.password[i]!='')
tmp.password[i]--;
else tmp.password[i]='';
if(vis.find(tmp.password)!=vis.end())//判断是否在正向队列中找到
return tmp.cnt++vis[tmp.password];
if(back_vis.find(tmp.password)==back_vis.end())
{
tmp.cnt++;
back_q.push(tmp);
back_vis[tmp.password]=tmp.cnt;
} //各个位+1
tmp=now;
if(tmp.password[i]!='') tmp.password[i]++;
else tmp.password[i]='';
if(vis.find(tmp.password)!=vis.end())//判断是否在正向队列中找到
return tmp.cnt++vis[tmp.password];
if(back_vis.find(tmp.password)==back_vis.end())
{
tmp.cnt++;
back_q.push(tmp);
back_vis[tmp.password]=tmp.cnt;
}
}
for(int i=;i<;i++)
{
tmp=now;
swap(tmp.password[i],tmp.password[i+]);
if(vis.find(tmp.password)!=vis.end())//判断是否在正向队列中找到 、
return tmp.cnt++vis[tmp.password];
if(back_vis.find(tmp.password)==back_vis.end())
{ tmp.cnt++; back_q.push(tmp); back_vis[tmp.password]=tmp.cnt; }
}
}
return -;
}
int bfs()
{
while(!q.empty())
q.pop();//清空正向BFS的队列
now.password=beg;
now.cnt=;
q.push(now);
vis[beg]=;
while(!q.empty())
{
int n=q.front().cnt;
while(q.front().cnt<=n)
{
now=q.front(); q.pop();
for(int i=;i<;i++)
{ //各个位-1
tmp=now;
if(tmp.password[i]!='')
tmp.password[i]--;
else tmp.password[i]='';
if(back_vis.find(tmp.password)!=back_vis.end())//判断是否在反向队列中找到
return tmp.cnt++back_vis[tmp.password];
if(vis.find(tmp.password)==vis.end())
{ tmp.cnt++; q.push(tmp); vis[tmp.password]=tmp.cnt; } //各个位+1
tmp=now;
if(tmp.password[i]!='') tmp.password[i]++;
else tmp.password[i]='';
if(back_vis.find(tmp.password)!=back_vis.end())//判断是否在反向队列中找到
return tmp.cnt++back_vis[tmp.password];
if(vis.find(tmp.password)==vis.end())
{ tmp.cnt++; q.push(tmp); vis[tmp.password]=tmp.cnt; }
}
for(int i=;i<;i++)
{
tmp=now;
swap(tmp.password[i],tmp.password[i+]);
if(back_vis.find(tmp.password)!=back_vis.end())//判断是否在反向队列中找到
return tmp.cnt++back_vis[tmp.password];
if(vis.find(tmp.password)==vis.end()) {
tmp.cnt++; q.push(tmp);
vis[tmp.password]=tmp.cnt;
}
}
}
int ret=back_bfs(now.cnt);
if(ret!=-)
return ret;
}
}
int main(){
int t;
while(cin>>t)
{
while(t--)
{
cin>>beg;
cin>>end;
vis.clear();//清空map
back_vis.clear();//清空map
while(!back_q.empty())
back_q.pop();//清空反向BFS的队列
now.password=end;
now.cnt=;
back_q.push(now);
back_vis[end]=;//将反向BFS的起始点入队列标记
int ret=bfs();
cout<<ret<<endl;
}
}
return ;
}

还没懂

HDOJ1195 双向BFS //单向也可以过 没想清的更多相关文章

  1. UVA - 1601 The Morning after Halloween (双向BFS&单向BFS)

    题目: w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...

  2. Word Ladder(双向BFS)

    2018-10-02 23:46:38 问题描述: 问题求解: 显然是个解空间遍历问题,每次修改其中一位,由于步长是1,所以可以使用BFS进行解空间的遍历.

  3. POJ1915Knight Moves(单向BFS + 双向BFS)

    题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...

  4. POJ 1915-Knight Moves (单向BFS &amp;&amp; 双向BFS 比)

    主题链接:Knight Moves 题意:8个方向的 马跳式走法 ,已知起点 和终点,求最短路 研究了一下双向BFS,不是非常难,和普通的BFS一样.双向BFS只是是从 起点和终点同一时候開始搜索,可 ...

  5. 双向BFS和启发式搜索的应用

    题目链接 P5507 机关 题意简述   有12个旋钮,每个旋钮开始时处于状态 \(1\) ~ \(4\) ,每次操作可以往规定方向转动一个旋钮 (\(1\Rightarrow2\Rightarrow ...

  6. [转] 搜索之双向BFS

    转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...

  7. 双向BFS

    转自“Yuan” 如果目标也已知的话,用双向BFS能很大提高速度 单向时,是 b^len的扩展. 双向的话,2*b^(len/2)  快了很多,特别是分支因子b较大时 至于实现上,网上有些做法是用两个 ...

  8. HDU 3085 Nightmare Ⅱ (双向BFS)

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. POJ 3126 Prime Path 解题报告(BFS & 双向BFS)

    题目大意:给定一个4位素数,一个目标4位素数.每次变换一位,保证变换后依然是素数,求变换到目标素数的最小步数. 解题报告:直接用最短路. 枚举1000-10000所有素数,如果素数A交换一位可以得到素 ...

随机推荐

  1. linux中的调试知识---基础gdb和strace查看系统调用信息,top性能分析,ps进程查看,内存分析工具

    1 调试一般分为两种,可以通过在程序中插入打印语句.有点能够显示程序的动态过程,比较容易的检查出源程序中的有关信息.缺点就是效率比较低了,而且需要输入大量无关的数据. 2 借助相关的调试工具. 3 有 ...

  2. python常用框架及第三方库

    python常用框架及第三方库 一.Web框架 1.Django: 开源web开发框架,它鼓励快速开发,并遵循MVC设计,比较庞大,开发周期短.Django的文档最完善.市场占有率最高.招聘职位最多. ...

  3. Bootstrap表格分页(一)

    最近在学习Bootstrap的分页,有一种方法用“Bootstrap-Paginator”的东西来做. 先预览一下: 为了能够局部刷新页面,我创建了一个PartialView 页面的HTML部分如下: ...

  4. 609. Find Duplicate File in System

    Given a list of directory info including directory path, and all the files with contents in this dir ...

  5. elasticsearch 部署

    环境 ubuntu 12.04 64位 桌面版 jdk 1.7 elasticsearch 2.3.4 伪集群部署 elasticsearch 主目录在 /home/sdbadmin/es-clute ...

  6. dumpe2fs: Bad magic number in super-block

    今天使用tune2fs和dumpe2fs来查看文件系统信息,出现如下图所示错误提示: 解决方法: 1.原来tune2fs和dumpe2fs只能打开ext3/ext4等文件类型. dumpe2fs - ...

  7. bzoj2825:[AHOI2012]收集资源

    传送门 看到数据范围这么小,就没想过暴力的办法么 考虑肯定是从近走到远,所以走的点之间一定没有其他的点,所以我们就可以暴力的建图,然后暴力的去dfs就好了 代码: #include<cstdio ...

  8. dom4j的下载 在线文档 创建用户库

  9. G - You Are the One(需要重想一遍)

    #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...

  10. JQuery | trigger() 方法

    trigger() 方法触发被选元素的指定事件类型. 语法格式: trigger(type,[data]) type:触发事件类型 [data]:可选项,表示在触发事件时传递给函数的附加参数. 实例: ...