HDU1195 双向BFS(或BFS)
题目链接: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)的更多相关文章
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ1475 Pushing Boxes(BFS套BFS)
描述 Imagine you are standing inside a two-dimensional maze composed of square cells which may or may ...
- UVA - 1601 The Morning after Halloween (双向BFS&单向BFS)
题目: w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...
- 2601 电路维修 (双端队列bfs\优先队列bfs(最短路))
描述 Ha'nyu是来自异世界的魔女,她在漫无目的地四处漂流的时候,遇到了善良的少女Rika,从而被收留在地球上.Rika的家里有一辆飞行车.有一天飞行车的电路板突然出现了故障,导致无法启动. 电路板 ...
- 【NOIP2013】 华容道 bfs预处理+bfs
这一题我们考虑一个最裸的算法: 我们设$dp[i][j][k][l]$表示当前棋子在$(i,j)$且空格在$(k,l)$时的最小步数 然后显然随便转移一下就好了,时间复杂度为$O(q(nm)^2)$. ...
- 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 ...
- poj2312 Battle City 【暴力 或 优先队列+BFS 或 BFS】
题意:M行N列的矩阵.Y:起点,T:终点.S.R不能走,走B花费2,走E花费1.求Y到T的最短时间. 三种解法.♪(^∇^*) //解法一:暴力 //157MS #include<cstdio& ...
- UVA - 1601 The Morning after Halloween (BFS/双向BFS/A*)
题目链接 挺有意思但是代码巨恶心的一道最短路搜索题. 因为图中的结点太多,应当首先考虑把隐式图转化成显式图,即对地图中可以相互连通的点之间连边,建立一个新图(由于每步不需要每个鬼都移动,所以每个点需要 ...
- BFS:HDU3085-Nightmare Ⅱ(双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
随机推荐
- rest framework 节流
一.简单节流示例 所谓节流就是控制用户访问频率,这里分为匿名用户(非登录用户)和登录用户的限制. 匿名用户:根据其 IP 限制其频率 登录用户:IP.用户名都 OK 获取用户请求 IP:request ...
- CF 983B XOR-pyramid(区间dp,异或)
CF 983B XOR-pyramid(区间dp,异或) 若有一个长度为m的数组b,定义函数f为: \(f(b) = \begin{cases} b[1] & \quad \text{if } ...
- vue-cli 使用sass(scss)
安装依赖: npm install sass-loader node-sass vue-style-loader --save-dev
- SQL 日期函数转换
1.转换函数 与date操作关系最大的就是两个转换函数:to_date(),to_char() to_date() 作用将字符类型按一定格式转化为日期类型: 具体用法:to_date('2004-11 ...
- AT2301 Solitaire
传送门 这里提供智障的\(O(n^2)\)做法 其实是有\(O(logn)\)做法的,但是我太菜了想不出来 Solution: 首先可以发现生成的序列一定是一个两边向中间单调递减的序列 这样就可以发现 ...
- 洛谷P1505 [国家集训队]旅游
题目描述 \(Ray\) 乐忠于旅游,这次他来到了\(T\) 城.\(T\) 城是一个水上城市,一共有 \(N\) 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,\(T ...
- python基础 3.0 file 读取文件
一.python 文件访问 1.在python中要访问文件,首先要打开文件,也就是open r: 只读 w: 只写 ,文件已存在则清空,不存在则创建 a:追加 ,写到文件末尾.如果文件存在,则在 ...
- Exadata Adaptive Scrubbing Schedule
1.为什么要引入"Hard Disk Scrub and Repair"特性 在exadata的11.2.3.3.0版本中,开始引进了"Automatic Hard Di ...
- MacOS下,Python2和Python3完美兼容使用(转)
问题阐述: MacOS默认Python版本是2.7.10,随着Python3的进一步占有市场,Python2.7也将在2020年结束维护,所以在同一台电脑上安装多个Python版本势在必行. 一.py ...
- Markdown 简单标签语言
Markdownhttps://www.cnblogs.com/ixysy/p/6236782.html Markdown基本语法https://www.jianshu.com/p/191d1e21f ...