题目链接: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. Fetch超时设置和终止请求

    1.基本使用 Fetch 是一个新的端获取资源的接口,用于替换笨重繁琐XMLHttpRequest.它有了Request 和 Response 以及Headers对象的概念,与后端语言请求资源更接近. ...

  2. (PHP)redis Hash(哈希)操作

    /** * * Hash操作 * 哈希操作 * 可理解为数据库操作 * */ //为user表中的字段赋值.成功返回1,失败返回0.若user表不存在会先创建表再赋值,若字段已存在会覆盖旧值. $re ...

  3. mybatis笔记 - 初始配置及dao的封装

    1.用户实体类 package com.javasm.entity; /** * *TODO 用户表实体类 * @author CaoLei 2018年6月26日上午10:50:12 * Manage ...

  4. 51nod1064(Bash博弈)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1067 题意:中文题诶~ 思路:直接规律就好了... 代码: ...

  5. uva11491 奖品的价值(贪心)

    uva11491 奖品的价值(贪心) 给你一个n位的整数,请你删除其中的d个数字,使得整数尽可能大.1<=d<n<=1e5. 首先因为前面的数位更重要,所以从左往右将每一位数字加入栈 ...

  6. 洛谷P3803 【模板】多项式乘法(FFT)

    P3803 [模板]多项式乘法(FFT) 题目背景 这是一道FFT模板题 题目描述 给定一个n次多项式F(x),和一个m次多项式G(x). 请求出F(x)和G(x)的卷积. 输入输出格式 输入格式: ...

  7. Exadata SL6 是个什么鬼?

    就在 前两天,ORACLE的Exadata家族又发布了一个新成员:SL6. 变化上给人最直观的感觉是:从以前的X86架构变成了SPARC架构. Exadata Database Machine SL6 ...

  8. 原生JS轮播-各种效果的极简实现

    寒假持续摸鱼中~此为老早以前博客的重写,当时还是分开写的,这里汇总重写,正好复习一遍~ 春招我来了! 所有有意思的,一股脑先扔进收藏,然后再也不看哈哈,真是糟糕. 今日事,今日毕,说起来容易. 当时竟 ...

  9. 谈谈python修饰器

    前言 对python的修饰器的理解一直停留在"使用修饰器把函数注册为事件的处理程序"的层次,也是一知半解:这样拖着不是办法,索性今天好好整理一下关于python修饰器的概念及用法. ...

  10. linux下生成随机密码

    常见的简单的两种方法 1.openssl rand -base64 32 2.date | md5sum