题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1195 , 双向BFS或者直接BFS也可以过。

  其实这道题只是单向BFS就可以过的,但是为了练算法,所以还是用了双向BFS来写。


算法:

  先预处理一下,从1111到9999的所有点进行构图(由于是1~9的,所以除去含有0元素的数字),能进行一次变换变成的数字则表示两点之间连通。然后从初态与目态两个点进行BFS,如果有轨迹重合的就返回路程和。

  这里注意双向BFS要一层一层的进行搜索,不然的话会产生错误,至于错误原因还在思考中。。

双向BFS代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL __int64
#define eps 1e-8
#define INF 1e8
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int MOD = ;
const int maxn = + ;
vector <int> e[maxn];
int vis[maxn] , dist[maxn]; void solve(int x)
{
int num[] , i , tmp , y;
i = ;
tmp = x;
while(tmp) {
num[i++] = tmp % ;
tmp /= ;
}
for(i = ; i < ; i++)
if(num[i] == )
return;
for(i = ; i < ; i++) {
if(i < ) {
swap(num[i] , num[i + ]);
y = num[] * + num[] * + num[] * + num[];
e[x].push_back(y);
e[y].push_back(x);
swap(num[i] , num[i + ]);
}
tmp = num[i];
if(num[i] == )
num[i] = ;
else
num[i]++;
y = num[] * + num[] * + num[] * + num[];
e[x].push_back(y);
e[y].push_back(x);
num[i] = tmp; if(num[i] == )
num[i] = ;
else
num[i]--;
y = num[] * + num[] * + num[] * + num[];
e[x].push_back(y);
e[y].push_back(x);
num[i] = tmp;
}
}
int BFS_2(int start , int end)
{
if(start == end)
return ;
memset(vis , , sizeof(vis));
queue <int> que[];
vis[start] = ;
vis[end] = ;
que[].push(start);
que[].push(end);
dist[start] = dist[end] = ;
while(!que[].empty() && !que[].empty()) {
int k = ;
if(que[].size() < que[].size())
k++;
int u = que[k].front();
que[k].pop();
for(int i = ; i < e[u].size() ; i++) {
int j = e[u][i];
if(!vis[j]) {
vis[j] = vis[u];
que[k].push(j);
dist[j] = dist[u] + ;
} else if(vis[j] == vis[u]) {
continue;
} else {
return dist[j] + dist[u] + ;
}
}
}
return -;
}
int main()
{
int T , a , b;
for(int i = ; i <= ; i++)
solve(i);
cin >> T;
while(T--) {
scanf("%d %d" , &a , &b);
printf("%d\n" , BFS_2(a , b));
}
return ;
}

BFS代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL __int64
#define eps 1e-8
#define INF 1e8
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int MOD = ;
const int maxn = + ;
vector <int> e[maxn];
int vis[maxn] , dist[maxn]; void solve(int x)
{
int num[] , i , tmp , y;
i = ;
tmp = x;
while(tmp) {
num[i++] = tmp % ;
tmp /= ;
}
for(i = ; i < ; i++)
if(num[i] == )
return;
for(i = ; i < ; i++) {
if(i < ) {
swap(num[i] , num[i + ]);
y = num[] * + num[] * + num[] * + num[];
e[x].push_back(y);
e[y].push_back(x);
swap(num[i] , num[i + ]);
}
tmp = num[i];
if(num[i] == )
num[i] = ;
else
num[i]++;
y = num[] * + num[] * + num[] * + num[];
e[x].push_back(y);
e[y].push_back(x);
num[i] = tmp; if(num[i] == )
num[i] = ;
else
num[i]--;
y = num[] * + num[] * + num[] * + num[];
e[x].push_back(y);
e[y].push_back(x);
num[i] = tmp;
}
}
int BFS(int a , int b)
{
if(a == b)
return ;
memset(vis , , sizeof(vis));
queue <int> que;
que.push(a);
vis[a] = ;
dist[a] = ;
while(!que.empty()) {
int u = que.front();
que.pop();
for(int i = ; i < e[u].size() ; i++) {
int j = e[u][i];
if(j == b)
return dist[u] + ;
if(!vis[j]) {
dist[j] = dist[u] + ;
vis[j] = ;
que.push(j);
}
}
}
}
int main()
{
int T , a , b;
for(int i = ; i <= ; i++)
solve(i);
cin >> T;
while(T--) {
scanf("%d %d" , &a , &b);
printf("%d\n" , BFS(a , b));
}
return ;
}

HDU1195 双向BFS(或BFS)的更多相关文章

  1. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  2. POJ1475 Pushing Boxes(BFS套BFS)

    描述 Imagine you are standing inside a two-dimensional maze composed of square cells which may or may ...

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

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

  4. 2601 电路维修 (双端队列bfs\优先队列bfs(最短路))

    描述 Ha'nyu是来自异世界的魔女,她在漫无目的地四处漂流的时候,遇到了善良的少女Rika,从而被收留在地球上.Rika的家里有一辆飞行车.有一天飞行车的电路板突然出现了故障,导致无法启动. 电路板 ...

  5. 【NOIP2013】 华容道 bfs预处理+bfs

    这一题我们考虑一个最裸的算法: 我们设$dp[i][j][k][l]$表示当前棋子在$(i,j)$且空格在$(k,l)$时的最小步数 然后显然随便转移一下就好了,时间复杂度为$O(q(nm)^2)$. ...

  6. Graph 133. Clone Graph in three ways(bfs, dfs, bfs(recursive))

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  7. poj2312 Battle City 【暴力 或 优先队列+BFS 或 BFS】

    题意:M行N列的矩阵.Y:起点,T:终点.S.R不能走,走B花费2,走E花费1.求Y到T的最短时间. 三种解法.♪(^∇^*) //解法一:暴力 //157MS #include<cstdio& ...

  8. UVA - 1601 The Morning after Halloween (BFS/双向BFS/A*)

    题目链接 挺有意思但是代码巨恶心的一道最短路搜索题. 因为图中的结点太多,应当首先考虑把隐式图转化成显式图,即对地图中可以相互连通的点之间连边,建立一个新图(由于每步不需要每个鬼都移动,所以每个点需要 ...

  9. BFS:HDU3085-Nightmare Ⅱ(双向BFS)

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

随机推荐

  1. 【mysql--sql语句小问题总结】

    1:查询过滤条件不为空: <select id="getActivityByIdAndEx" resultMap="BaseResultMap" > ...

  2. Ubuntu16.4下安装Chrome浏览器以及Chromedriver

    一.Chrome浏览器的安装 对于谷歌Chrome32位版本,使用如下链接: wget https://dl.google.com/linux/direct/google-chrome-stable_ ...

  3. vue,webpack,node间的关系

    针对在“思否”上看到的关于vue,node,webpack的一些问题及回复,做出如下的整理,给同样不是很清楚的朋友做了解,也供自己学习 原链接:https://segmentfault.com/q/1 ...

  4. CentOS常用软件安装方法

    软件包介绍 源码包(脚本安装包) 二进制包(RPM包,系统默认包) 源码包 优点 开源,如果有足够的能力,可以修改源代码 编译安装,更加适合自己的系统,稳定高效 缺点 安装步骤较多,容易出错 编译过程 ...

  5. bzoj 4974: [Lydsy八月月赛]字符串大师

    4974: [Lydsy八月月赛]字符串大师 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 371  Solved: 190[Submit][Statu ...

  6. 《权限系列shiro+cas》---封装公共验证模块

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 Introduction 我们现在可以将任意属性传递给每个顶点的顶点着色器使用.但是 ...

  7. Error: Attribute application@allowBackup value=(false) from AndroidManifest.xml:14:7-34 is also present at [:react-native-qq] AndroidManifest.xml:14:18-44 value=(true).

    解决方法: 修改文件: 在manifest标签中添加 xmlns:tools="http://schemas.android.com/tools" 在application标签中添 ...

  8. SCOJ4427 / TOPOI 4404: Miss Zhao's Graph 解题报告

    题目链接 SCOJ TOPOI 题目描述 Problem 给定一个包含n个顶点m条边的带权有向图,找一条边数最多的路径,且路径上的边的权值严格递增.图中可能有重边和自环. Input Data 第一行 ...

  9. saltstack执行结果存储到MySQL

    saltstack执行结果保存到MySQL中,以便进行命令安全审计必须是python2.7以上的环境安装相关模块ubuntu系统安装 apt-get install -y python-mysqldb ...

  10. CentOS6.5安装MySQL5.7(也适合其他版本安装)

     1.查看是否已经安装过mysql或其依赖,若已装过要先将其删除,否则第4步使用yum安装时会报错: 方法一:yum list installed | grep mysql 方法二:rpm -qa | ...