HDOJ1195 双向BFS //单向也可以过 没想清
#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 //单向也可以过 没想清的更多相关文章
- UVA - 1601 The Morning after Halloween (双向BFS&单向BFS)
题目: w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...
- Word Ladder(双向BFS)
2018-10-02 23:46:38 问题描述: 问题求解: 显然是个解空间遍历问题,每次修改其中一位,由于步长是1,所以可以使用BFS进行解空间的遍历.
- POJ1915Knight Moves(单向BFS + 双向BFS)
题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...
- POJ 1915-Knight Moves (单向BFS && 双向BFS 比)
主题链接:Knight Moves 题意:8个方向的 马跳式走法 ,已知起点 和终点,求最短路 研究了一下双向BFS,不是非常难,和普通的BFS一样.双向BFS只是是从 起点和终点同一时候開始搜索,可 ...
- 双向BFS和启发式搜索的应用
题目链接 P5507 机关 题意简述 有12个旋钮,每个旋钮开始时处于状态 \(1\) ~ \(4\) ,每次操作可以往规定方向转动一个旋钮 (\(1\Rightarrow2\Rightarrow ...
- [转] 搜索之双向BFS
转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...
- 双向BFS
转自“Yuan” 如果目标也已知的话,用双向BFS能很大提高速度 单向时,是 b^len的扩展. 双向的话,2*b^(len/2) 快了很多,特别是分支因子b较大时 至于实现上,网上有些做法是用两个 ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- POJ 3126 Prime Path 解题报告(BFS & 双向BFS)
题目大意:给定一个4位素数,一个目标4位素数.每次变换一位,保证变换后依然是素数,求变换到目标素数的最小步数. 解题报告:直接用最短路. 枚举1000-10000所有素数,如果素数A交换一位可以得到素 ...
随机推荐
- Python及Pycharm安装详细教程
参考:http://blog.csdn.net/qq_29883591/article/details/52664478 首先我们来安装python 1.首先进入网站下载:点击打开链接(或自己输入网址 ...
- bootstrap的tab中,echarts 图表宽度设为100%之后,会出现图表宽带变为100px的情况。只有第一个正常
1.原因 echarts官方解释是 Tip: 有时候图表会放在多个标签页里,那些初始隐藏的标签在初始化图表的时候因为获取不到容器的实际高宽,可能会绘制失败,因此在切换到该标签页时需要手动调用resiz ...
- Git小白到老鸟的进阶之路
点"计算机视觉life"关注,置顶更快接收消息! 小白:师兄,师兄,上次你教我的操作,我傻乎乎的执行了一遍,可是那个Git究竟是什么那? 师兄:小白莫慌,Git就是一种版本控制,小 ...
- CodeForces 689C【二分】
转自: http://blog.csdn.net/qq_26071477/article/details/51892995 #include<stdio.h> typedef long l ...
- Bundle Adjustment光束平差法概述
http://blog.csdn.net/abcjennifer/article/details/7588865 http://blog.csdn.net/ximenchuixuezijin/arti ...
- android调用其他apk的activity
<img src="https://img-blog.csdn.net/20160322114625025" alt="" />启动另一个apk的工 ...
- Mol Cell Proteomics. |陈洁| 整合鸟枪法蛋白质组学中鉴定和定量的错误率
大家好,本周分享的是发表在MCP上的一篇关于鸟枪蛋白质组学中的错误率的文章,题目是Integrated identification and quantification error probabil ...
- 返回零长度的数组或集合,而不是null
返回零长度的数组或集合,而不是null 像下面的方法并不少见: private final List<Cheese> cheesesInStock = ...; /** * @retu ...
- NOIP 2006 T2 金明的预算方案
题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过NN元钱就行”. ...
- iOS 7:漫谈#define 宏定义(转)
iOS :漫谈#define 宏定义 #define宏定义在C系开发中可以说占有举足轻重的作用.底层框架自不必说,为了编译优化和方便,以及跨平台能力,宏被大量使用,可以说底层开发离开define将寸步 ...