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交换一位可以得到素 ...
随机推荐
- Git(二)--常用命令
$ git init 从当前目录初始化 $ git clone git://xxxx 从远程仓库克隆 $ git status 确定哪些文件当前处于什么状态 $ git add 开始跟踪一个新文件 ...
- HDU - 6016 Count the Sheep 二分图+思维
Count the Sheep 题意: 问题描述 开学翘课固然快乐,然而也有让呃喵抓狂的事,那当然就是考试了!这可急坏了既要翘课又想要打BC还要准备考试的呃喵. 呃喵为了准备考试没有时间刷题,想打BC ...
- FLINK源代码调试方式
此文已由作者岳猛授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 第一种,直接通过IDEA WINDOWS调试 前提是Flink所有依赖已经导入,直接在Test中打断点,然后直 ...
- BlocksKit的使用
一.引言 众所周知Block已被广泛用于iOS编程.它们通常被用作可并发执行的逻辑单元的封装,或者作为事件触发的回调.Block比传统回调函数有2点优势: 允许在调用点上下文书写执行逻辑,不用分离函数 ...
- Git之常用的命令操作
set LESSCHARSET=utf-8 git设置文件名大小写敏感 git branch -rgit checkout 'TestMaster'git checkout -b 'TestMaste ...
- POJ3692【二分匹配】
题意: 有男生女生,男生都认识双方,女生都认识双方,给出一些男女关系,问最大拿多少个人,使得所有人都认识双方. 思路: 原图最大团=总结点数-[[补图(补图为二分图)]的最大独立集=最大完全子图的顶点 ...
- HDU2819【二分匹配与矩阵的秩】
题意: 给出一个矩阵问能否实现对角线全部是1,能的话输出路径,不能的话输出-1 思路: 首先根据矩阵的性质,这一定是一个满秩矩阵,所以只根据行或列交换就一定能实现. 所以行和列构成二分图,然后跑一发匈 ...
- 51nod1270(dp)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1270 题意:中文题诶- 思路:dp s=abs(a1-a0)+ ...
- [Xcode 实际操作]九、实用进阶-(21)使用“调试视图”查看各界面元素的层次顺序
目录:[Swift]Xcode实际操作 本文将演示如何在程序运行期间,查看模拟器各界面元素的层次顺序. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import ...
- 我被面试官给虐懵了,竟然是因为我不懂Spring中的@Configuration
现在大部分的Spring项目都采用了基于注解的配置,采用了@Configuration 替换标签的做法.一行简单的注解就可以解决很多事情.但是,其实每一个注解背后都有很多值得学习和思考的内容.这些思考 ...